/
track.cs
165 lines (158 loc) · 8.06 KB
/
track.cs
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
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace lastfm
{
public class track
{
public static async Task<List<trackInfo>> search(string trackName, int page = 0, int limit = 30)
{
RequestParameters rParams = new RequestParameters("track.search");
rParams.Add("track", trackName);
rParams.Add("limit", limit.ToString());
rParams.Add("page", page.ToString());
XDocument returnedXml = await Request.MakeRequest(rParams);
if (Request.CheckStatus(returnedXml) == 0)
{
List<trackInfo> tracks = new List<trackInfo>((from item in returnedXml.Descendants("trackmatches").Elements() select new trackInfo(item)));
XNamespace opensearch = @"http://a9.com/-/spec/opensearch/1.1/";
IEnumerable<XElement> opensearch_ = from el in returnedXml.Element("lfm").Element("results").Elements()
where el.Name.Namespace == opensearch
select el;
int totalResults = Int32.Parse((from el in opensearch_ where el.Name.LocalName == "totalResults" select el.Value).First());
int startIndex = Int32.Parse((from el in opensearch_ where el.Name.LocalName == "startIndex" select el.Value).First());
int itemsPerPage = Int32.Parse((from el in opensearch_ where el.Name.LocalName == "itemsPerPage" select el.Value).First());
if (totalResults - startIndex < 0)
throw new IndexOutOfRangeException("Page being shown is the first page");
return tracks;
}
else
throw new LastFmAPIException(returnedXml);
}
public static async Task<trackInfo> getInfo(string artistName, string trackName, string username = "")
{
RequestParameters rParams = new RequestParameters("track.getinfo");
rParams.Add("artist", artistName);
rParams.Add("track", trackName);
if (!string.IsNullOrEmpty(username))
rParams.Add("username", username);
XDocument returnedXml = await Request.MakeRequest(rParams);
if (Request.CheckStatus(returnedXml) == 0)
{
trackInfo track = new trackInfo(returnedXml.Element("lfm").Element("track"));
return track;
}
else if (Request.CheckStatus(returnedXml) == 6) // Track not found
{
// Trying with autocorrect
rParams.Add("autocorrect", "1");
returnedXml = await Request.MakeRequest(rParams);
if (Request.CheckStatus(returnedXml) == 0)
{
trackInfo track = new trackInfo(returnedXml.Element("lfm").Element("track"));
return track;
}
else
throw new LastFmAPIException(returnedXml);
}
else
throw new LastFmAPIException(returnedXml);
}
/// <summary>
/// Sends scrobble request to the server
/// </summary>
/// <param name="artistName"> Artist who plays the track </param>
/// <param name="trackName"> Name of the track </param>
/// <param name="timestamp"> DateTime object representing when track was played </param>
public static async void scrobble(string artistName, string trackName, DateTime timestamp = default(DateTime))
{
if (!Session.CanUseCurrentSession())
MessageBox.Show("This service requires authentication");
int timeStamp;
if (timestamp != default(DateTime) && timestamp != null)
timeStamp = (int)(timestamp.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
else
timeStamp = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
RequestParameters rParams = new RequestParameters("track.scrobble");
rParams.Add("artist", artistName);
rParams.Add("track", trackName);
rParams.Add("timestamp", timeStamp.ToString());
rParams.Add("sk", Session.CurrentSession.SessionKey);
XDocument returnedXml = await Request.MakeRequest(rParams, true);
if (Request.CheckStatus(returnedXml) != 0)
throw new LastFmAPIException(returnedXml);
}
public static async void scrobble(List<trackInfo> tracks)
{
if (!Session.CanUseCurrentSession())
MessageBox.Show("This service requires authentication");
if (tracks.Count > 50)
throw new ArgumentOutOfRangeException("tracks", "Number of elements in the list must not exceed 50");
RequestParameters rParams = new RequestParameters("track.scrobble");
rParams.Add("sk", Session.CurrentSession.SessionKey);
for (int i = 0; i < tracks.Count; ++i)
{
trackInfo track = tracks[i];
if (track != null && track.name != null && track.album != null && track.date != null)
{
rParams.Add("artist[" + i + "]", track.artist.name);
rParams.Add("track[" + i + "]", track.name);
int timestamp = (int)(track.date.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
rParams.Add("timestamp[" + i + "]", timestamp.ToString());
}
}
XDocument returnedXml = await Request.MakeRequest(rParams, true);
if (Request.CheckStatus(returnedXml) != 0)
throw new LastFmAPIException(returnedXml);
}
public static async void updateNowPlaying(string artistName, string trackName, string albumName = null)
{
if (!Session.CanUseCurrentSession())
MessageBox.Show("This service requires authentication");
RequestParameters rParams = new RequestParameters("track.updateNowPlaying");
rParams.Add("artist", artistName);
rParams.Add("track", trackName);
rParams.Add("sk", Session.CurrentSession.SessionKey);
if (!String.IsNullOrEmpty(albumName))
rParams.Add("album", albumName);
XDocument returnedXml = await Request.MakeRequest(rParams, true);
if (Request.CheckStatus(returnedXml) != 0)
throw new LastFmAPIException(returnedXml);
}
public static async void love(string trackName, string artistName)
{
if (!Session.CanUseCurrentSession())
throw new UnauthorizedException("User must login to perform this action");
RequestParameters rParams = new RequestParameters("track.love");
rParams.Add("artist", artistName);
rParams.Add("track", trackName);
rParams.Add("sk", Session.CurrentSession.SessionKey);
XDocument returnedXml = await Request.MakeRequest(rParams, true);
if (Request.CheckStatus(returnedXml) != 0)
throw new LastFmAPIException(returnedXml);
}
public static async void unlove(string trackName, string artistName)
{
if (!Session.CanUseCurrentSession())
throw new UnauthorizedException("User must login to perform this action");
RequestParameters rParams = new RequestParameters("track.unlove");
rParams.Add("artist", artistName);
rParams.Add("track", trackName);
rParams.Add("sk", Session.CurrentSession.SessionKey);
XDocument returnedXml = await Request.MakeRequest(rParams, true);
if (Request.CheckStatus(returnedXml) != 0)
throw new LastFmAPIException(returnedXml);
}
}
}