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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "LastFMPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx"
#include "PlaylistRegistry.hxx"
#include "conf.h"
#include "Song.hxx"
#include "input_stream.h"
#include <glib.h>
#include <assert.h>
#include <string.h>
struct LastfmPlaylist {
struct playlist_provider base;
struct input_stream *is;
struct playlist_provider *xspf;
LastfmPlaylist(input_stream *_is, playlist_provider *_xspf)
:is(_is), xspf(_xspf) {
playlist_provider_init(&base, &lastfm_playlist_plugin);
}
~LastfmPlaylist() {
playlist_plugin_close(xspf);
input_stream_close(is);
}
};
static struct {
char *user;
char *md5;
} lastfm_config;
static bool
lastfm_init(const config_param ¶m)
{
const char *user = param.GetBlockValue("user");
const char *passwd = param.GetBlockValue("password");
if (user == NULL || passwd == NULL) {
g_debug("disabling the last.fm playlist plugin "
"because account is not configured");
return false;
}
lastfm_config.user = g_uri_escape_string(user, NULL, false);
if (strlen(passwd) != 32)
lastfm_config.md5 = g_compute_checksum_for_string(G_CHECKSUM_MD5,
passwd, strlen(passwd));
else
lastfm_config.md5 = g_strdup(passwd);
return true;
}
static void
lastfm_finish(void)
{
g_free(lastfm_config.user);
g_free(lastfm_config.md5);
}
/**
* Simple data fetcher.
* @param url path or url of data to fetch.
* @return data fetched, or NULL on error. Must be freed with g_free.
*/
static char *
lastfm_get(const char *url, Mutex &mutex, Cond &cond)
{
struct input_stream *input_stream;
GError *error = NULL;
char buffer[4096];
size_t length = 0, nbytes;
input_stream = input_stream_open(url, mutex, cond, &error);
if (input_stream == NULL) {
if (error != NULL) {
g_warning("%s", error->message);
g_error_free(error);
}
return NULL;
}
mutex.lock();
input_stream_wait_ready(input_stream);
do {
nbytes = input_stream_read(input_stream, buffer + length,
sizeof(buffer) - length, &error);
if (nbytes == 0) {
if (error != NULL) {
g_warning("%s", error->message);
g_error_free(error);
}
if (input_stream_eof(input_stream))
break;
/* I/O error */
mutex.unlock();
input_stream_close(input_stream);
return NULL;
}
length += nbytes;
} while (length < sizeof(buffer));
mutex.unlock();
input_stream_close(input_stream);
return g_strndup(buffer, length);
}
/**
* Ini-style value fetcher.
* @param response data through which to search.
* @param name name of value to search for.
* @return value for param name in param response or NULL on error. Free with g_free.
*/
static char *
lastfm_find(const char *response, const char *name)
{
size_t name_length = strlen(name);
while (true) {
const char *eol = strchr(response, '\n');
if (eol == NULL)
return NULL;
if (strncmp(response, name, name_length) == 0 &&
response[name_length] == '=') {
response += name_length + 1;
return g_strndup(response, eol - response);
}
response = eol + 1;
}
}
static struct playlist_provider *
lastfm_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{
GError *error = NULL;
char *p, *q, *response, *session;
/* handshake */
p = g_strconcat("http://ws.audioscrobbler.com/radio/handshake.php?"
"version=1.1.1&platform=linux&"
"username=", lastfm_config.user, "&"
"passwordmd5=", lastfm_config.md5, "&"
"debug=0&partner=", NULL);
response = lastfm_get(p, mutex, cond);
g_free(p);
if (response == NULL)
return NULL;
/* extract session id from response */
session = lastfm_find(response, "session");
g_free(response);
if (session == NULL) {
g_warning("last.fm handshake failed");
return NULL;
}
q = g_uri_escape_string(session, NULL, false);
g_free(session);
session = q;
g_debug("session='%s'", session);
/* "adjust" last.fm radio */
if (strlen(uri) > 9) {
char *escaped_uri;
escaped_uri = g_uri_escape_string(uri, NULL, false);
p = g_strconcat("http://ws.audioscrobbler.com/radio/adjust.php?"
"session=", session, "&url=", escaped_uri, "&debug=0",
NULL);
g_free(escaped_uri);
response = lastfm_get(p, mutex, cond);
g_free(response);
g_free(p);
if (response == NULL) {
g_free(session);
return NULL;
}
}
/* open the last.fm playlist */
p = g_strconcat("http://ws.audioscrobbler.com/radio/xspf.php?"
"sk=", session, "&discovery=0&desktop=1.5.1.31879",
NULL);
g_free(session);
const auto is = input_stream_open(p, mutex, cond, &error);
g_free(p);
if (is == nullptr) {
if (error != NULL) {
g_warning("Failed to load XSPF playlist: %s",
error->message);
g_error_free(error);
} else
g_warning("Failed to load XSPF playlist");
return NULL;
}
mutex.lock();
input_stream_wait_ready(is);
/* last.fm does not send a MIME type, we have to fake it here
:-( */
input_stream_override_mime_type(is, "application/xspf+xml");
mutex.unlock();
/* parse the XSPF playlist */
const auto xspf = playlist_list_open_stream(is, nullptr);
if (xspf == nullptr) {
input_stream_close(is);
g_warning("Failed to parse XSPF playlist");
return NULL;
}
/* create the playlist object */
const auto playlist = new LastfmPlaylist(is, xspf);
return &playlist->base;
}
static void
lastfm_close(struct playlist_provider *_playlist)
{
LastfmPlaylist *playlist = (LastfmPlaylist *)_playlist;
delete playlist;
}
static Song *
lastfm_read(struct playlist_provider *_playlist)
{
LastfmPlaylist *playlist = (LastfmPlaylist *)_playlist;
return playlist_plugin_read(playlist->xspf);
}
static const char *const lastfm_schemes[] = {
"lastfm",
NULL
};
const struct playlist_plugin lastfm_playlist_plugin = {
"lastfm",
lastfm_init,
lastfm_finish,
lastfm_open_uri,
nullptr,
lastfm_close,
lastfm_read,
lastfm_schemes,
nullptr,
nullptr,
};
|