forked from hrkfdn/mpdas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioscrobbler.cpp
232 lines (200 loc) · 5.31 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "mpdas.h"
#define HOST "http://post.audioscrobbler.com"
#define PVERSION "1.2.1"
#define CLIENT "mp5"
#define CVERSION "0.2.5"
CAudioScrobbler* AudioScrobbler = 0;
#define CLEANUP() _response.clear()
size_t
writecb(void* ptr, size_t size, size_t nmemb, void *stream)
{
AudioScrobbler->ReportResponse((char*)ptr, size*nmemb);
}
CAudioScrobbler::CAudioScrobbler()
{
_ratingpipe = 0;
_love = false;
_failcount = 0;
_authed = false;
_response = "";
_handle = curl_easy_init();
if(!_handle) {
eprintf("%s", "Could not initialize CURL.");
exit(EXIT_FAILURE);
}
InitPipe();
}
void
CAudioScrobbler::InitPipe()
{
umask(0666);
if(mkfifo("/tmp/mpdaspipe", 0666) != 0 && errno != EEXIST)
eprintf("Could not create the rating pipe. (%s)", strerror(errno));
else {
chmod("/tmp/mpdaspipe", 0666); // somehow this didnt suffice in the mkfifo() call
_ratingpipe = open("/tmp/mpdaspipe", O_RDONLY | O_NDELAY);
if(!_ratingpipe)
eprintf("Could not open the rating pipe. (%s)", strerror(errno));
}
}
void
CAudioScrobbler::OpenURL(const std::string& url, const char* postfields = 0, const char* errbuf = 0)
{
curl_easy_setopt(_handle, CURLOPT_DNS_CACHE_TIMEOUT, 0);
curl_easy_setopt(_handle, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(_handle, CURLOPT_WRITEFUNCTION, writecb);
if(postfields) {
curl_easy_setopt(_handle, CURLOPT_POST, 1);
curl_easy_setopt(_handle, CURLOPT_POSTFIELDS, postfields);
}
else
curl_easy_setopt(_handle, CURLOPT_POST, 0);
if(errbuf)
curl_easy_setopt(_handle, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(_handle, CURLOPT_URL, url.c_str());
curl_easy_perform(_handle);
}
void
CAudioScrobbler::ReportResponse(const char* buf, const size_t size)
{
_response.append(buf);
}
const std::string
CAudioScrobbler::CreateScrobbleMessage(const int index, const centry_t* entry)
{
std::stringstream msg;
msg << "&a[" << index << "]=" << entry->artist;
msg << "&t[" << index << "]=" << entry->title;
msg << "&i[" << index << "]=" << entry->starttime;
msg << "&o[" << index << "]=P";
msg << "&r[" << index << "]=";
if(_love)
msg << "L";
msg << "&l[" << index << "]=" << entry->time;
msg << "&b[" << index << "]=";
if(entry->album)
msg << entry->album;
msg << "&n[" << index << "]=&m[" << index << "]=";
return msg.str();
}
void
CAudioScrobbler::Failure()
{
_failcount += 1;
if(_failcount >= 3) {
eprintf("%s", "Re-Handshaking!");
_failcount = 0;
Handshake();
}
}
const bool
CAudioScrobbler::CheckFailure(const std::string& response)
{
bool retval = false;
if(_response.find("BADSESSION")) {
eprintf("%s", "Bad session ID, re-handshaking!");
Handshake();
retval = false;
}
else if(_response.find("FAILED"))
retval = true;
else if(_response.find("OK"))
retval = false;
return retval;
}
void
CAudioScrobbler::GetLove()
{
if(_ratingpipe == 0)
return;
char buf[80];
int numread = 0;
memset(buf, 0, sizeof(buf));
numread = read(_ratingpipe, &buf, sizeof(buf));
if(numread > 0) {
if(strstr(buf, "L")) {
iprintf("Track will be scrobbled with \"love\" attribute.");
_love = true;
}
else if(strstr(buf, "C")) {
iprintf("Track will not be scrobbled with \"love\" attribute.");
_love = false;
}
}
}
const bool
CAudioScrobbler::Scrobble(const centry_t* entry)
{
bool retval = false;
if(!_authed) {
eprintf("Handshake hasn't been done yet.");
Handshake();
return retval;
}
std::ostringstream post;
iprintf("Scrobbling: %s - %s", entry->artist, entry->title);
post << "s=" << _sessionid;
post << CreateScrobbleMessage(0, entry);
OpenURL(_scroburl, post.str().c_str());
if(_response.find("OK") == 0)
retval = true;
else {
if(CheckFailure(_response))
Failure();
}
CLEANUP();
_love = false;
return retval;
}
const bool
CAudioScrobbler::SendNowPlaying(const mpd_Song* song)
{
bool retval = false;
if(!song || !song->artist || !song->title) return retval;
_love = false;
char* artist = curl_easy_escape(_handle, song->artist, 0);
char* title = curl_easy_escape(_handle, song->title, 0);
char* album = 0;
if(song->album)
album = curl_easy_escape(_handle, song->album, 0);
std::ostringstream post;
post << "s=" << _sessionid << "&a=" << artist << "&t=" << title << "&b=";
if(album)
post << album;
post << "&l=" << song->time << "&n=&m=";
OpenURL(_npurl, post.str().c_str());
if(_response.find("OK") == 0)
retval = true;
else {
if(CheckFailure(_response))
Failure();
}
CLEANUP();
return retval;
}
void
CAudioScrobbler::Handshake()
{
time_t timestamp = time(NULL);
std::string authtoken(md5sum((char*)"%s%i", Config->getLPassword().c_str(), timestamp));
std::ostringstream query;
query << HOST << "/?hs=true&p=" << PVERSION << "&c=" << CLIENT << "&v=" << CVERSION << "&u=" << Config->getLUsername() << "&t=" << timestamp << "&a=" << authtoken;
OpenURL(query.str());
if(_response.find("OK") == 0) {
iprintf("%s", "AudioScrobbler handshake successful.");
_authed = true;
_sessionid = _response.substr(3, 32);
int found = _response.find("\n", 36);
_npurl = _response.substr(36, found-36);
_scroburl = _response.substr(found+1, _response.length()-found-2);
}
else if(_response.find("BADAUTH") == 0) {
eprintf("%s", "Bad username/password.");
exit(EXIT_FAILURE);
}
else if(_response.find("BADTIME") == 0) {
eprintf("%s", "Your computer time is not accurate enough to generate a session id.");
exit(EXIT_FAILURE);
}
CLEANUP();
}