-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMusicTrack.cs
76 lines (58 loc) · 2.37 KB
/
MusicTrack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using AngleSharp.Dom;
using YoutubeExplode;
using YoutubeExplode.Videos;
using YoutubeExplode.Videos.Streams;
using SpotifyExplode;
using SpotifyExplode.Tracks;
using MessagePack;
namespace Music
{
[MessagePackObject]
public struct MusicTrack
{
public MusicTrack(string name, List<string> artists, string url)
{
Name = name;
URL = url;
Artists = artists;
}
[Key(0)]
public string Name { get; }
[Key(1)]
public List<string> Artists { get; }
[Key(2)]
public string URL { get; }
public async Task DownloadAsync(string directory)
{
Url url = new Url(URL);
if (url.Host.Contains("spotify"))
{
SpotifyClient spotify = new SpotifyClient();
string? youtubeId = await spotify.Tracks.GetYoutubeIdAsync(URL.ToString());
url = new Url("https://youtube.com/watch?v=" + youtubeId);
}
YoutubeClient client = new YoutubeClient();
StreamManifest manifest = await client.Videos.Streams.GetManifestAsync(url.ToString());
IStreamInfo streamInfo = manifest.GetAudioOnlyStreams().GetWithHighestBitrate();
await client.Videos.Streams.DownloadAsync(streamInfo, $"{Path.TrimEndingDirectorySeparator(directory)}/{string.Join(", ", Artists)} - {Name}.{streamInfo.Container}");
}
public static async Task<MusicTrack> FromUrlAsync(string URL)
{
Url url = new Url(URL);
if (url.Host.Contains("spotify"))
{
SpotifyClient spotify = new SpotifyClient();
Track track = await spotify.Tracks.GetAsync(url.ToString());
string? youtubeId = await spotify.Tracks.GetYoutubeIdAsync(url.ToString());
return new MusicTrack(track.Title, track.Artists.Select(x => x.Name).ToList(), "https://youtube.com/watch?v=" + youtubeId);
}
else
{
YoutubeClient client = new YoutubeClient();
Video video = await client.Videos.GetAsync(url.ToString());
return new MusicTrack(video.Title, new List<string>() { video.Author.ChannelTitle }, url.ToString());
}
}
public override string ToString() => $"[{string.Join(", ", Artists)} - {Name}]({URL})";
}
}