Update copyright informations.
[lastgo:lastgo.git] / LastGo / apiclient.cpp
1 /*
2  * apiclient.cpp -- Contains all methods related to Last.fm API
3  *
4  * Copyright (C) 2010 Andrea Grandi
5  * Authors: Andrea Grandi <a.grandi@gmail.com>
6  *
7  * This file is part of LastGo and is published under the GNU GPLv2
8  * See the README file for more details.
9  */
10
11 #include "apiclient.h"
12 #include <QXmlStreamReader>
13 #include <QDomDocument>
14 #include <QDebug>
15 #include <QMap>
16
17 ApiClient::ApiClient(QString key, QString secret, QObject *parent) :
18     QObject(parent)
19 {
20     api_key = key;
21     api_secret = secret;
22 }
23
24 void ApiClient::authGetMobileSession(QString username, QString password)
25 {
26     Utils *u = new Utils();
27
28     QString token = u->getAuthToken(username, password);
29
30     QMap<QString, QString> params;
31     params["username"] = username;
32     params["authToken"] = token;
33     params["api_key"] = api_key;
34     params["method"] = "auth.getMobileSession";
35
36     QString signature = u->buildSignature(params, api_secret);
37     params["api_sig"] = signature;
38
39     QString api_url = u->buildApiUrl(LASTFM_BASE_URL, params);
40
41     Requests *r = new Requests(this);
42     connect(r, SIGNAL(dataAvailable(QString)), this, SLOT(authGetMobileSessionReply(QString)));
43     r->apiRequest(api_url);
44 }
45
46 void ApiClient::authGetMobileSessionReply(QString data)
47 {
48     QXmlStreamReader xml;
49     xml.addData(data);
50
51     QMap<QString, QString> response;
52     QString status;
53
54     while (!xml.atEnd())
55     {
56         xml.readNext();
57
58         if (xml.isStartElement())
59         {
60             if(xml.name() == "lfm")
61             {
62                 status = xml.attributes().value("status").toString();
63                 response["status"] = status;
64             }
65             else
66             {
67                 if(status == "ok")
68                 {
69                     if(xml.name() == "name")
70                         response[xml.name().toString()] = xml.readElementText();
71                     if(xml.name() == "key")
72                         response[xml.name().toString()] = xml.readElementText();
73                     if(xml.name() == "subscriber")
74                         response[xml.name().toString()] = xml.readElementText();
75                 }
76                 else if(status == "failed")
77                 {
78                     if(xml.name() == "error")
79                     {
80                         response[xml.name().toString()] = xml.attributes().value("code").toString();
81                         response["message"] = xml.readElementText();
82                     }
83                 }
84             }
85         }
86     }
87
88     emit authGetMobileSessionData(response);
89 }
90
91 void ApiClient::userShout(QString user, QString message, QString sk)
92 {
93     Utils *u = new Utils();
94
95     QMap<QString, QString> params;
96     params["user"] = user;
97     params["message"] = message;
98     params["method"] = "user.shout";
99     params["api_key"] = api_key;
100     params["sk"] = sk;
101
102     QString signature = u->buildSignature(params, api_secret);
103     params["api_sig"] = signature;
104
105     QByteArray post_params = u->buildPostParameters(params);
106
107     Requests *r = new Requests(this);
108     connect(r, SIGNAL(dataAvailable(QString)), this, SLOT(userShoutReply(QString)));
109     r->apiRequestPost(LASTFM_BASE_URL, post_params);
110 }
111
112 void ApiClient::userShoutReply(QString data)
113 {
114     QXmlStreamReader xml;
115     xml.addData(data);
116
117     QMap<QString, QString> response;
118     QString status;
119
120     while (!xml.atEnd())
121     {
122         xml.readNext();
123
124         if (xml.isStartElement())
125         {
126             if(xml.name() == "lfm")
127             {
128                 status = xml.attributes().value("status").toString();
129                 response["status"] = status;
130             }
131             else
132             {
133                 if(status == "failed")
134                 {
135                     if(xml.name() == "error")
136                     {
137                         response[xml.name().toString()] = xml.attributes().value("code").toString();
138                         response["message"] = xml.readElementText();
139                     }
140                 }
141             }
142         }
143     }
144
145     emit userShoutData(response);
146 }
147
148 void ApiClient::radioTune(QString station, QString sk)
149 {
150     Utils *u = new Utils();
151
152     QMap<QString, QString> params;
153     params["station"] = station;
154     params["method"] = "radio.tune";
155     params["api_key"] = api_key;
156     params["sk"] = sk;
157
158     QString signature = u->buildSignature(params, api_secret);
159     params["api_sig"] = signature;
160
161     QByteArray post_params = u->buildPostParameters(params);
162
163     Requests *r = new Requests(this);
164     connect(r, SIGNAL(dataAvailable(QString)), this, SLOT(radioTuneReply(QString)));
165     r->apiRequestPost(LASTFM_BASE_URL, post_params);
166 }
167
168 void ApiClient::radioTuneReply(QString data)
169 {
170     QXmlStreamReader xml;
171     xml.addData(data);
172
173     QMap<QString, QString> response;
174     QString status;
175
176     while (!xml.atEnd())
177     {
178         xml.readNext();
179
180         if (xml.isStartElement())
181         {
182             if(xml.name() == "lfm")
183             {
184                 status = xml.attributes().value("status").toString();
185                 response["status"] = status;
186             }
187             else
188             {
189                 if(status == "ok")
190                 {
191                     if(xml.name() == "type")
192                         response[xml.name().toString()] = xml.readElementText();
193                     if(xml.name() == "name")
194                         response[xml.name().toString()] = xml.readElementText();
195                     if(xml.name() == "url")
196                         response[xml.name().toString()] = xml.readElementText();
197                     if(xml.name() == "supportsdiscovery")
198                         response[xml.name().toString()] = xml.readElementText();
199                 }
200                 else if(status == "failed")
201                 {
202                     if(xml.name() == "error")
203                     {
204                         response[xml.name().toString()] = xml.attributes().value("code").toString();
205                         response["message"] = xml.readElementText();
206                     }
207                 }
208             }
209         }
210     }
211
212     emit radioTuneData(response);
213 }
214
215 void ApiClient::radioGetPlaylist(QString sk)
216 {
217     Utils *u = new Utils();
218
219     QMap<QString, QString> params;
220     params["method"] = "radio.getplaylist";
221     params["api_key"] = api_key;
222     params["sk"] = sk;
223
224     QString signature = u->buildSignature(params, api_secret);
225     params["api_sig"] = signature;
226
227     QString api_url = u->buildApiUrl(LASTFM_BASE_URL, params);
228
229     Requests *r = new Requests(this);
230     connect(r, SIGNAL(dataAvailable(QString)), this, SLOT(radioGetPlaylistReply(QString)));
231     r->apiRequest(api_url);
232 }
233
234 void ApiClient::radioGetPlaylistReply(QString data)
235 {
236     QDomDocument doc("Playlist");
237     doc.setContent(data.toUtf8());
238
239     QDomElement elem = doc.documentElement();
240     QString status = elem.attribute("status");
241
242     QList<LastfmTrack*> track_list;
243
244     if(status == "ok")
245     {
246         QDomNodeList tracks = elem.elementsByTagName("playlist").
247                               at(0).toElement().elementsByTagName("trackList").
248                               at(0).toElement().elementsByTagName("track");
249
250         qDebug() << elem.elementsByTagName("playlist").at(0).toElement().elementsByTagName("title").at(0).toElement().text();
251
252         for(int i = 0; i < tracks.count(); i++)
253         {
254             LastfmTrack *track = new LastfmTrack();
255
256             track->Location = tracks.at(i).toElement().elementsByTagName("location").at(0).toElement().text();
257             track->Title = tracks.at(i).toElement().elementsByTagName("title").at(0).toElement().text();
258             track->Identifier = tracks.at(i).toElement().elementsByTagName("identifier").at(0).toElement().text();
259             track->Album = tracks.at(i).toElement().elementsByTagName("album").at(0).toElement().text();
260             track->Creator = tracks.at(i).toElement().elementsByTagName("creator").at(0).toElement().text();
261             track->Duration = tracks.at(i).toElement().elementsByTagName("duration").at(0).toElement().text();
262             track->ArtistPage = tracks.at(i).toElement().elementsByTagName("artistpage").at(0).toElement().text();
263             track->AlbumPage = tracks.at(i).toElement().elementsByTagName("albumpage").at(0).toElement().text();
264             track->TrackPage = tracks.at(i).toElement().elementsByTagName("trackpage").at(0).toElement().text();
265             track->Image = tracks.at(i).toElement().elementsByTagName("image").at(0).toElement().text();
266
267             track_list.append(track);
268         }
269     }
270
271     emit radioGetPlaylistData(track_list);
272 }