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>
|
2015-10-01 16:28:40 +01:00
|
|
|
|
public bool IsAd()
|
|
|
|
|
{
|
2015-10-02 20:02:52 +01:00
|
|
|
|
if (TrackType == "ad")
|
|
|
|
|
return true;
|
|
|
|
|
if (Length == 0)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2015-10-01 16:28:40 +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>
|
|
|
|
|
/// <returns>A String, which is the URL to the Albumart</returns>
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public string GetAlbumArtUrl(AlbumArtSize size)
|
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
|
|
|
|
{
|
2017-06-20 20:31:41 +01:00
|
|
|
|
wc.Headers.Add("User-Agent", "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>
|
|
|
|
|
/// <returns>A Bitmap, which is the albumart</returns>
|
|
|
|
|
public async Task<Bitmap> GetAlbumArtAsync(AlbumArtSize size)
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string url = GetAlbumArtUrl(size);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
if (url == "")
|
|
|
|
|
return null;
|
2016-09-08 11:24:16 +01:00
|
|
|
|
var data = await wc.DownloadDataTaskAsync(url).ConfigureAwait(false);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using (MemoryStream ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
return (Bitmap)Image.FromStream(ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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>
|
|
|
|
|
/// <returns>A byte[], which is the albumart in binary data</returns>
|
2016-09-08 11:06:24 +01:00
|
|
|
|
public Task<byte[]> GetAlbumArtAsByteArrayAsync(AlbumArtSize size)
|
2015-10-10 19:51:21 +01:00
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string url = GetAlbumArtUrl(size);
|
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>
|
|
|
|
|
/// <returns>A Bitmap, which is the albumart</returns>
|
|
|
|
|
public Bitmap GetAlbumArt(AlbumArtSize size)
|
|
|
|
|
{
|
2015-10-02 20:02:52 +01:00
|
|
|
|
using (WebClient wc = new WebClient())
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string url = GetAlbumArtUrl(size);
|
|
|
|
|
if (string.IsNullOrEmpty(url))
|
2015-07-07 17:11:11 +01:00
|
|
|
|
return null;
|
|
|
|
|
var data = wc.DownloadData(url);
|
|
|
|
|
using (MemoryStream ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
return (Bitmap)Image.FromStream(ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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>
|
|
|
|
|
/// <returns>A byte[], which is the albumart in binary data</returns>
|
|
|
|
|
public byte[] GetAlbumArtAsByteArray(AlbumArtSize size)
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string url = GetAlbumArtUrl(size);
|
|
|
|
|
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
|
|
|
|
}
|