-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
87 lines (74 loc) · 2.48 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
var url = require('url');
var util = require('util');
var rest = require('restler');
var cheerio = require('cheerio');
var Spotify = module.exports = {};
Spotify.uri = require('spotify-uri');
// Spotify data urls
Spotify.url = {
playlist: 'https://open.spotify.com/embed?uri=spotify%3Auser%3A%s%3Aplaylist%3A%s'
}
/**
* Lookup a spotify playlist uri.
* @param {String} uri Spotify URI to lookup
* @param {Function} cb Callback
*/
Spotify.playlistUri = function(uri, extras, cb) {
var parsed = typeof uri === 'string' ? Spotify.uri.parse(uri) : uri;
// Playlist
if (parsed.type === 'playlist') {
Spotify.playlist(parsed.user, parsed.id, typeof extras==='function'?extras:cb);
}
// Everything else
else {
console.warn('*WARNING*: Unknown type. It only supports Spotify playlists using the default Spotify URI. Use https://github.com/thelinmichael/spotify-web-api-node to use the full Spotify Web API');
}
}
/**
* Get tracks in a playlist. Same format as an album
* @param {String} user User/creator of this playlist
* @param {String} id ID of the playlist
* @param {Function} cb
* @return {Array} Array of tracks in playlist
*/
Spotify.playlist = function(user, id, cb) {
var tracks = [];
var req = rest.get(util.format(Spotify.url.playlist, user, id)).on('complete', function(result) {
if (result instanceof Error) {
cb('Error: ', result.message);
this.retry(5000); // try again after 5 sec
} else {
var tracks = [];
var $ = cheerio.load(result);
if($('#text').text() == ""){ //No error =D
$('.track-row').each(function(i, el) {
var track = {
"name": $(this).attr('data-name'),
"href": $(this).attr('data-uri'),
"artists": $(this).attr('data-artists').split(", "),
"duration": parseInt($(this).attr('data-duration-ms')),
"cover-640": $(this).attr('data-size-640'),
"cover-300": $(this).attr('data-size-300'),
"cover-64": $(this).attr('data-size-64'),
"number": parseInt($(this).attr('data-position'))
};
tracks.push(track);
});
var playlist = {
playlist: {
// spotify-uri supports parsing of playlist, but not formating -.-
'playlist-id': ['spotify', 'user', user, 'playlist', id].join(':'),
title: $('.context-name').text().split(" by")[0].replace(/ /g, ""),
tracks: tracks,
},
info: {
type: 'playlist'
}
}
cb(null, playlist);
}else{
cb('Error: This playlist is empty or does not exist');
}
}
});
}