forked from hrkfdn/mpdas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioscrobbler.cpp
212 lines (173 loc) · 5.57 KB
/
audioscrobbler.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 "mpdas.h"
#include "http.hpp"
#define ROOTURL "http://ws.audioscrobbler.com/2.0/"
#define APIKEY "a0ed2629d3d28606f67d7214c916788d"
#define SECRET "295f31c5d28215215b1503fb0327cc01"
CAudioScrobbler* AudioScrobbler = 0;
CAudioScrobbler::CAudioScrobbler()
{
_failcount = 0;
_authed = false;
}
std::string
CAudioScrobbler::CreateSignedMessage(std::map<std::string, std::string> params)
{
std::ostringstream msg, sig;
// Add the Last.fm method signature: http://www.last.fm/api/authspec#8
// std::map keeps the elements sorted by key, which makes this simple
for(auto param : params)
sig << param.first << param.second;
sig << SECRET;
params["api_sig"] = md5sum(sig.str());
// Create a message in application/x-www-form-urlencoded format
for(auto param : params)
msg << "&" << param.first << "=" << conn.urlencode(param.second);
return msg.str();
}
std::string
CAudioScrobbler::CreateScrobbleMessage(const CacheEntry& entry)
{
std::map<std::string, std::string> params = entry.getSong().tags();
params["method"] = "track.scrobble";
params["timestamp"] = std::to_string(entry.getStartTime());
params["api_key"] = APIKEY;
params["sk"] = _sessionid;
return CreateSignedMessage(params);
}
void
CAudioScrobbler::Failure()
{
_failcount += 1;
if(_failcount >= 3) {
eprintf("%s", "Re-Handshaking!");
_failcount = 0;
Handshake();
}
}
bool
CAudioScrobbler::CheckFailure(std::string response)
{
bool retval = false;
size_t start, end;
start = conn.response().find("<error code=\"")+13;
end = conn.response().find(">", start)-1;
std::string errorcode = conn.response().substr(start, end-start);
int code = strtol(errorcode.c_str(), 0, 10);
eprintf("%s%i", "Code: ", code);
switch(code) {
case 3:
eprintf("Invalid Method. This should not happen.");
retval = true;
break;
case 4:
eprintf("Authentication failed. Please check your login data.");
exit(EXIT_FAILURE);
case 9:
eprintf("Invalid session key. Re-authenticating.");
retval = true;
_failcount = 3;
break;
case 10:
eprintf("Invalid API-Key. Let's bugger off.");
exit(EXIT_FAILURE);
case 16:
eprintf("The service is temporarily unavailable, we will try again later..");
retval = true;
break;
case 26:
eprintf("Uh oh. Suspended API key - Access for your account has been suspended, please contact Last.fm");
exit(EXIT_FAILURE);
}
return retval;
}
bool
CAudioScrobbler::Scrobble(const CacheEntry& entry)
{
bool retval = false;
if(!_authed) {
eprintf("Handshake hasn't been done yet.");
Handshake();
return retval;
}
iprintf("Scrobbling: %s - %s", entry.getSong()["artist"].c_str(), entry.getSong()["track"].c_str());
conn.post(ROOTURL, CreateScrobbleMessage(entry));
if(conn.response().find("<lfm status=\"ok\">") != std::string::npos) {
iprintf("%s", "Scrobbled successfully.");
retval = true;
}
else if(conn.response().find("<lfm status=\"failed\">") != std::string::npos) {
eprintf("%s%s", "Last.fm returned an error while scrobbling:\n", conn.response().c_str());
if(CheckFailure(conn.response()))
Failure();
}
return retval;
}
bool
CAudioScrobbler::LoveTrack(const Song& song)
{
bool retval = false;
std::map<std::string, std::string> params;
params["artist"] = song["artist"];
params["track"] = song["track"];
params["method"] = "track.love";
params["api_key"] = APIKEY;
params["sk"] = _sessionid;
conn.post(ROOTURL, CreateSignedMessage(params));
if(conn.response().find("<lfm status=\"ok\">") != std::string::npos) {
iprintf("%s", "Loved track successfully.");
retval = true;
}
else if(conn.response().find("<lfm status=\"failed\">") != std::string::npos) {
eprintf("%s%s", "Last.fm returned an error while loving the currently playing track:\n", conn.response().c_str());
if(CheckFailure(conn.response()))
Failure();
}
return retval;
}
bool
CAudioScrobbler::SendNowPlaying(const Song& song)
{
bool retval = false;
std::map<std::string, std::string> params = song.tags();
params["method"] = "track.updateNowPlaying";
params["api_key"] = APIKEY;
params["sk"] = _sessionid;
conn.post(ROOTURL, CreateSignedMessage(params));
if(conn.response().find("<lfm status=\"ok\">") != std::string::npos) {
iprintf("%s", "Updated \"Now Playing\" status successfully.");
retval = true;
}
else if(conn.response().find("<lfm status=\"failed\">") != std::string::npos) {
eprintf("%s%s", "Last.fm returned an error while updating the currently playing track:\n", conn.response().c_str());
if(CheckFailure(conn.response()))
Failure();
}
return retval;
}
// This method uses the DEPRECATED authToken parameter
void
CAudioScrobbler::Handshake()
{
std::map<std::string, std::string> params;
std::string username="";
for(unsigned int i = 0; i < Config->getLUsername().length(); i++) {
username.append(1, tolower(Config->getLUsername().c_str()[i]));
}
params["method"] = "auth.getMobileSession";
params["username"] = Config->getLUsername();
params["authToken"] = md5sum(username + Config->getLPassword());
params["api_key"] = APIKEY;
conn.post(ROOTURL, CreateSignedMessage(params));
if(conn.response().find("<lfm status=\"ok\">") != std::string::npos) {
size_t start, end;
start = conn.response().find("<key>") + 5;
end = conn.response().find("</key>");
_sessionid = conn.response().substr(start, end-start);
iprintf("%s%s", "Last.fm handshake successful. SessionID: ", _sessionid.c_str());
_authed = true;
}
else if(conn.response().find("<lfm status=\"failed\">") != std::string::npos) {
CheckFailure(conn.response());
exit(EXIT_FAILURE);
}
}