-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
651 lines (576 loc) · 20.4 KB
/
index.js
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
var request = require('request');
var util = require('util');
/**
* Jamendo API client class
* Use it with
* var client = new Jamendo({ client_id: XXXXXX, client_secret: YYYYYYY })
*
* @param {Object} settings A settings object with a client_id member
* @return object An instanciated Jamendo class
*/
var Jamendo = function(settings) {
if (!settings || !settings.client_id) {
throw 'You must provide a client_id setting';
}
this.debug = settings.debug;
this.protocol = settings.protocol || 'http';
this.base_url = this.protocol + '://api.jamendo.com/' + (settings.version ? settings.version : 'v3.0');
this.client_id = settings.client_id;
this.client_secret = settings.client_secret;
this.rejectUnauthorized = settings.rejectUnauthorized || false;
this.retry = settings.retry || false;
};
module.exports = Jamendo;
/**
* Clean a object parameters according to Jamendo policy
* - client_id is set if not specified
* - limit and offset are set if not specified
* - arrays are converted to space-separated strings
*
* @param {String} path The path parameters will be used against
* @param {Object} object The object parameters to clean
* @return {Object} Cleaned object
*/
Jamendo.prototype.clean_params = function(path, object){
var param,
default_params = {
limit : 10,
offset : 0,
format : 'json',
client_id : this.client_id
};
object = object || {};
// explicit params
for (var name in default_params) {
if (typeof object[name] === 'undefined') {
object[name] = default_params[name];
}
}
// datebetween params
if (object.datebetween && util.isArray(object.datebetween) && object.datebetween.length === 2) {
for (var i = 0; i < 2; i++) {
// perfect
if (util.isDate(object.datebetween[i])) {
// a timestamp (milliseconds)
} else if (parseInt(object.datebetween[i], 0)) {
object.datebetween[i] = new Date(object.datebetween[i]);
// an IETF-compliant RFC 2822 timestamps string
} else {
object.datebetween[i] = new Date(object.datebetween[i]);
}
}
object.datebetween = this.format_date(object.datebetween[0]) + '_' + this.format_date(object.datebetween[1]);
}
// arrays as strings
for (var pname in object) {
if (util.isArray(object[pname])) {
object[pname] = object[pname].join(' ');
}
}
return object;
};
/**
* Format a Date according to API
* @param {Date} date Date to format
*/
Jamendo.prototype.format_date = function(date) {
return date.getFullYear() + '-' + ('0' + date.getMonth()).slice(-2) + '-' + ('0' + (date.getDate() + 1)).slice(-2);
};
/**
* Main request wrapper
*
* @param {String} path The path to the api endpoint
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.request = function(path, parameters, callback) {
parameters = this.clean_params(path, parameters || {});
var self = this;
var r = request({
url: this.base_url + path,
method: 'GET',
rejectUnauthorized: this.rejectUnauthorized,
qs: parameters,
json: true
}, function(error, response, body){
if (error && !response && self.retry) {
if (self.debug) {
console.log('network error, retry', error);
}
return self.request(path, parameters, callback);
}
if (typeof callback === 'function') {
callback(error, body);
}
});
return r;
};
/**
* Main write request wrapper
*
* @param {String} path The path to the api endpoint
* @param {Object} parameters A query string object, must contain an access_token member
* @param {Function} callback The request callback(error, error_message, warnings)
* @return {Request} The request object
*/
Jamendo.prototype.write_request = function(path, parameters, callback) {
parameters = parameters || {};
var self = this;
var r = request({
url: this.base_url + path,
method: 'POST',
rejectUnauthorized: this.rejectUnauthorized,
form: parameters,
json: true
}, function(error, response, body){
if (error && !response && self.retry) {
if (self.debug) {
console.log('network error, retry', error);
}
return self.write_request(path, parameters, callback);
}
if (typeof callback === 'function') {
// http error
if (error || !response) {
callback(error, 'network error', null);
// api error
} else if (parseInt(response.headers.status, 0) !== 0) {
callback(response.headers.code, response.headers.error_message, response.headers.warnings);
// api success ! /me eyebrow
} else {
callback(response.headers.code, response.headers.error_message, response.headers.warnings);
}
}
});
return r;
};
/**
* Wrapper to the /tracks endpoint
*
* @see http://developer.jamendo.com/v3.0/tracks
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.tracks = function(parameters, callback) {
return this.request('/tracks', parameters, callback);
};
/**
* Wrapper to the /tracks/file endpoint
*
* @see http://developer.jamendo.com/v3.0/tracks/file
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.tracks_file = function(parameters, callback) {
return this.request('/tracks/file', parameters, callback);
};
/**
* Wrapper to the /albums endpoint
*
* @see http://developer.jamendo.com/v3.0/albums
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.albums = function(parameters, callback) {
return this.request('/albums', parameters, callback);
};
/**
* Wrapper to the /album/tracks endpoint
*
* @see http://developer.jamendo.com/v3.0/album/tracks
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.album_tracks = function(parameters, callback) {
return this.request('/albums/tracks', parameters, callback);
};
/**
* Wrapper to the /albums/file endpoint
*
* @see http://developer.jamendo.com/v3.0/albums/file
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.albums_file = function(parameters, callback) {
return this.request('/albums/file', parameters, callback);
};
/**
* Wrapper to the /albums/musicinfo endpoint
*
* @see http://developer.jamendo.com/v3.0/albums/musicinfo
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.albums_musicinfo = function(parameters, callback) {
return this.request('/albums/musicinfo', parameters, callback);
};
/**
* Wrapper to the /artists endpoint
*
* @see http://developer.jamendo.com/v3.0/artists
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.artists = function(parameters, callback) {
return this.request('/artists', parameters, callback);
};
/**
* Wrapper to the /artist/albums endpoint
*
* @see http://developer.jamendo.com/v3.0/artist_albums
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.artist_albums = function(parameters, callback) {
return this.request('/artists/albums', parameters, callback);
};
/**
* Wrapper to the /artist/tracks endpoint
*
* @see http://developer.jamendo.com/v3.0/artist_tracks
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.artist_tracks = function(parameters, callback) {
return this.request('/artists/tracks', parameters, callback);
};
/**
* Wrapper to the /artists/musicinfo endpoint
*
* @see http://developer.jamendo.com/v3.0/artists/musicinfo
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.artists_musicinfo = function(parameters, callback) {
return this.request('/artists/musicinfo', parameters, callback);
};
/**
* Wrapper to the /artists/locations endpoint
*
* @see http://developer.jamendo.com/v3.0/artists/locations
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.artists_locations = function(parameters, callback) {
return this.request('/artists/locations', parameters, callback);
};
/**
* Wrapper to the /concerts endpoint
*
* @see http://developer.jamendo.com/v3.0/concerts
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.concerts = function(parameters, callback) {
return this.request('/concerts', parameters, callback);
};
/**
* Wrapper to the /playlists endpoint
*
* @see http://developer.jamendo.com/v3.0/playlists
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.playlists = function(parameters, callback) {
return this.request('/playlists', parameters, callback);
};
/**
* Wrapper to the /playlists/tracks endpoint
*
* @see http://developer.jamendo.com/v3.0/playlists_tracks
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.playlists_tracks = function(parameters, callback) {
return this.request('/playlists/tracks', parameters, callback);
};
/**
* Wrapper to the /playlists/file endpoint
*
* @see http://developer.jamendo.com/v3.0/playlists/file
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.playlists_file = function(parameters, callback) {
return this.request('/playlists/file', parameters, callback);
};
/**
* Wrapper to the /reviews endpoint
*
* @see http://developer.jamendo.com/v3.0/reviews
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.reviews = function(parameters, callback) {
return this.request('/reviews', parameters, callback);
};
/**
* Wrapper to the /reviews/albums endpoint
*
* @see http://developer.jamendo.com/v3.0/reviews_albums
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.reviews_albums = function(parameters, callback) {
return this.request('/reviews/albums', parameters, callback);
};
/**
* Wrapper to the /radios endpoint
*
* @see http://developer.jamendo.com/v3.0/radios
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.radios = function(parameters, callback) {
return this.request('/radios', parameters, callback);
};
/**
* Wrapper to the /radios/stream endpoint
*
* @see http://developer.jamendo.com/v3.0/radios/stream
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.radios_stream = function(parameters, callback) {
return this.request('/radios/stream', parameters, callback);
};
/**
* Wrapper to the /users endpoint
*
* @see http://developer.jamendo.com/v3.0/users
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users = function(parameters, callback) {
return this.request('/users', parameters, callback);
};
/**
* Wrapper to the /users/artists endpoint
*
* @see http://developer.jamendo.com/v3.0/users/artists
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users_artists = function(parameters, callback) {
return this.request('/users/artists', parameters, callback);
};
/**
* Wrapper to the /users/albums endpoint
*
* @see http://developer.jamendo.com/v3.0/users/albums
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users_albums = function(parameters, callback) {
return this.request('/users/albums', parameters, callback);
};
/**
* Wrapper to the /users/tracks endpoint
*
* @see http://developer.jamendo.com/v3.0/users/tracks
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users_tracks = function(parameters, callback) {
return this.request('/users/tracks', parameters, callback);
};
/**
* Wrapper to the /users/artists endpoint, with an explicit 'fan' relation
*
* @see http://developer.jamendo.com/v3.0/users/artists
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users_favorites_artists = function(parameters, callback) {
parameters.relation = 'fan';
return this.request('/users/artists', parameters, callback);
};
/**
* Wrapper to the /users/albums endpoint, with an explicit 'fan' relation
*
* @see http://developer.jamendo.com/v3.0/users/albums
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users_favorites_albums = function(parameters, callback) {
parameters.relation = 'fan';
return this.request('/users/albums', parameters, callback);
};
/**
* Wrapper to the /users/tracks endpoint, with an explicit 'fan' relation
*
* @see http://developer.jamendo.com/v3.0/users/tracks
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.users_favorites_tracks = function(parameters, callback) {
parameters.relation = 'fan';
return this.request('/users/tracks', parameters, callback);
};
/**
* Wrapper to the /autocomplete endpoint
*
* @see http://developer.jamendo.com/v3.0/autocomplete
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, response_body)
* @return {Request} The request object
*/
Jamendo.prototype.autocomplete = function(parameters, callback) {
return this.request('/autocomplete', parameters, callback);
};
/********************************************/
/**
* Wrapper to the /setuser/fan endpoint
*
* Use this method to let the user identified by 'userid' become a fan of the artist identified 'artistid' (/users/artists?relation='fan').
* Note that if the artist doesn't exist, no error will be raised, but the track will not appear in any read request of api and website.
*
* @see http://developer.jamendo.com/v3.0/setuser/fan
* @param {Object} parameters A query string object ( access_token, artist_id are required )
* @param {Function} callback The request callback(error, error_message, warnings)
* @return {Request} The request object
*/
Jamendo.prototype.setuser_fan = function(parameters, callback) {
return this.write_request('/setuser/fan', parameters, callback);
};
/**
* Wrapper to the /setuser/favorite endpoint
*
* Use this method to add a given track to the user's preferites (/users/tracks?relation='preferite') (also called 'Favorites' playlist in Jamendo.com).
* Note that if the track doesn't exist, no error will be raised, but the track will not appear in any read request of api and website.
*
* @see http://developer.jamendo.com/v3.0/setuser/favorite
* @param {Object} parameters A query string object ( access_token, track_id are required )
* @param {Function} callback The request callback(error, error_message, warnings)
* @return {Request} The request object
*/
Jamendo.prototype.setuser_favorite = function(parameters, callback) {
return this.write_request('/setuser/favorite', parameters, callback);
};
/**
* Wrapper to the /setuser/like endpoint
*
* Let this 'userid' like the given 'trackid' (/users/tracks?relation='like').
* Note that if the track doesn't exist, no error will be raised, but the track will not appear in any read request of api and website.
*
* @see http://developer.jamendo.com/v3.0/setuser/like
* @param {Object} parameters A query string object ( access_token, track_id are required )
* @param {Function} callback The request callback(error, error_message, warnings)
* @return {Request} The request object
*/
Jamendo.prototype.setuser_like = function(parameters, callback) {
return this.write_request('/setuser/like', parameters, callback);
};
/**
* Wrapper to the /setuser/dislike endpoint
*
* As youtube and many other social web site, on Jamendo is possible to 'like' a track, but also to 'dislike' a track.
* This method allow you to such an action.
*
* @see http://developer.jamendo.com/v3.0/setuser/dislike
* @param {Object} parameters A query string object ( access_token, track_id are required )
* @param {Function} callback The request callback(error, error_message, warnings)
* @return {Request} The request object
*/
Jamendo.prototype.setuser_dislike = function(parameters, callback) {
return this.write_request('/setuser/dislike', parameters, callback);
};
/******************************************/
/**
* Authorize request wrapper
*
* The objective of such a request is to ask the user if he agrees to grant some rights to your application.
*
* @param {Object} parameters A query string object
* @param {Function} callback The request callback(error, login_url)
* @return {Request} The request object
*/
Jamendo.prototype.authorize = function(parameters, callback) {
parameters = parameters || {};
parameters.client_id = this.client_id;
var self = this;
// send authorize request
var r = request({
url: this.base_url + '/oauth/authorize',
method: 'GET',
rejectUnauthorized: this.rejectUnauthorized,
qs: parameters
}, function(error, response, body){
if (error && !response && self.retry) {
if (self.debug) {
console.log('network error, retry', error);
}
return self.authorize(parameters, callback);
}
// forward the login url
callback(null, response.request.href);
});
return r;
};
/**
* Grant request wrapper
*
* The main objective of the OAuth2 Grant request is to exchange the authorization code you have received with the OAuth2 Authorize request to get an 'access token'.
*
* @param {Object} parameters A query string object (code is required)
* @param {Function} callback The request callback(error, login_url)
* @return {Request} The request object
*/
Jamendo.prototype.grant = function(parameters, callback) {
parameters = parameters || {};
parameters.client_id = this.client_id;
parameters.client_secret = this.client_secret;
parameters.grant_type = 'authorization_code';
if (!parameters.client_secret) {
return callback('You must provide a client_secret to the constructor', null);
}
if (!parameters.code) {
return callback('You must provide a code in parameters', null);
}
var self = this;
// send authorize request
var r = request({
url: this.base_url + '/oauth/grant',
method: 'POST',
rejectUnauthorized: this.rejectUnauthorized,
form: parameters,
json: true
}, function(error, response, body){
if (error && !response && self.retry) {
if (self.debug) {
console.log('network error, retry', error);
}
return self.grant(parameters, callback);
}
// forward oauth detail
callback(null, body);
});
return r;
};