1
- const createSpotifyApi = require ( '../utils/spotifyApi' ) ;
1
+ const spotifyApi = require ( '../utils/spotifyApi' ) ;
2
2
3
3
const path = require ( 'path' ) ;
4
4
const playlistImages = [
@@ -18,18 +18,16 @@ const convertImageToBase64 = async (imagePath) => {
18
18
19
19
const getAuthUrl = ( ) => {
20
20
const scopes = [ 'user-library-read' , 'playlist-modify-private' , 'playlist-modify-public' ] ;
21
- return createSpotifyApi . createAuthorizeURL ( scopes , 'state-key' ) ;
21
+ return spotifyApi . createAuthorizeURL ( scopes , 'state-key' ) ;
22
22
} ;
23
23
24
24
const setAccessToken = ( accessToken ) => {
25
- createSpotifyApi . setAccessToken ( accessToken ) ;
25
+ spotifyApi . setAccessToken ( accessToken ) ;
26
26
} ;
27
27
28
- const getUserPlaylists = async ( token , offset = 0 ) => {
29
- const spotifyApi = createSpotifyApi ( ) ;
30
- spotifyApi . setAccessToken ( token ) ;
31
-
28
+ const getUserPlaylists = async ( offset = 0 ) => {
32
29
const playlistResponse = await spotifyApi . getUserPlaylists ( { offset } ) ;
30
+
33
31
const likedSongsResponse = await spotifyApi . getMySavedTracks ( ) ;
34
32
35
33
return {
@@ -40,7 +38,7 @@ const getUserPlaylists = async (token, offset = 0) => {
40
38
41
39
const getLikedSongs = async ( ) => {
42
40
try {
43
- const response = await createSpotifyApi . getMySavedTracks ( {
41
+ const response = await spotifyApi . getMySavedTracks ( {
44
42
limit : 50 ,
45
43
offset : 0
46
44
} ) ;
@@ -56,19 +54,19 @@ const getLikedSongs = async () => {
56
54
57
55
58
56
const getPlaylist = async ( playlistId ) => {
59
- const response = await createSpotifyApi . getPlaylist ( playlistId ) ;
57
+ const response = await spotifyApi . getPlaylist ( playlistId ) ;
60
58
return response . body ;
61
59
} ;
62
60
63
61
const getPlaylistTracks = async ( playlistId ) => {
64
- const response = await createSpotifyApi . getPlaylistTracks ( playlistId ) ;
62
+ const response = await spotifyApi . getPlaylistTracks ( playlistId ) ;
65
63
return response . body ;
66
64
} ;
67
65
68
66
const getTrackRecommendations = async ( seedTrackId ) => {
69
- const seedTrackFeatures = await createSpotifyApi . getAudioFeaturesForTrack ( seedTrackId ) ;
67
+ const seedTrackFeatures = await spotifyApi . getAudioFeaturesForTrack ( seedTrackId ) ;
70
68
71
- const response = await createSpotifyApi . getRecommendations ( {
69
+ const response = await spotifyApi . getRecommendations ( {
72
70
seed_tracks : [ seedTrackId ] ,
73
71
limit : 100 ,
74
72
target_instrumentalness : seedTrackFeatures . body . instrumentalness ,
@@ -102,25 +100,25 @@ const getTrackRecommendations = async (seedTrackId) => {
102
100
} ;
103
101
104
102
const createPlaylist = async ( userId , name , description , trackUris ) => {
105
- const playlistResponse = await createSpotifyApi . createPlaylist ( userId , {
103
+ const playlistResponse = await spotifyApi . createPlaylist ( userId , {
106
104
name,
107
105
description,
108
106
} ) ;
109
107
110
108
const playlistId = playlistResponse . body . id ;
111
- await createSpotifyApi . addTracksToPlaylist ( playlistId , trackUris ) ;
109
+ await spotifyApi . addTracksToPlaylist ( playlistId , trackUris ) ;
112
110
113
111
const randomImageIndex = Math . floor ( Math . random ( ) * playlistImages . length ) ;
114
112
const imagePath = playlistImages [ randomImageIndex ] ;
115
113
116
114
const imageData = await convertImageToBase64 ( imagePath ) ;
117
- await createSpotifyApi . uploadCustomPlaylistCoverImage ( playlistId , imageData ) ;
115
+ await spotifyApi . uploadCustomPlaylistCoverImage ( playlistId , imageData ) ;
118
116
119
117
return playlistId ;
120
118
} ;
121
119
122
120
const createPlaylistFromSeedTrack = async ( userId , seedTrackId ) => {
123
- const seedTrack = await createSpotifyApi . getTrack ( seedTrackId ) ;
121
+ const seedTrack = await spotifyApi . getTrack ( seedTrackId ) ;
124
122
const seedTrackName = seedTrack . body . name ;
125
123
126
124
const recommendations = await getTrackRecommendations ( seedTrackId ) ;
0 commit comments