forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrack.cs
More file actions
187 lines (185 loc) · 6.08 KB
/
Track.cs
File metadata and controls
187 lines (185 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Net;
using System.IO;
namespace SpotifyAPI.SpotifyLocalAPI
{
public class Track
{
public TrackResource track_resource { get; set; }
public TrackResource artist_resource { get; set; }
public TrackResource album_resource { get; set; }
public int length { get; set; }
public string track_type { get; set; }
/// <summary>
/// Returns the track name
/// </summary>
/// <returns>A String. which is the track name</returns>
public String GetTrackName()
{
return track_resource.name;
}
/// <summary>
/// Returns the track lenght
/// </summary>
/// <returns>A integer, which is the track length</returns>
public int GetLength()
{
return length;
}
/// <summary>
/// Returns the URI for the album
/// </summary>
/// <returns>A String, which is the album URI</returns>
public String GetAlbumURI()
{
return album_resource.uri;
}
/// <summary>
/// Returns the URI for the track
/// </summary>
/// <returns>A String, which is the track URI</returns>
public String GetTrackURI()
{
return track_resource.uri;
}
/// <summary>
/// Returns the URI for the artist
/// </summary>
/// <returns>A String, which is the artist URI</returns>
public String GetArtistURI()
{
return artist_resource.uri;
}
/// <summary>
/// Returns the albume name
/// </summary>
/// <returns>A String, which is the album name</returns>
public String GetAlbumName()
{
return album_resource.name;
}
/// <summary>
/// Returns the artist name
/// </summary>
/// <returns>A String, which is the artist name</returns>
public String GetArtistName()
{
return artist_resource.name;
}
/// <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>
public String GetAlbumArtURL(AlbumArtSize size)
{
if (album_resource.uri.Contains("local"))
return "";
int albumsize = 0;
switch (size)
{
case AlbumArtSize.SIZE_160:
albumsize = 160;
break;
case AlbumArtSize.SIZE_320:
albumsize = 320;
break;
case AlbumArtSize.SIZE_640:
albumsize = 640;
break;
}
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]);
}
raw = raw.Replace("\t", ""); ;
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
if (line.Trim().StartsWith("<meta property=\"og:image\""))
{
string[] l = line.Split(new string[] { "/" }, StringSplitOptions.None);
return "http://o.scdn.co/" + albumsize + @"/" + l[4].Replace("\"", "").Replace(">", "");
}
}
return "";
}
/// <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())
{
wc.Proxy = null;
String url = GetAlbumArtURL(size);
if (url == "")
return new Bitmap(640, 640);
byte[] data = null;
try
{
data = await wc.DownloadDataTaskAsync(url);
}
catch(WebException)
{
throw;
}
using (MemoryStream ms = new MemoryStream(data))
{
return (Bitmap)Image.FromStream(ms);
}
}
}
/// <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)
{
using(WebClient wc = new WebClient())
{
wc.Proxy = null;
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))
{
return (Bitmap)Image.FromStream(ms);
}
}
}
}
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; }
}
public class OpenGraphState
{
public Boolean private_session { get; set; }
public Boolean posting_disabled { get; set; }
}
}