Spotify.NET/SpotifyAPI/Track.cs

146 lines
4.3 KiB
C#
Raw Normal View History

2014-01-07 15:26:03 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2014-01-08 22:22:54 +00:00
using System.Drawing;
using System.Net;
using System.IO;
2014-01-07 15:26:03 +00:00
namespace SpotifyAPIv1
{
2014-01-08 22:22:54 +00:00
public class Track
2014-01-07 15:26:03 +00:00
{
2014-01-08 22:22:54 +00:00
public TrackResource track_resource { get; set; }
public TrackResource artist_resource { get; set; }
public TrackResource album_resource { get; set; }
2014-01-10 07:09:14 +00:00
public int length { get; set; }
public string track_type { get; set; }
2014-01-07 15:26:03 +00:00
2014-02-01 12:52:33 +00:00
public String GetTrackName()
2014-01-07 15:26:03 +00:00
{
2014-01-08 22:22:54 +00:00
return track_resource.name;
2014-01-07 15:26:03 +00:00
}
2014-01-10 07:09:14 +00:00
public int GetLength()
{
return length;
}
2014-02-01 12:52:33 +00:00
public String GetAlbumURI()
2014-01-10 07:09:14 +00:00
{
2014-02-01 12:52:33 +00:00
return album_resource.uri;
2014-01-10 07:09:14 +00:00
}
2014-02-01 12:52:33 +00:00
public String GetTrackURI()
{
return track_resource.uri;
}
public String GetArtistURI()
{
return artist_resource.uri;
}
public String GetAlbumName()
2014-01-08 22:22:54 +00:00
{
return album_resource.name;
}
2014-02-01 12:52:33 +00:00
public String GetArtistName()
2014-01-08 22:22:54 +00:00
{
return artist_resource.name;
}
2014-02-01 12:52:33 +00:00
public String GetAlbumArtURL(AlbumArtSize size)
2014-01-08 22:22:54 +00:00
{
2014-02-01 12:52:33 +00:00
if (album_resource.uri.Contains("local"))
return "";
2014-01-08 22:22:54 +00:00
int albumsize = 0;
switch (size)
{
2014-02-01 12:52:33 +00:00
case AlbumArtSize.SIZE_160:
2014-01-08 22:22:54 +00:00
albumsize = 160;
break;
2014-02-01 12:52:33 +00:00
case AlbumArtSize.SIZE_320:
2014-01-08 22:22:54 +00:00
albumsize = 320;
break;
2014-02-01 12:52:33 +00:00
case AlbumArtSize.SIZE_640:
2014-01-08 22:22:54 +00:00
albumsize = 640;
break;
}
2014-01-10 07:09:14 +00:00
String raw = "";
using(WebClient wc = new WebClient())
{
wc.Proxy = null;
raw = wc.DownloadString("http://open.spotify.com/album/" + album_resource.uri.Split(new string[] { ":" }, StringSplitOptions.None)[2]);
}
2014-01-08 22:22:54 +00:00
raw = raw.Replace("\t", ""); ;
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
if (line.StartsWith("<meta property=\"og:image\""))
{
string[] l = line.Split(new string[] { "/" }, StringSplitOptions.None);
return "http://o.scdn.co/" + albumsize + @"/" + l[4].Replace("\"", "").Replace(">", "");
}
}
return "";
}
2014-02-01 12:52:33 +00:00
public async Task<Bitmap> GetAlbumArtAsync(AlbumArtSize size)
2014-01-08 22:22:54 +00:00
{
2014-02-01 12:52:33 +00:00
using (WebClient wc = new WebClient())
2014-01-08 22:22:54 +00:00
{
2014-02-01 12:52:33 +00:00
wc.Proxy = null;
String url = GetAlbumArtURL(size);
if (url == "")
return new Bitmap(640, 640);
byte[] data = null;
2014-02-01 12:52:33 +00:00
try
{
data = await wc.DownloadDataTaskAsync(url);
2014-02-01 12:52:33 +00:00
}
catch(WebException e)
{
throw;
2014-02-01 12:52:33 +00:00
}
using (MemoryStream ms = new MemoryStream(data))
2014-02-01 12:52:33 +00:00
{
return (Bitmap)Image.FromStream(ms);
}
2014-01-08 22:22:54 +00:00
}
2014-02-01 12:52:33 +00:00
}
public Bitmap GetAlbumArt(AlbumArtSize size)
{
2014-01-10 07:09:14 +00:00
using(WebClient wc = new WebClient())
2014-01-08 22:22:54 +00:00
{
2014-01-10 07:09:14 +00:00
wc.Proxy = null;
2014-02-01 12:52:33 +00:00
String url = GetAlbumArtURL(size);
if (url == "")
return new Bitmap(640,640);
byte[] data = null;
try
{
data = wc.DownloadData(url);
}
catch (WebException e)
{
throw;
}
using (MemoryStream ms = new MemoryStream(data))
2014-01-08 22:22:54 +00:00
{
2014-02-01 12:52:33 +00:00
return (Bitmap)Image.FromStream(ms);
2014-01-08 22:22:54 +00:00
}
}
}
}
public class TrackResource
{
public String name { get; set; }
public String uri { get; set; }
public TrackResourceLocation location { get; set; }
}
public class TrackResourceLocation
{
public String og { get; set; }
}
2014-01-10 07:09:14 +00:00
public class OpenGraphState
2014-01-08 22:22:54 +00:00
{
public Boolean private_session { get; set; }
public Boolean posting_disabled { get; set; }
2014-01-07 15:26:03 +00:00
}
}