9506ed8786af504201de1f23a32de7b1802acb4c
[groove] / gscom.cpp
1 #include "gscom.h"
2 #include <parser.h>
3 #include <serializer.h>
4 #include <QCryptographicHash>
5 //#include <QApplication>
6
7 gscom::gscom()
8 {
9     manager = new QNetworkAccessManager(this);
10     connect(manager, SIGNAL(finished(QNetworkReply*)),
11             this, SLOT(replyFinished(QNetworkReply*)));
12     cookies = manager->cookieJar();
13     currentaction = getPHP;
14     manager->get(QNetworkRequest(QUrl(QString("http://") + GS_LISTEN)));
15     phpSession = new QString("none");
16     sessionKey = new QString("none");
17     firstR = true;
18     model = new QStandardItemModel();
19     model->setHorizontalHeaderLabels(
20         QStringList() << "Online");
21     addDebugMsg("You may now search for a song");
22     //addProgressbar("test");
23     //getSessionKey();
24 }
25
26 QStandardItemModel* gscom::getSongModel(QString song)
27 {
28     model = new QStandardItemModel();
29     model->setHorizontalHeaderLabels(
30         QStringList() << "Searching..");
31     addDebugMsg("Fetching Song List...");
32
33     if(currentaction != none)
34     {
35         addDebugMsg("Error: communication is busy Try again later");
36         return model;
37     }
38     else
39     {
40         QString *token = getToken(getSearchResults);
41         qDebug() << token->toAscii();
42         QNetworkRequest request;
43         request.setUrl(QUrl("http://cowbell.grooveshark.com/more.php?getSearchResults"));
44         request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
45         QVariantMap jlist;
46         QVariantMap header;
47         //header.insert("uuid","DEA8E133-2080-F666-4B38-9465187B20A9");
48         header.insert("session",phpSession->toUtf8());
49         header.insert("client","gslite");
50         header.insert("clientRevision","20100412.09");
51         header.insert("token",token->toAscii());
52         jlist.insert("method","getSearchResults");
53         jlist.insertMulti("header",header);
54         QVariantMap param;
55         param.insert("type","Songs");
56         param.insert("query",song);
57         jlist.insertMulti("parameters",param);
58         QJson::Serializer *serializer = new QJson::Serializer();
59         //qDebug() << serializer->serialize(jlist);
60         manager->post(request,serializer->serialize(jlist));
61         currentaction = getSearchResults;
62     }
63
64     return model;
65 }
66 QStandardItemModel* gscom::getArtistModel(QString song)
67 {
68     model = new QStandardItemModel();
69     model->setHorizontalHeaderLabels(
70         QStringList() << "Searching..");
71     addDebugMsg("Fetching Song List...");
72
73     if(currentaction != none)
74     {
75         addDebugMsg("Error: communication is busy Try again later");
76         return model;
77     }
78     else
79     {
80         QString *token = getToken(getSearchResults);
81         qDebug() << token->toAscii();
82         QNetworkRequest request;
83         request.setUrl(QUrl("http://cowbell.grooveshark.com/more.php?getSearchResults"));
84         request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
85         QVariantMap jlist;
86         QVariantMap header;
87         //header.insert("uuid","DEA8E133-2080-F666-4B38-9465187B20A9");
88         header.insert("session",phpSession->toUtf8());
89         header.insert("client","gslite");
90         header.insert("clientRevision","20100412.09");
91         header.insert("token",token->toAscii());
92         jlist.insert("method","getSearchResults");
93         jlist.insertMulti("header",header);
94         QVariantMap param;
95         param.insert("type","Artists");
96         param.insert("query",song.toAscii());
97         jlist.insertMulti("parameters",param);
98         QJson::Serializer *serializer = new QJson::Serializer();
99         //qDebug() << serializer->serialize(jlist);
100         manager->post(request,serializer->serialize(jlist));
101         currentaction = getSearchResults;
102     }
103
104     return model;
105 }
106 QStandardItemModel* gscom::getAlbumModel(QString song)
107 {
108     model = new QStandardItemModel();
109     model->setHorizontalHeaderLabels(
110         QStringList() << "Searching..");
111     addDebugMsg("Fetching Song List...");
112
113     if(currentaction != none)
114     {
115         addDebugMsg("Error: communication is busy Try again later");
116         return model;
117     }
118     else
119     {
120         QString *token = getToken(getSearchResults);
121         qDebug() << token->toAscii();
122         QNetworkRequest request;
123         request.setUrl(QUrl("http://cowbell.grooveshark.com/more.php?getSearchResults"));
124         request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
125         QVariantMap jlist;
126         QVariantMap header;
127         //header.insert("uuid","DEA8E133-2080-F666-4B38-9465187B20A9");
128         header.insert("session",phpSession->toUtf8());
129         header.insert("client","gslite");
130         header.insert("clientRevision","20100412.09");
131         header.insert("token",token->toAscii());
132         jlist.insert("method","getSearchResults");
133         jlist.insertMulti("header",header);
134         QVariantMap param;
135         param.insert("type","Albums");
136         param.insert("query",song.toAscii());
137         jlist.insertMulti("parameters",param);
138         QJson::Serializer *serializer = new QJson::Serializer();
139         //qDebug() << serializer->serialize(jlist);
140         manager->post(request,serializer->serialize(jlist));
141         currentaction = getSearchResults;
142     }
143
144     return model;
145 }
146 void gscom::replyFinished(QNetworkReply *reply)
147 {
148     switch (currentaction)
149     {
150     case getStreamKeyFromSongIDEx:
151         {
152             qDebug() << "tester";
153             QJson::Parser parser;
154             bool ok;
155             QVariantMap result = parser.parse (reply->readAll(), &ok).toMap();
156             if (!ok) {
157               qFatal("An error occurred during parsing");
158               return;
159             }
160             QVariantMap results = result["result"].toMap();
161             this->streamID = results["streamKey"].toString();
162             this->sku = QUrl(QString("http://") + results["ip"].toString() + "/stream.php");
163             qDebug() << sku;
164             currentaction = none;
165             reply->close();
166             emit sKeyFound();
167         }
168         break;
169     case getPHP:
170         {
171             QList<QNetworkCookie> cookieList = cookies->cookiesForUrl(QUrl(QString("http://") + GS_LISTEN));
172             foreach(QNetworkCookie cookie, cookieList)
173             {
174                 if(cookie.name() == "PHPSESSID")
175                 {
176                     phpSession = new QString(cookie.value());
177                     qDebug() << QDateTime::currentDateTime();
178                     qDebug() << cookie.expirationDate();
179                 }
180             }
181             reply->readAll();
182             reply->close();
183             qDebug() << phpSession->toAscii();
184             firstR = false;
185             currentaction = getCommunicationToken;
186             getSessionKey();
187
188         }
189         break;
190     case getCommunicationToken:
191         {
192             bool ok;
193             QJson::Parser parser;
194             QVariantMap result = parser.parse(reply->readAll(),&ok).toMap();
195             if(!ok)
196                 qDebug("Error parsing request");
197             else
198             {
199                 sessionKey = new QString(result["result"].toString());
200                 qDebug() << sessionKey->toAscii();
201             }
202             qDebug() << result;
203             currentaction = none;
204             reply->close();
205             emit finishedSearch();
206         }
207         break;
208     case(getSearchResults):
209         {
210             QJson::Parser parser;
211             bool ok;
212             QVariantMap result = parser.parse (reply->readAll(), &ok).toMap();
213             if (!ok) {
214               qFatal("An error occurred during parsing");
215               return;
216             }
217             if(result.contains("fault"))
218             {
219                 currentaction = getPHP;
220                 manager->get(QNetworkRequest(QUrl(QString("http://") + GS_LISTEN)));
221                 qDebug() << reply->readAll();
222                 qDebug() << result;
223                 reply->close();
224                 return;
225             }
226             model->clear();
227             model->setHorizontalHeaderLabels(
228                 QStringList() << "Song"
229                               << "Artist"
230                               << "Song Id");
231
232
233             foreach (QVariant plugin, result["result"].toList()) {
234               QList<QStandardItem *> items;
235               QVariantMap nestedMap = plugin.toMap();
236               items.append(new QStandardItem(nestedMap["Name"].toString()));
237               items.append(new QStandardItem(nestedMap["ArtistName"].toString()));
238               items.append(new QStandardItem(nestedMap["SongID"].toString()));
239               model->appendRow(items);
240             }
241             currentaction = none;
242             reply->close();
243             emit finishedSearch();
244         }
245         break;
246     default:
247         qDebug() << reply->readAll();
248         reply->close();
249         currentaction=none;
250     }
251
252 }
253 QStandardItemModel* gscom::getModel()
254 {
255     return model;
256 }
257 void gscom::addDebugMsg(QString debug)
258 {
259     QList<QStringList> rows = QList<QStringList>()
260         << (QStringList() << debug);
261     foreach (QStringList row, rows) {
262         QList<QStandardItem *> items;
263         foreach (QString text, row)
264             items.append(new QStandardItem(text));
265         model->appendRow(items);
266     }
267     return;
268 }
269 void gscom::addProgressbar(QString debug)
270 {
271     QList<QStringList> rows = QList<QStringList>()
272         << (QStringList() << debug);
273     foreach (QStringList row, rows) {
274         QList<QStandardItem *> items;
275         foreach (QString text, row)
276         {
277             QLinearGradient gradient(0,0,250,0);
278             gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));
279             gradient.setColorAt(0.9, QColor::fromRgbF(0, 1, 0, 1));
280             gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));
281             QBrush brush(Qt::red);
282             QStandardItem *pBar = new QStandardItem(text);
283             pBar->setBackground(brush);
284             pBar->setSelectable(false);
285             items.append(pBar);
286         }
287         model->appendRow(items);
288     }
289     return;
290 }
291 QString* gscom::getToken(gMETHOD meth)
292 {
293     QCryptographicHash *hasher = new QCryptographicHash(QCryptographicHash::Sha1);
294     //generate six random numbers
295     QString rnum =  QString(qrand() %9 + 48)
296                     + QString(qrand() %9 + 48)
297                     + QString(qrand() %9 + 48)
298                     + QString(qrand() %9 + 48)
299                     + QString(qrand() %9 + 48)
300                     + QString(qrand() %9 + 48);
301     QString *data = new QString();
302     QString *string = new QString();
303     switch(meth)
304     {
305     case(getSearchResults):
306         data->append("getSearchResults");
307         break;
308     case(getStreamKeyFromSongIDEx):
309         data->append("getStreamKeyFromSongIDEx");
310         break;
311     default:
312         data->append("getSearchResults");
313     }
314     string->append(data);
315     string->append(":");
316     string->append(sessionKey->toAscii());
317     string->append(":quitStealinMahShit:");
318     string->append(rnum);
319     hasher->addData(string->toAscii());
320     QString *rs = new QString();
321     rs->append(rnum);
322     rs->append(hasher->result().toHex());
323     return rs;
324 }
325 void gscom::getSong(QString songid)
326 {
327     if(currentaction != none)
328     {
329         return;
330     }
331     else
332     {
333         QString *token = getToken(getStreamKeyFromSongIDEx);
334         qDebug() << token->toAscii();
335         QNetworkRequest request;
336         request.setUrl(QUrl("http://cowbell.grooveshark.com/more.php?getStreamKeyFromSongIdEx"));
337         request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
338         QVariantMap jlist;
339         QVariantMap header;
340         //header.insert("uuid","DEA8E133-2080-F666-4B38-9465187B20A9");
341         header.insert("session",phpSession->toUtf8());
342         header.insert("client","gslite");
343         header.insert("clientRevision","20100412.09");
344         header.insert("token",token->toAscii());
345         jlist.insert("method","getStreamKeyFromSongIDEx");
346         jlist.insertMulti("header",header);
347         QVariantMap param;
348         QVariantMap country;
349         country.insert("CC1","0");
350         country.insert("CC3","0");
351         country.insert("ID","223");
352         country.insert("CC2","0");
353         country.insert("CC4","1073741824");
354         param.insertMulti("country",country);
355         param.insert("mobile",false);
356         param.insert("songID",songid.toAscii());
357         param.insert("prefetch",false);
358         jlist.insertMulti("parameters",param);
359         QJson::Serializer *serializer = new QJson::Serializer();
360         //qDebug() << serializer->serialize(jlist);
361         manager->post(request,serializer->serialize(jlist));
362         currentaction = getStreamKeyFromSongIDEx;
363     }
364     return;
365 }
366
367 void gscom::getSessionKey()
368 {
369     QNetworkRequest request; // = new QNetworkRequest(QUrl("https://cowbell.grooveshark.com/service.php"));
370     request.setUrl(QUrl("https://cowbell.grooveshark.com/service.php"));
371     request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
372     QVariantMap jlist;
373     QVariantMap header;
374     //header.insert("uuid","DEA8E133-2080-F666-4B38-9465187B20A9");
375     //header.insert("session",phpSession->toUtf8());
376     header.insert("client","gslite");
377     header.insert("clientRevision","20100412.09");
378     jlist.insert("method","getCommunicationToken");
379     jlist.insertMulti("header",header);
380     QVariantMap param;
381     QCryptographicHash *hasher = new QCryptographicHash(QCryptographicHash::Md5);
382     hasher->addData(phpSession->toUtf8());
383     param.insert("secretKey",hasher->result().toHex());
384     jlist.insertMulti("parameters",param);
385     QJson::Serializer *serializer = new QJson::Serializer();
386     manager->post(request,serializer->serialize(jlist));
387 }