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