forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrack.cs
More file actions
executable file
·181 lines (160 loc) · 6.77 KB
/
Copy pathTrack.cs
File metadata and controls
executable file
·181 lines (160 loc) · 6.77 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using Newtonsoft.Json;
using SpotifyAPI.Local.Enums;
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SpotifyAPI.Local.Models
{
public class Track
{
[JsonProperty("track_resource")]
public SpotifyResource TrackResource { get; set; }
[JsonProperty("artist_resource")]
public SpotifyResource ArtistResource { get; set; }
[JsonProperty("album_resource")]
public SpotifyResource AlbumResource { get; set; }
[JsonProperty("length")]
public int Length { get; set; }
[JsonProperty("track_type")]
public string TrackType { get; set; }
/// <summary>
/// Checks if the track is an advert
/// </summary>
/// <returns>true if the track is an advert, false otherwise</returns>
public bool IsAd() => TrackType == "ad" || Length == 0;
/// <summary>
/// Checks if the track id of type "other"
/// </summary>
/// <returns>true if the track is neither an advert nor a normal track, for example a podcast</returns>
public bool IsOtherTrackType()
{
return TrackType == "other";
}
/// <summary>
/// Returns a URL to the album cover in the provided size
/// </summary>
/// <param name="size">AlbumArtSize (160,320,640)</param>
/// <param name="proxyConfig">Optional proxy settings</param>
/// <returns>A String, which is the URL to the Albumart</returns>
public string GetAlbumArtUrl(AlbumArtSize size, ProxyConfig proxyConfig = null)
{
return GetAlbumArtUrl(size, proxyConfig?.CreateWebProxy());
}
private string GetAlbumArtUrl(AlbumArtSize size, IWebProxy proxy = null)
{
if (AlbumResource.Uri == null || !AlbumResource.Uri.Contains("spotify:album:") || AlbumResource.Uri.Contains("spotify:album:0000000000000000000000"))
return "";
int albumsize = 0;
switch (size)
{
case AlbumArtSize.Size160:
albumsize = 160;
break;
case AlbumArtSize.Size320:
albumsize = 320;
break;
case AlbumArtSize.Size640:
albumsize = 640;
break;
}
string raw;
using (WebClient wc = new WebClient())
{
wc.Proxy = proxy;
wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
raw = wc.DownloadString("http://open.spotify.com/album/" + AlbumResource.Uri.Split(new[] { ":" }, StringSplitOptions.None)[2]);
}
raw = raw.Replace("\t", "");
// <img id="cover-img" src="https://d3rt1990lpmkn.cloudfront.net/640/e62a04cfea4122961f3b9159493730c27d61f71b" ...
string[] lines = raw.Split(new[] { "\n" }, StringSplitOptions.None);
const string pattern = "id=\"cover-img\".*?src=\"(.*?)\"";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
foreach (string line in lines)
{
MatchCollection matches = rgx.Matches(line);
if (matches.Count > 0)
{
string content = matches[0].Groups[1].Value;
string[] l = content.Split(new[] { "/" }, StringSplitOptions.None);
return "http://o.scdn.co/" + albumsize + @"/" + l[4];
}
}
return "";
}
/// <summary>
/// Returns a Bitmap of the album cover in the provided size asynchronous
/// </summary>
/// <param name="size">AlbumArtSize (160,320,640)</param>
/// <param name="proxyConfig">Optional proxy settings</param>
/// <returns>A Bitmap, which is the albumart</returns>
public async Task<Bitmap> GetAlbumArtAsync(AlbumArtSize size, ProxyConfig proxyConfig = null)
{
var data = await GetAlbumArtAsByteArrayAsync(size, proxyConfig).ConfigureAwait(false);
if (data != null)
{
using (MemoryStream ms = new MemoryStream(data))
{
return (Bitmap)Image.FromStream(ms);
}
}
return null;
}
/// <summary>
/// Returns a byte[] of the the album cover in the provided size asynchronous
/// </summary>
/// <param name="size">AlbumArtSize (160,320,640)</param>
/// <param name="proxyConfig">Optional proxy settings</param>
/// <returns>A byte[], which is the albumart in binary data</returns>
public Task<byte[]> GetAlbumArtAsByteArrayAsync(AlbumArtSize size, ProxyConfig proxyConfig = null)
{
using (WebClient wc = new WebClient())
{
IWebProxy proxy = proxyConfig?.CreateWebProxy();
wc.Proxy = proxy;
string url = GetAlbumArtUrl(size, proxy);
if (url == "")
return null;
return wc.DownloadDataTaskAsync(url);
}
}
/// <summary>
/// Returns a Bitmap of the album cover in the provided size
/// </summary>
/// <param name="size">AlbumArtSize (160,320,640)</param>
/// <param name="proxyConfig">Optional proxy settings</param>
/// <returns>A Bitmap, which is the albumart</returns>
public Bitmap GetAlbumArt(AlbumArtSize size, ProxyConfig proxyConfig = null)
{
var data = GetAlbumArtAsByteArray(size, proxyConfig);
if (data != null)
{
using (MemoryStream ms = new MemoryStream(data))
{
return (Bitmap)Image.FromStream(ms);
}
}
return null;
}
/// <summary>
/// Returns a byte[] of the album cover in the provided size
/// </summary>
/// <param name="size">AlbumArtSize (160,320,640)</param>
/// <param name="proxyConfig">Optional proxy settings</param>
/// <returns>A byte[], which is the albumart in binary data</returns>
public byte[] GetAlbumArtAsByteArray(AlbumArtSize size, ProxyConfig proxyConfig = null)
{
using (WebClient wc = new WebClient())
{
IWebProxy proxy = proxyConfig?.CreateWebProxy();
wc.Proxy = proxy;
string url = GetAlbumArtUrl(size, proxy);
if (string.IsNullOrEmpty(url))
return null;
return wc.DownloadData(url);
}
}
}
}