-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (103 loc) · 3.16 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
const fs = require('fs');
const axios = require('axios');
const accessToken = process.env.ACCESS_TOKEN;
const handleAsyncOperation = async (options) => {
try {
const response = await axios(options);
return response?.data;
} catch (e) {
throw new Error(
`Can not fetch url ${options?.url}, error: ${JSON.stringify(
e?.response?.data?.error,
)}, status: ${e?.response?.status}`,
);
}
};
const getSongs = async (playlistId, offset = 0) => {
const options = {
url: `https://api.spotify.com/v1/playlists/${playlistId}/tracks?offset=${offset}&limit=100`,
headers: {
Authorization: 'Bearer ' + accessToken,
},
};
const response = await handleAsyncOperation(options);
return response?.items?.map((song) => {
Reflect.deleteProperty(song.track.album, 'available_markets');
Reflect.deleteProperty(song.track, 'available_markets');
return song;
});
};
const extractSongsFromPlaylist = async (playlistId) => {
let songs = await getSongs(playlistId);
let offset = 100;
const totalSongs = [...songs];
while (songs.length > 0) {
songs = await getSongs(playlistId, offset);
offset += 100;
totalSongs.push(...songs);
}
return totalSongs;
};
const getPlaylists = async () => {
const options = {
url: 'https://api.spotify.com/v1/me/playlists?limit=50',
headers: {
Authorization: 'Bearer ' + accessToken,
},
};
const response = await handleAsyncOperation(options);
return response?.items;
};
const fetchSavedTracks = async (offset = 0) => {
const options = {
url: `https://api.spotify.com/v1/me/tracks?offset=${offset}&limit=50`,
headers: {
Authorization: 'Bearer ' + accessToken,
},
};
const response = await handleAsyncOperation(options);
return response?.items?.map((song) => {
Reflect.deleteProperty(song.track.album, 'available_markets');
Reflect.deleteProperty(song.track, 'available_markets');
return song;
});
};
const getSavedTracks = async () => {
let songs = await fetchSavedTracks();
let offset = 50;
const totalSongs = [...songs];
while (songs.length > 0) {
songs = await fetchSavedTracks(offset);
offset += 50;
totalSongs.push(...songs);
}
return totalSongs;
};
const processing = async () => {
const library = {};
const playlists = await getPlaylists();
for (const playlist of playlists) {
const { name, id } = playlist;
console.log(`Extracting playlist ${name}...\n`);
const songs = await extractSongsFromPlaylist(id);
const key = `${name}_${id}`.replace(/[ +-]/gim, '_');
library[key] = songs;
console.log(
`Extracted ${songs?.length || 0} songs from ${name} playlist.\n`,
);
}
const savedTracks = await getSavedTracks();
console.log(`Extracted ${savedTracks.length} songs from Liked tracks.`);
library.Liked_Tracks = savedTracks;
for (key in library) {
const formatted = JSON.stringify(library[key]);
const path = `${__dirname}/extracted/${key}_${Date.now()}.json`;
fs.writeFile(path, formatted, (err) => {
if (err) {
console.error(err);
console.error('Error writing result...');
}
});
}
};
processing();