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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*
* 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 "SoundCloudPlaylistPlugin.hxx"
#include "MemoryPlaylistProvider.hxx"
#include "conf.h"
#include "input_stream.h"
#include "song.h"
#include "tag.h"
#include <glib.h>
#include <yajl/yajl_parse.h>
#include <string.h>
static struct {
char *apikey;
} soundcloud_config;
static bool
soundcloud_init(const struct config_param *param)
{
soundcloud_config.apikey =
config_dup_block_string(param, "apikey", NULL);
if (soundcloud_config.apikey == NULL) {
g_debug("disabling the soundcloud playlist plugin "
"because API key is not set");
return false;
}
return true;
}
static void
soundcloud_finish(void)
{
g_free(soundcloud_config.apikey);
}
/**
* Construct a full soundcloud resolver URL from the given fragment.
* @param uri uri of a soundcloud page (or just the path)
* @return Constructed URL. Must be freed with g_free.
*/
static char *
soundcloud_resolve(const char* uri) {
char *u, *ru;
if (g_str_has_prefix(uri, "http://")) {
u = g_strdup(uri);
} else if (g_str_has_prefix(uri, "soundcloud.com")) {
u = g_strconcat("http://", uri, NULL);
} else {
/* assume it's just a path on soundcloud.com */
u = g_strconcat("http://soundcloud.com/", uri, NULL);
}
ru = g_strconcat("http://api.soundcloud.com/resolve.json?url=",
u, "&client_id=", soundcloud_config.apikey, NULL);
g_free(u);
return ru;
}
/* YAJL parser for track data from both /tracks/ and /playlists/ JSON */
enum key {
Duration,
Title,
Stream_URL,
Other,
};
const char* key_str[] = {
"duration",
"title",
"stream_url",
NULL,
};
struct parse_data {
int key;
char* stream_url;
long duration;
char* title;
int got_url; /* nesting level of last stream_url */
std::forward_list<SongPointer> songs;
};
static int handle_integer(void *ctx,
long
#ifndef HAVE_YAJL1
long
#endif
intval)
{
struct parse_data *data = (struct parse_data *) ctx;
switch (data->key) {
case Duration:
data->duration = intval;
break;
default:
break;
}
return 1;
}
static int handle_string(void *ctx, const unsigned char* stringval,
#ifdef HAVE_YAJL1
unsigned int
#else
size_t
#endif
stringlen)
{
struct parse_data *data = (struct parse_data *) ctx;
const char *s = (const char *) stringval;
switch (data->key) {
case Title:
if (data->title != NULL)
g_free(data->title);
data->title = g_strndup(s, stringlen);
break;
case Stream_URL:
if (data->stream_url != NULL)
g_free(data->stream_url);
data->stream_url = g_strndup(s, stringlen);
data->got_url = 1;
break;
default:
break;
}
return 1;
}
static int handle_mapkey(void *ctx, const unsigned char* stringval,
#ifdef HAVE_YAJL1
unsigned int
#else
size_t
#endif
stringlen)
{
struct parse_data *data = (struct parse_data *) ctx;
int i;
data->key = Other;
for (i = 0; i < Other; ++i) {
if (strncmp((const char *)stringval, key_str[i], stringlen) == 0) {
data->key = i;
break;
}
}
return 1;
}
static int handle_start_map(void *ctx)
{
struct parse_data *data = (struct parse_data *) ctx;
if (data->got_url > 0)
data->got_url++;
return 1;
}
static int handle_end_map(void *ctx)
{
struct parse_data *data = (struct parse_data *) ctx;
if (data->got_url > 1) {
data->got_url--;
return 1;
}
if (data->got_url == 0)
return 1;
/* got_url == 1, track finished, make it into a song */
data->got_url = 0;
struct song *s;
struct tag *t;
char *u;
u = g_strconcat(data->stream_url, "?client_id=", soundcloud_config.apikey, NULL);
s = song_remote_new(u);
g_free(u);
t = tag_new();
t->time = data->duration / 1000;
if (data->title != NULL)
tag_add_item(t, TAG_NAME, data->title);
s->tag = t;
data->songs.emplace_front(s);
return 1;
}
static yajl_callbacks parse_callbacks = {
NULL,
NULL,
handle_integer,
NULL,
NULL,
handle_string,
handle_start_map,
handle_mapkey,
handle_end_map,
NULL,
NULL,
};
/**
* Read JSON data and parse it using the given YAJL parser.
* @param url URL of the JSON data.
* @param hand YAJL parser handle.
* @return -1 on error, 0 on success.
*/
static int
soundcloud_parse_json(const char *url, yajl_handle hand,
Mutex &mutex, Cond &cond)
{
struct input_stream *input_stream;
GError *error = NULL;
char buffer[4096];
unsigned char *ubuffer = (unsigned char *)buffer;
size_t 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 -1;
}
mutex.lock();
input_stream_wait_ready(input_stream);
yajl_status stat;
int done = 0;
while (!done) {
nbytes = input_stream_read(input_stream, buffer, sizeof(buffer), &error);
if (nbytes == 0) {
if (error != NULL) {
g_warning("%s", error->message);
g_error_free(error);
}
if (input_stream_eof(input_stream)) {
done = true;
} else {
mutex.unlock();
input_stream_close(input_stream);
return -1;
}
}
if (done) {
#ifdef HAVE_YAJL1
stat = yajl_parse_complete(hand);
#else
stat = yajl_complete_parse(hand);
#endif
} else
stat = yajl_parse(hand, ubuffer, nbytes);
if (stat != yajl_status_ok
#ifdef HAVE_YAJL1
&& stat != yajl_status_insufficient_data
#endif
)
{
unsigned char *str = yajl_get_error(hand, 1, ubuffer, nbytes);
g_warning("%s", str);
yajl_free_error(hand, str);
break;
}
}
mutex.unlock();
input_stream_close(input_stream);
return 0;
}
/**
* Parse a soundcloud:// URL and create a playlist.
* @param uri A soundcloud URL. Accepted forms:
* soundcloud://track/<track-id>
* soundcloud://playlist/<playlist-id>
* soundcloud://url/<url or path of soundcloud page>
*/
static struct playlist_provider *
soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{
char *s, *p;
char *scheme, *arg, *rest;
s = g_strdup(uri);
scheme = s;
for (p = s; *p; p++) {
if (*p == ':' && *(p+1) == '/' && *(p+2) == '/') {
*p = 0;
p += 3;
break;
}
}
arg = p;
for (; *p; p++) {
if (*p == '/') {
*p = 0;
p++;
break;
}
}
rest = p;
if (strcmp(scheme, "soundcloud") != 0) {
g_warning("incompatible scheme for soundcloud plugin: %s", scheme);
g_free(s);
return NULL;
}
char *u = NULL;
if (strcmp(arg, "track") == 0) {
u = g_strconcat("http://api.soundcloud.com/tracks/",
rest, ".json?client_id=", soundcloud_config.apikey, NULL);
} else if (strcmp(arg, "playlist") == 0) {
u = g_strconcat("http://api.soundcloud.com/playlists/",
rest, ".json?client_id=", soundcloud_config.apikey, NULL);
} else if (strcmp(arg, "url") == 0) {
/* Translate to soundcloud resolver call. libcurl will automatically
follow the redirect to the right resource. */
u = soundcloud_resolve(rest);
}
g_free(s);
if (u == NULL) {
g_warning("unknown soundcloud URI");
return NULL;
}
yajl_handle hand;
struct parse_data data;
data.got_url = 0;
data.title = NULL;
data.stream_url = NULL;
#ifdef HAVE_YAJL1
hand = yajl_alloc(&parse_callbacks, NULL, NULL, (void *) &data);
#else
hand = yajl_alloc(&parse_callbacks, NULL, (void *) &data);
#endif
int ret = soundcloud_parse_json(u, hand, mutex, cond);
g_free(u);
yajl_free(hand);
if (data.title != NULL)
g_free(data.title);
if (data.stream_url != NULL)
g_free(data.stream_url);
if (ret == -1)
return NULL;
data.songs.reverse();
return new MemoryPlaylistProvider(std::move(data.songs));
}
static const char *const soundcloud_schemes[] = {
"soundcloud",
NULL
};
const struct playlist_plugin soundcloud_playlist_plugin = {
"soundcloud",
soundcloud_init,
soundcloud_finish,
soundcloud_open_uri,
nullptr,
nullptr,
nullptr,
soundcloud_schemes,
nullptr,
nullptr,
};
|