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; } /// /// Checks if the track is an advert /// /// true if the track is an advert, false otherwise public bool IsAd() => TrackType == "ad" || Length == 0; /// /// Checks if the track id of type "other" /// /// true if the track is neither an advert nor a normal track, for example a podcast public bool IsOtherTrackType() { return TrackType == "other"; } /// /// Returns a URL to the album cover in the provided size /// /// AlbumArtSize (160,320,640) /// Optional proxy settings /// A String, which is the URL to the Albumart 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", ""); // 0) { string content = matches[0].Groups[1].Value; string[] l = content.Split(new[] { "/" }, StringSplitOptions.None); return "http://o.scdn.co/" + albumsize + @"/" + l[4]; } } return ""; } /// /// Returns a Bitmap of the album cover in the provided size asynchronous /// /// AlbumArtSize (160,320,640) /// Optional proxy settings /// A Bitmap, which is the albumart public async Task 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; } /// /// Returns a byte[] of the the album cover in the provided size asynchronous /// /// AlbumArtSize (160,320,640) /// Optional proxy settings /// A byte[], which is the albumart in binary data public Task 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); } } /// /// Returns a Bitmap of the album cover in the provided size /// /// AlbumArtSize (160,320,640) /// Optional proxy settings /// A Bitmap, which is the albumart 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; } /// /// Returns a byte[] of the album cover in the provided size /// /// AlbumArtSize (160,320,640) /// Optional proxy settings /// A byte[], which is the albumart in binary data 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); } } } }