Spotify.NET/SpotifyAPI/SpoitfyLocalAPI/Track.cs

188 lines
6.1 KiB
C#
Raw Permalink Normal View History

2014-01-07 15:26:03 +00:00
using System;
using System.Collections.Generic;
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 SpotifyAPI.SpotifyLocalAPI
2014-01-07 15:26:03 +00:00
{
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-13 13:23:52 +00:00
/// <summary>
/// Returns the track name
/// </summary>
/// <returns>A String. which is the track name</returns>
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-02-13 13:23:52 +00:00
/// <summary>
/// Returns the track lenght
/// </summary>
/// <returns>A integer, which is the track length</returns>
2014-01-10 07:09:14 +00:00
public int GetLength()
{
return length;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the URI for the album
/// </summary>
/// <returns>A String, which is the album URI</returns>
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-13 13:23:52 +00:00
/// <summary>
/// Returns the URI for the track
/// </summary>
/// <returns>A String, which is the track URI</returns>
2014-02-01 12:52:33 +00:00
public String GetTrackURI()
{
return track_resource.uri;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the URI for the artist
/// </summary>
/// <returns>A String, which is the artist URI</returns>
2014-02-01 12:52:33 +00:00
public String GetArtistURI()
{
return artist_resource.uri;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the albume name
/// </summary>
/// <returns>A String, which is the album name</returns>
2014-02-01 12:52:33 +00:00
public String GetAlbumName()
2014-01-08 22:22:54 +00:00
{
return album_resource.name;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the artist name
/// </summary>
/// <returns>A String, which is the artist name</returns>
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-13 13:23:52 +00: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>
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-13 13:23:52 +00: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>
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)
2014-02-01 12:52:33 +00:00
{
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
}
2014-02-13 13:23:52 +00: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>
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
}
}