-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotifyapi.cpp
213 lines (183 loc) · 6.83 KB
/
spotifyapi.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
#include "spotifyapi.h"
#include <QOAuthHttpServerReplyHandler>
#include <QNetworkReply>
#include <QDesktopServices>
#include <QUrl>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QProcessEnvironment>
constexpr auto AUTHORIZATION_URL = "https://accounts.spotify.com/authorize";
constexpr auto ACCESS_TOKEN_URL = "https://accounts.spotify.com/api/token";
constexpr auto CLIENT_IDENTIFIER = "b90c5291239b412a86d07ac6bd8190af";
constexpr auto SCOPE = "user-read-private user-read-playback-state user-modify-playback-state";
constexpr int TIMER_INTERVAL_MS = 1000;
constexpr int SERVER_PORT = 3000;
// ctor
SpotifyAPI::SpotifyAPI(QObject *parent)
: QObject{parent}, timer{new QTimer(this)}
{
const QString sharedKey = QProcessEnvironment::systemEnvironment().value("SPOTIFY_CLIENT_SECRET", "");
setupOAuth2(sharedKey);
setupConnections();
SpotifyAPI::authenticate();
}
/////////////////////////////////////
// Public Slots
/////////////////////////////////////
// slot hit upon authentication completion
void SpotifyAPI::authenticate()
{
m_oauth2->grant();
}
// slot hit to request playback toggle
void SpotifyAPI::togglePlayback()
{
volatile bool isPlaying = false;
QUrl isPlayingUrl("https://api.spotify.com/v1/me/player/");
auto reply = m_oauth2->get(isPlayingUrl);
connect(reply, &QNetworkReply::finished, [this, reply, &isPlaying]() {
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Network error" << reply->errorString();
return;
}
auto jsonData = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
if (doc.isNull())
{
qDebug() << "TP: No JSON returned by Spotify API";
//startInitialPlayback();
return;
}
QJsonObject obj = doc.object();
isPlaying = obj["is_playing"].toBool();
qDebug() << "Initial isPlaying is " << isPlaying;
QUrl toggleUrl = QUrl("");
if(isPlaying)
{
qDebug() << "Attempting to pause";
sendPlaybackCommand(QUrl("https://api.spotify.com/v1/me/player/pause"), false);
}
else
{
qDebug() << "Attempting to play";
sendPlaybackCommand(QUrl("https://api.spotify.com/v1/me/player/play"), true);
}
reply->deleteLater();
});
}
/////////////////////////////////////
// Private Slots
/////////////////////////////////////
// slot for authentication. kind of a duplicate
void SpotifyAPI::onGranted()
{
emit authenticated();
timer->start(TIMER_INTERVAL_MS);
qDebug() << m_oauth2->token();
}
// OAuth Error slot
void SpotifyAPI::onError(const QString & error1, const QString & error2, const QUrl & errorUrl)
{
qDebug() << "OAuth Error";
exit(1);
}
/////////////////////////////////////
// Helpers
/////////////////////////////////////
// fxn to pull current track
void SpotifyAPI::getCurrentPlayingTrack()
{
QUrl url("https://api.spotify.com/v1/me/player/currently-playing");
auto reply = m_oauth2->get(url);
connect(reply, &QNetworkReply::finished, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Network error" << reply->errorString();
return;
}
auto jsonData = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
if (doc.isNull())
{
qDebug() << "No JSON returned by Spotify API";
return;
}
QJsonObject obj = doc.object();
if (obj.isEmpty() || !obj.contains("item"))
{
qDebug() << "Empty or invalid item object";
return;
}
QJsonObject itemObject = obj["item"].toObject();
if(!itemObject.contains("artists") || itemObject["artists"].toArray().isEmpty())
{
qDebug() << "Response lacks artist information";
return;
}
if(!itemObject.contains("album"))
{
qDebug() << "Response lacks album information";
return;
}
QString trackName = itemObject["name"].toString();
QJsonArray artistsArray = itemObject["artists"].toArray();
QJsonObject artistObject = artistsArray.first().toObject();
QString artistName = artistObject["name"].toString();
QString albumName = itemObject["album"].toObject()["name"].toString();
emit trackInfoReceived(trackName, artistName, albumName);
QString albumCoverUrl = itemObject["album"].toObject()["images"].toArray()[0].toObject()["url"].toString();
emit albumCoverReceived(albumCoverUrl);
reply->deleteLater();
});
}
// fxn to set up class connnections
void SpotifyAPI::setupConnections()
{
// OAuth2 Signals
connect(m_oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, this, &QDesktopServices::openUrl);
connect(m_oauth2, &QOAuth2AuthorizationCodeFlow::granted, this, &SpotifyAPI::onGranted);
connect(m_oauth2, &QOAuth2AuthorizationCodeFlow::error, this, &SpotifyAPI::onError);
// Timer signal to update track
connect(timer, &QTimer::timeout, this, &SpotifyAPI::getCurrentPlayingTrack);
}
// fxn to set up OAuth connection w/ Spotify API
void SpotifyAPI::setupOAuth2(const QString& sharedKey)
{
m_oauth2 = new QOAuth2AuthorizationCodeFlow(this);
m_oauth2->setAuthorizationUrl(QUrl(AUTHORIZATION_URL));
m_oauth2->setAccessTokenUrl(QUrl(ACCESS_TOKEN_URL));
m_oauth2->setClientIdentifier(CLIENT_IDENTIFIER);
m_oauth2->setClientIdentifierSharedKey(sharedKey);
m_oauth2->setScope(SCOPE);
auto replyHandler = new QOAuthHttpServerReplyHandler(3000);
m_oauth2->setReplyHandler(replyHandler);
}
//TODO: Either function or API endpoint a little glitchy. Debug and fix
// fxn to wrap playback
void SpotifyAPI::sendPlaybackCommand(const QUrl& commandUrl, bool wasAttemptingToPlay)
{
auto putReply = m_oauth2->put(commandUrl);
connect(putReply, &QNetworkReply::finished, [this, putReply, wasAttemptingToPlay]() {
if (putReply->error() != QNetworkReply::NoError)
{
qDebug() << "Network error while attempting playback command:" << putReply->errorString();
if (wasAttemptingToPlay)
{
qDebug() << "Error while attempting to play. Trying to pause instead.";
sendPlaybackCommand(QUrl("https://api.spotify.com/v1/me/player/pause"), false);
}
else
{
qDebug() << "Error while attempting to pause. Trying to play instead.";
sendPlaybackCommand(QUrl("https://api.spotify.com/v1/me/player/play"), true);
}
putReply->deleteLater();
return;
}
emit musicToggled(wasAttemptingToPlay);
SpotifyAPI::getCurrentPlayingTrack();
putReply->deleteLater();
});
}