-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsong.js
113 lines (107 loc) · 3.28 KB
/
song.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
const axios = require("axios");
require('dotenv').config({ path: './.env' });
const qs = require("qs");
const res = require("express/lib/response");
const { response } = require("express");
const { all } = require("express/lib/application");
const { compileETag } = require("express/lib/utils");
let token = "";
let all_playlist_songs = [];
// Add a Spotify playlist to the queue of songs
async function add_spotify_playlist(link, callback) {
all_playlist_songs.splice(0, all_playlist_songs.length);
token = await getToken();
const playlist_id_split = link.split("/")[4];
let playlist_id;
if (playlist_id_split){
playlist_id = playlist_id_split.substr(
0,
playlist_id_split.indexOf("?")
);
} else {
callback(`no playlist found`);
return;
}
try {
await recursive_add_playlist(playlist_id, 0);
callback(undefined, all_playlist_songs);
} catch (err) {
console.log(err);
callback(err);
}
}
// Uses recursion to get > 100 songs from a Spotify playlist
const recursive_add_playlist = async (playlist_id, offset) => {
/**
* @param {number} offset: local variable to help keep track of recursive process
* @param {playlist_id} playlist_id: id of playlist to get songs from
*/
var current_offset = offset;
var current_offset_100 = current_offset * 100;
const options = {
headers: {
'Authorization': 'Bearer ' + token
}
}
let response = await axios.get(`https://api.spotify.com/v1/playlists/${playlist_id}/tracks?market=ES&offset=${current_offset_100}`, options);
let playlist = response.data;
if (playlist.items.length < 100) {
push_the_songs(playlist);
} else {
push_the_songs(playlist);
current_offset++;
await recursive_add_playlist(playlist_id, current_offset);
}
}
async function push_the_songs(playlist) {
for (let i = 0; i < playlist.items.length; i++) {
if (playlist.items[i].track) {
let fixed_track_name = playlist.items[i].track.name;
if (fixed_track_name.includes("(")) {
fixed_track_name = fixed_track_name.substr(0, fixed_track_name.indexOf('('));
}
if (playlist.items[i].track.album.artists.length > 0) {
let playlist_data = {
artist_name: playlist.items[i].track.album.artists[0].name,
track_name: fixed_track_name,
track_image: playlist.items[i].track.album.images[0].url,
preview_mp3: playlist.items[i].track.preview_url,
};
all_playlist_songs.push(playlist_data);
}
}
}
}
// Retrieves Spotify API token
const getToken = async (
clientId = process.env.SPOTIFY_CLIENT_ID,
clientSecret = process.env.SPOTIFY_CLIENT_SECRET
) => {
const headers = {
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
auth: {
username: clientId,
password: clientSecret,
},
};
const data = {
grant_type: "client_credentials",
};
try {
const response = await axios.post(
"https://accounts.spotify.com/api/token",
qs.stringify(data),
headers
);
return response.data.access_token;
} catch (err) {
console.log(err.response);
}
};
module.exports = {
add_spotify_playlist: add_spotify_playlist,
all_playlist_songs: all_playlist_songs,
}