-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
170 lines (158 loc) · 4.39 KB
/
functions.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
import axios from "axios";
function generateRandomString(size) {
let dictionary =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let str = "";
for (let i = 0; i < size; i++) {
str += dictionary[Math.floor((dictionary.length - 1) * Math.random())];
}
return str;
}
function serialize(obj) {
var str = [];
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
}
return str.join("&");
}
async function getAccessToken(clientId, clientSecret, code, redirectUri) {
try {
const response = await axios
.post(
"https://accounts.spotify.com/api/token",
serialize({
code: code,
redirect_uri: redirectUri,
grant_type: "authorization_code",
}),
{
headers: {
Authorization:
"Basic " +
Buffer.from(`${clientId}:${clientSecret}`).toString("base64"),
},
}
)
return response.data.access_token
} catch (error) {
}
}
async function getUser(access_token) {
try {
const response = await axios.get("https://api.spotify.com/v1/me", {
headers: {
Authorization: "Bearer " + access_token,
},
});
return response.data;
} catch (error) {
return null;
}
}
async function getPlaylistName(playlistId, rapidApiKey, rapidApiHost) {
try {
const response = await axios.get(
"https://youtube-v31.p.rapidapi.com/playlists",
{
params: {
id: playlistId,
part: "snippet",
},
headers: {
"X-RapidAPI-Key": rapidApiKey,
"X-RapidAPI-Host": rapidApiHost,
},
}
);
return response.data.items[0].snippet.title;
} catch (error) {
console.error(error);
return null;
}
}
async function getPlaylistItems(playlistId, rapidApiKey, rapidApiHost) {
try {
const response = await axios.get(
"https://youtube-v31.p.rapidapi.com/playlistItems",
{
params: {
playlistId: playlistId,
part: "snippet",
maxResults: 50,
},
headers: {
"X-RapidAPI-Key": rapidApiKey,
"X-RapidAPI-Host": rapidApiHost,
},
}
);
return response.data.items;
} catch (error) {
console.error(error);
return null;
}
}
async function makePlaylist(access_token, user, playlistId, name) {
try {
const playlist = await axios.post(
"https://api.spotify.com/v1/users/" + user.id + "/playlists",
{
name: name,
description:
"Playlist cloned from https://www.youtube.com/playlist?list=" +
playlistId +
" with Crossify.",
public: true,
},
{
headers: {
Authorization: "Bearer " + access_token,
"Content-Type": "application/json",
},
}
);
return playlist.data;
} catch (error) {
console.log(error);
return null;
}
}
function filterSearchQuery(title) {
const regFilter =
/video song|music video|lyrics|lyric|lyrical|official|oficial|full video|video|audio|[\[\]\(\)\-\|\,\.]/gi;
let newTitle = title.replace(regFilter, "");
// console.log(newTitle);
return newTitle;
}
async function searchSpotify(access_token, query) {
try {
const response = await axios.get("https://api.spotify.com/v1/search", {
params: {
q: query,
type: "track",
limit: 1,
},
headers: {
Authorization: "Bearer " + access_token,
},
});
return response.data.tracks.items[0].uri;
} catch (error) {
console.log(error);
return null;
}
}
async function processPlaylist(access_token, playlistItems) {
let spotifyURI = [];
for (const song of playlistItems) {
const title = filterSearchQuery(song.snippet.title);
const uri = await searchSpotify(access_token, title);
if (uri !== null) {
spotifyURI.push(uri);
}
}
return spotifyURI;
}
export {processPlaylist, getAccessToken, makePlaylist, getPlaylistItems, getPlaylistName, getUser, generateRandomString}