-
Notifications
You must be signed in to change notification settings - Fork 1
/
soundcloudapi.cpp
264 lines (203 loc) · 9.33 KB
/
soundcloudapi.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# include "soundcloudapi.h"
# include <QJsonDocument>
# include <QJsonArray>
# include <QJsonObject>
# include <QDesktopServices>
# define CLIENT_ID "23896355641020e5f7e2292321372280"
# define CLIENT_SECRET "0a49f062eaaa04561cc4f74d42da6739"
# define OAUTH_LOCAL_SERVER_PORT 9999
// --- public methods
SoundCloudApi& SoundCloudApi::getInstance() {
static SoundCloudApi instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
bool SoundCloudApi::isAuthenticated() {
return oauthAuthenticator->linked();
}
void SoundCloudApi::requestStreamUrl(int songId) {
static QString urlTemplate("http://api.sndcdn.com/i1/tracks/%1/streams?client_id=" CLIENT_ID);
QNetworkReply* reply = networkManager->get(QNetworkRequest(QUrl(urlTemplate.arg(songId))));
waitingStreamUrlReplies.insert(reply, songId);
}
void SoundCloudApi::requestLikes() {
static QString urlTemplate("https://api.soundcloud.com/me/favorites.json?oauth_token=%1");
QNetworkReply* reply = networkManager->get(QNetworkRequest(QUrl(urlTemplate.arg(oauthAuthenticator->token()))));
waitingLikeReplies.append(reply);
}
void SoundCloudApi::requestArtwork(int songId, QUrl artworkUrl) {
QNetworkReply* reply = networkManager->get(QNetworkRequest(artworkUrl));
waitingArtworkReplies.insert(reply, songId);
}
void SoundCloudApi::requestPlaylists() {
static QString urlTemplate("https://api.soundcloud.com/me/playlists.json?oauth_token=%1");
QNetworkReply* reply = networkManager->get(QNetworkRequest(QUrl(urlTemplate.arg(oauthAuthenticator->token()))));
waitingPlaylistReplies.append(reply);
}
void SoundCloudApi::requestActivities() {
static QString urlTemplate("https://api.soundcloud.com/me/activities.json?oauth_token=%1");
QNetworkReply* reply = networkManager->get(QNetworkRequest(QUrl(urlTemplate.arg(oauthAuthenticator->token()))));
waitingActivityReplies.append(reply);
}
void SoundCloudApi::requestAuthentification() {
oauthAuthenticator->setGrantFlow(O2::GrantFlowAuthorizationCode);
oauthAuthenticator->link();
}
// --- private methods
SoundCloudApi::SoundCloudApi() {
networkManager = new QNetworkAccessManager(this);
oauthAuthenticator = new O2(this);
oauthRequestor = new O2Requestor(networkManager, oauthAuthenticator, this);
oauthAuthenticator->setClientId(CLIENT_ID);
oauthAuthenticator->setClientSecret(CLIENT_SECRET);
oauthAuthenticator->setScope("non-expiring");
oauthAuthenticator->setLocalPort(OAUTH_LOCAL_SERVER_PORT);
oauthAuthenticator->setRequestUrl("https://soundcloud.com/connect");
oauthAuthenticator->setTokenUrl("https://api.soundcloud.com/oauth2/token");
// sum connecthunz
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleFinishedRequest(QNetworkReply*)));
connect(oauthAuthenticator, SIGNAL(linkedChanged()), this, SLOT(onOauthLinkedChanged()));
connect(oauthAuthenticator, SIGNAL(linkingFailed()), this, SLOT(onOauthLinkingFailed()));
connect(oauthAuthenticator, SIGNAL(linkingSucceeded()), this, SLOT(onOauthLinkingSucceeded()));
connect(oauthAuthenticator, SIGNAL(openBrowser(QUrl)), this, SLOT(onOauthOpenBrowser(QUrl)));
}
Sound SoundCloudApi::parseSoundJson(const QJsonObject &soundJson) {
Sound sound;
sound.setId(soundJson["id"].toInt());
sound.setTitle(soundJson["title"].toString());
sound.setUser(soundJson["user"].toObject()["username"].toString());
sound.setArtworkUrl(soundJson["artwork_url"].toString());
return sound;
}
Playlist SoundCloudApi::parsePlaylistJson(const QJsonObject &playlistJson, QList<Sound>& containedSounds) {
// read all tracks from this playlist
QJsonArray playlistSoundsJson = playlistJson["tracks"].toArray();
QList<int> playlistSoundIds;
for (QJsonArray::const_iterator psit = playlistSoundsJson.begin(); psit != playlistSoundsJson.end(); ++psit) {
QJsonObject soundJson = (*psit).toObject();
containedSounds.append(parseSoundJson(soundJson));
playlistSoundIds.append(containedSounds.last().getId());
}
// provide some information about this playlist
Playlist playlist(playlistSoundIds);
playlist.setId(playlistJson["id"].toInt());
playlist.setUser(playlistJson["user"].toObject()["username"].toString());
playlist.setTitle(playlistJson["title"].toString());
playlist.setArtworkUrl(playlistJson["artwork_url"].toString());
return playlist;
}
// --- private slots
void SoundCloudApi::handleFinishedRequest(QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NoError) {
if (waitingStreamUrlReplies.contains(reply)) {
handleStreamUrlReply(reply);
} else if (waitingLikeReplies.contains(reply)) {
handleLikeReply(reply);
} else if (waitingArtworkReplies.contains(reply)) {
handleArtworkReply(reply);
} else if (waitingPlaylistReplies.contains(reply)) {
handlePlaylistReply(reply);
} else if (waitingActivityReplies.contains(reply)) {
handleActivityReply(reply);
}
} else {
qDebug("Network error %i | %s", (int)reply->error(), reply->url().toString().toStdString().c_str());
if (waitingStreamUrlReplies.contains(reply)) {
waitingStreamUrlReplies.remove(reply);
qDebug() << "bad stream request";
// emit badStreamUrlRequest?!
} else if (waitingLikeReplies.contains(reply)){
waitingLikeReplies.removeOne(reply);
qDebug() << "bad like request";
// emit badLikeRequest?!
} else if (waitingPlaylistReplies.contains(reply)) {
waitingPlaylistReplies.removeOne(reply);
qDebug() << "bad playlist request";
// emit badPlaylistRequest?!
} else if (waitingActivityReplies.contains(reply)) {
waitingActivityReplies.removeOne(reply);
qDebug() << "bad activity request";
}
}
}
void SoundCloudApi::handleStreamUrlReply(QNetworkReply *reply) {
QString replyString = QString(reply->readAll());
QJsonDocument jsonDocument = QJsonDocument::fromJson(replyString.toUtf8());
QJsonValue streamUrl = jsonDocument.object()["http_mp3_128_url"];
if (streamUrl.isString()) {
emit streamUrlReceived(waitingStreamUrlReplies.take(reply), QUrl(streamUrl.toString()));
}
}
void SoundCloudApi::handleLikeReply(QNetworkReply *reply) {
QString replyString = QString(reply->readAll());
QJsonDocument jsonDocument = QJsonDocument::fromJson(replyString.toUtf8());
QJsonArray jsonArray = jsonDocument.array();
QJsonObject songObject;
QList<Sound> likes;
for (QJsonArray::const_iterator it = jsonArray.begin(); it != jsonArray.end(); ++it) {
songObject = (*it).toObject();
likes.append(parseSoundJson(songObject));
}
waitingLikeReplies.removeOne(reply);
emit likesReceived(likes);
}
void SoundCloudApi::handleArtworkReply(QNetworkReply *reply) {
QPixmap p;
QByteArray picBytes = reply->readAll();
int id = waitingArtworkReplies.take(reply);
if (p.loadFromData(picBytes, "JPG")) {
emit artworkReceived(id, p);
}
}
void SoundCloudApi::handlePlaylistReply(QNetworkReply *reply) {
QString replyString = QString(reply->readAll());
QJsonDocument jsonDocument = QJsonDocument::fromJson(replyString.toUtf8());
QJsonArray jsonArray = jsonDocument.array();
QList<Sound> sounds;
QList<Playlist> playlists;
for (QJsonArray::const_iterator it = jsonArray.begin(); it != jsonArray.end(); ++it) {
QJsonObject playlistJson = (*it).toObject();
playlists.append(parsePlaylistJson(playlistJson, sounds));
}
waitingPlaylistReplies.removeOne(reply);
emit playlistsReceived(sounds, playlists);
}
void SoundCloudApi::handleActivityReply(QNetworkReply *reply) {
QString replyString = QString(reply->readAll());
QJsonObject jsonObject = QJsonDocument::fromJson(replyString.toUtf8()).object();
QJsonArray jsonArray = jsonObject["collection"].toArray();
QList<Sound> sounds;
QList<Playlist> playlists;
QList< QPair<int,QString> > idsAndTypes;
for (QJsonArray::const_iterator it = jsonArray.begin(); it != jsonArray.end(); ++it) {
QJsonObject listElement = (*it).toObject();
if (listElement["type"].toString() == QString("track")) {
sounds.append(parseSoundJson(listElement["origin"].toObject()));
idsAndTypes.append(QPair<int,QString>(sounds.last().getId(), "track"));
} else if (listElement["type"].toString() == QString("playlist")) {
playlists.append(parsePlaylistJson(listElement["origin"].toObject(), sounds));
idsAndTypes.append(QPair<int,QString>(playlists.last().getId(), "playlist"));
}
}
waitingActivityReplies.removeOne(reply);
emit activitiesReceived(idsAndTypes, sounds, playlists);
}
void SoundCloudApi::onOauthLinkedChanged() {
if (oauthAuthenticator->linked()) {
emit authenticated();
} else {
emit notAuthenticated();
}
}
void SoundCloudApi::onOauthLinkingFailed() {
// Login has failed
emit notAuthenticated();
}
void SoundCloudApi::onOauthLinkingSucceeded() {
// Login has succeeded
qDebug() << "Gained auth like a boss!";
emit authenticated();
}
void SoundCloudApi::onOauthOpenBrowser(const QUrl url) {
QDesktopServices::openUrl(url);
}