2015-10-16 23:44:35 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using SpotifyAPI.Local.Enums;
|
|
|
|
|
using System;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2017-06-20 20:31:41 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Local.Models
|
|
|
|
|
{
|
|
|
|
|
public class Track
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("track_resource")]
|
|
|
|
|
public SpotifyResource TrackResource { get; set; }
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
[JsonProperty("artist_resource")]
|
|
|
|
|
public SpotifyResource ArtistResource { get; set; }
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
[JsonProperty("album_resource")]
|
|
|
|
|
public SpotifyResource AlbumResource { get; set; }
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
[JsonProperty("length")]
|
|
|
|
|
public int Length { get; set; }
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
[JsonProperty("track_type")]
|
|
|
|
|
public string TrackType { get; set; }
|
|
|
|
|
|
2015-10-01 16:28:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the track is an advert
|
|
|
|
|
/// </summary>
|
2015-10-02 20:02:52 +01:00
|
|
|
|
/// <returns>true if the track is an advert, false otherwise</returns>
|
2018-05-06 13:46:05 +01:00
|
|
|
|
public bool IsAd() => TrackType == "ad" || Length == 0;
|
2018-03-25 18:39:19 +01:00
|
|
|
|
|
2018-01-10 13:56:58 +00:00
|
|
|
|
/// <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";
|
|
|
|
|
}
|
2015-10-01 16:28:40 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a URL to the album cover in the provided size
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size">AlbumArtSize (160,320,640)</param>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
/// <param name="proxyConfig">Optional proxy settings</param>
|
2015-07-07 17:11:11 +01:00
|
|
|
|
/// <returns>A String, which is the URL to the Albumart</returns>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
public string GetAlbumArtUrl(AlbumArtSize size, ProxyConfig proxyConfig = null)
|
|
|
|
|
{
|
|
|
|
|
return GetAlbumArtUrl(size, proxyConfig?.CreateWebProxy());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetAlbumArtUrl(AlbumArtSize size, IWebProxy proxy = null)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2015-10-03 15:16:07 +01:00
|
|
|
|
if (AlbumResource.Uri == null || !AlbumResource.Uri.Contains("spotify:album:") || AlbumResource.Uri.Contains("spotify:album:0000000000000000000000"))
|
2015-07-07 17:11:11 +01:00
|
|
|
|
return "";
|
2015-07-14 06:05:16 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
int albumsize = 0;
|
|
|
|
|
switch (size)
|
|
|
|
|
{
|
|
|
|
|
case AlbumArtSize.Size160:
|
|
|
|
|
albumsize = 160;
|
|
|
|
|
break;
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
case AlbumArtSize.Size320:
|
|
|
|
|
albumsize = 320;
|
|
|
|
|
break;
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
case AlbumArtSize.Size640:
|
|
|
|
|
albumsize = 640;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string raw;
|
2015-10-02 20:02:52 +01:00
|
|
|
|
using (WebClient wc = new WebClient())
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2018-03-25 18:39:19 +01:00
|
|
|
|
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");
|
2015-07-07 17:11:11 +01:00
|
|
|
|
raw = wc.DownloadString("http://open.spotify.com/album/" + AlbumResource.Uri.Split(new[] { ":" }, StringSplitOptions.None)[2]);
|
|
|
|
|
}
|
|
|
|
|
raw = raw.Replace("\t", "");
|
2016-04-02 15:11:57 +01:00
|
|
|
|
|
2017-06-20 20:31:41 +01:00
|
|
|
|
// <img id="cover-img" src="https://d3rt1990lpmkn.cloudfront.net/640/e62a04cfea4122961f3b9159493730c27d61f71b" ...
|
2015-07-07 17:11:11 +01:00
|
|
|
|
string[] lines = raw.Split(new[] { "\n" }, StringSplitOptions.None);
|
2017-06-20 20:31:41 +01:00
|
|
|
|
const string pattern = "id=\"cover-img\".*?src=\"(.*?)\"";
|
|
|
|
|
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
foreach (string line in lines)
|
|
|
|
|
{
|
2017-06-20 20:31:41 +01:00
|
|
|
|
MatchCollection matches = rgx.Matches(line);
|
|
|
|
|
if (matches.Count > 0)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2017-06-20 20:31:41 +01:00
|
|
|
|
string content = matches[0].Groups[1].Value;
|
2016-04-02 15:11:57 +01:00
|
|
|
|
string[] l = content.Split(new[] { "/" }, StringSplitOptions.None);
|
2017-06-20 20:31:41 +01:00
|
|
|
|
return "http://o.scdn.co/" + albumsize + @"/" + l[4];
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2015-10-10 19:51:21 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a Bitmap of the album cover in the provided size asynchronous
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size">AlbumArtSize (160,320,640)</param>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
/// <param name="proxyConfig">Optional proxy settings</param>
|
2015-07-07 17:11:11 +01:00
|
|
|
|
/// <returns>A Bitmap, which is the albumart</returns>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
public async Task<Bitmap> GetAlbumArtAsync(AlbumArtSize size, ProxyConfig proxyConfig = null)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2018-03-25 18:39:19 +01:00
|
|
|
|
var data = await GetAlbumArtAsByteArrayAsync(size, proxyConfig).ConfigureAwait(false);
|
|
|
|
|
if (data != null)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
return (Bitmap)Image.FromStream(ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-25 18:39:19 +01:00
|
|
|
|
|
|
|
|
|
return null;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
2015-10-10 19:51:21 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a byte[] of the the album cover in the provided size asynchronous
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size">AlbumArtSize (160,320,640)</param>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
/// <param name="proxyConfig">Optional proxy settings</param>
|
2015-10-10 19:51:21 +01:00
|
|
|
|
/// <returns>A byte[], which is the albumart in binary data</returns>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
public Task<byte[]> GetAlbumArtAsByteArrayAsync(AlbumArtSize size, ProxyConfig proxyConfig = null)
|
2015-10-10 19:51:21 +01:00
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2018-03-25 18:39:19 +01:00
|
|
|
|
IWebProxy proxy = proxyConfig?.CreateWebProxy();
|
|
|
|
|
wc.Proxy = proxy;
|
|
|
|
|
|
|
|
|
|
string url = GetAlbumArtUrl(size, proxy);
|
2015-10-10 19:51:21 +01:00
|
|
|
|
if (url == "")
|
|
|
|
|
return null;
|
2016-09-08 11:06:24 +01:00
|
|
|
|
return wc.DownloadDataTaskAsync(url);
|
2015-10-10 19:51:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a Bitmap of the album cover in the provided size
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size">AlbumArtSize (160,320,640)</param>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
/// <param name="proxyConfig">Optional proxy settings</param>
|
2015-07-07 17:11:11 +01:00
|
|
|
|
/// <returns>A Bitmap, which is the albumart</returns>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
public Bitmap GetAlbumArt(AlbumArtSize size, ProxyConfig proxyConfig = null)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2018-03-25 18:39:19 +01:00
|
|
|
|
var data = GetAlbumArtAsByteArray(size, proxyConfig);
|
|
|
|
|
if (data != null)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
return (Bitmap)Image.FromStream(ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-25 18:39:19 +01:00
|
|
|
|
|
|
|
|
|
return null;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
2015-10-10 19:51:21 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a byte[] of the album cover in the provided size
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size">AlbumArtSize (160,320,640)</param>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
/// <param name="proxyConfig">Optional proxy settings</param>
|
2015-10-10 19:51:21 +01:00
|
|
|
|
/// <returns>A byte[], which is the albumart in binary data</returns>
|
2018-03-25 18:39:19 +01:00
|
|
|
|
public byte[] GetAlbumArtAsByteArray(AlbumArtSize size, ProxyConfig proxyConfig = null)
|
2015-10-10 19:51:21 +01:00
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2018-03-25 18:39:19 +01:00
|
|
|
|
IWebProxy proxy = proxyConfig?.CreateWebProxy();
|
|
|
|
|
wc.Proxy = proxy;
|
|
|
|
|
|
|
|
|
|
string url = GetAlbumArtUrl(size, proxy);
|
2016-03-31 11:08:23 +01:00
|
|
|
|
if (string.IsNullOrEmpty(url))
|
2015-10-10 19:51:21 +01:00
|
|
|
|
return null;
|
|
|
|
|
return wc.DownloadData(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
2015-10-16 23:44:35 +01:00
|
|
|
|
}
|