-
Notifications
You must be signed in to change notification settings - Fork 4
/
interface.js
83 lines (74 loc) · 2.58 KB
/
interface.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
const response = require('./response')
const getPlaylists = async () => {
const playlists = await response({
type: 'paged',
url: 'me/playlists',
query: { offset: 0, limit: 50 },
})
return playlists
}
const getPlaylistTracks = async playlist_id => {
const playlistTracks = await response({
type: 'paged',
url: 'playlists/' + playlist_id + '/tracks',
query: { offset: 0, limit: 100 },
})
return playlistTracks.items.map(track => track.track)
}
const createPlaylist = async playlist_name => {
const me = await response({ url: 'me' })
playlistWithName = await response({
url: `users/${me.id}/playlists`,
json: { name: playlist_name },
method: 'POST',
})
return playlistWithName
}
const getPlaylistId = async playlist_name => {
const playlists = await getPlaylists()
let playlistWithName = playlists.items.find(e => e.name === playlist_name)
if (!playlistWithName) {
playlistWithName = await createPlaylist(playlist_name)
}
return playlistWithName.id
}
const addTracksToPlaylist = async (track_ids, playlist_id) => {
for (let i = 0; i < Math.ceil(track_ids.length / 100); i++) {
const uris = track_ids.slice(i * 100, (i + 1) * 100).map(e => 'spotify:track:' + e)
await response({
method: 'POST',
url: 'playlists/' + playlist_id + '/tracks',
json: { uris },
})
}
}
const addToPlaylist = async (track_ids, playlist_name) => {
const playlist_id = await getPlaylistId(playlist_name)
if (playlist_id) {
const existingTracks = await getPlaylistTracks(playlist_id)
const existingTrackIds = existingTracks.map(existingTrack => existingTrack.id)
const tracksToAdd = track_ids.filter(
track_id => !existingTrackIds.some(existingTrackId => existingTrackId === track_id)
)
await addTracksToPlaylist(tracksToAdd, playlist_id)
return { playlist_id, added: tracksToAdd.length }
}
}
const getSyncedPlaylists = async ({ spotifyPrefix, rekordboxPrefix }) => {
const playlists = await getPlaylists()
const syncedPlaylists = playlists.items.filter(playlist => playlist.name.indexOf(spotifyPrefix) === 0)
for (const [i, playlist] of syncedPlaylists.entries()) {
const playlistTracks = await getPlaylistTracks(playlist.id)
syncedPlaylists[i] = {
name: playlist.name.replace(spotifyPrefix, rekordboxPrefix),
tracks: playlistTracks.map(track => ({
uri: track.id,
name: track.name,
album: track.album.name,
artist: track.artists.map(e => e.name).join(', '),
})),
}
}
return syncedPlaylists
}
module.exports = { addToPlaylist, getSyncedPlaylists }