@@ -2,81 +2,67 @@ const spotifyApi = require('../utils/spotifyApi');
22
33const path = require ( 'path' ) ;
44const playlistImages = [
5- path . join ( __dirname , '../images/1.jpg' ) ,
6- path . join ( __dirname , '../images/2.jpg' ) ,
7- path . join ( __dirname , '../images/3.jpg' ) ,
8- path . join ( __dirname , '../images/4.jpg' )
5+ path . join ( __dirname , '../images/1.jpg' ) ,
6+ path . join ( __dirname , '../images/2.jpg' ) ,
7+ path . join ( __dirname , '../images/3.jpg' ) ,
8+ path . join ( __dirname , '../images/4.jpg' )
99] ;
1010
11-
1211const convertImageToBase64 = async ( imagePath ) => {
13- const fs = require ( 'fs' ) . promises ;
14- const buffer = await fs . readFile ( imagePath ) ;
15- return buffer . toString ( 'base64' ) ;
12+ const fs = require ( 'fs' ) . promises ;
13+ const buffer = await fs . readFile ( imagePath ) ;
14+ return buffer . toString ( 'base64' ) ;
1615} ;
1716
18-
19- const getAuthUrl = ( ) => {
20- const scopes = [ 'user-library-read' , 'playlist-modify-private' , 'playlist-modify-public' ] ;
21- return spotifyApi . createAuthorizeURL ( scopes , 'state-key' ) ;
17+ const getAuthUrl = ( scopes ) => {
18+ return spotifyApi . createAuthorizeURL ( scopes , 'state' ) ;
2219} ;
2320
2421const setAccessToken = ( accessToken ) => {
25- spotifyApi . setAccessToken ( accessToken ) ;
22+ spotifyApi . setAccessToken ( accessToken ) ;
2623} ;
2724
28- const getUserPlaylists = async ( offset = 0 ) => {
29- const playlistResponse = await spotifyApi . getUserPlaylists ( { offset } ) ;
30-
31- const likedSongsResponse = await spotifyApi . getMySavedTracks ( ) ;
32-
33- return {
34- playlists : playlistResponse . body ,
35- likedSongs : likedSongsResponse . body
36- } ;
25+ const getUserPlaylists = async ( offset = 0 , spotifyApi ) => {
26+ const playlistResponse = await spotifyApi . getUserPlaylists ( { offset } ) ;
27+
28+ const likedSongsResponse = await spotifyApi . getMySavedTracks ( ) ;
29+
30+ return {
31+ playlists : playlistResponse . body ,
32+ likedSongs : likedSongsResponse . body
33+ } ;
3734} ;
3835
39- const getLikedSongs = async ( ) => {
40- try {
41- const response = await spotifyApi . getMySavedTracks ( {
42- limit : 50 ,
43- offset : 0
44- } ) ;
45- return response . body ;
46- } catch ( error ) {
47- console . error ( 'Error fetching liked songs:' , error ) ;
48- if ( error . statusCode ) {
49- throw new Error ( `Spotify API error: ${ error . statusCode } - ${ error . message } ` ) ;
50- }
51- throw error ;
52- }
36+ const getLikedSongs = async ( spotifyApi ) => {
37+ try {
38+ const response = await spotifyApi . getMySavedTracks ( {
39+ limit : 50 ,
40+ offset : 0
41+ } ) ;
42+ return response . body ;
43+ } catch ( error ) {
44+ console . error ( 'Error fetching liked songs:' , error ) ;
45+ if ( error . statusCode ) {
46+ throw new Error ( `Spotify API error: ${ error . statusCode } - ${ error . message } ` ) ;
47+ }
48+ throw error ;
49+ }
5350} ;
5451
55-
56- const getPlaylist = async ( playlistId ) => {
57- const response = await spotifyApi . getPlaylist ( playlistId ) ;
58- return response . body ;
52+ const getPlaylist = async ( playlistId , spotifyApi ) => {
53+ const response = await spotifyApi . getPlaylist ( playlistId ) ;
54+ return response . body ;
5955} ;
6056
61- const getPlaylistTracks = async ( playlistId ) => {
62- const response = await spotifyApi . getPlaylistTracks ( playlistId ) ;
63- return response . body ;
57+ const getPlaylistTracks = async ( playlistId , spotifyApi ) => {
58+ const response = await spotifyApi . getPlaylistTracks ( playlistId ) ;
59+ return response . body ;
6460} ;
6561
66- const getTrackRecommendations = async ( seedTrackId ) => {
67- const seedTrackFeatures = await spotifyApi . getAudioFeaturesForTrack ( seedTrackId ) ;
68-
62+ const getTrackRecommendations = async ( seedTrackId , spotifyApi ) => {
6963 const response = await spotifyApi . getRecommendations ( {
70- seed_tracks : [ seedTrackId ] ,
64+ seed_tracks : [ seedTrackId ] ,
7165 limit : 100 ,
72- target_instrumentalness : seedTrackFeatures . body . instrumentalness ,
73- target_acousticness : seedTrackFeatures . body . acousticness ,
74- min_instrumentalness : Math . max ( 0 , seedTrackFeatures . body . instrumentalness - 0.2 ) ,
75- max_instrumentalness : Math . min ( 1 , seedTrackFeatures . body . instrumentalness + 0.2 ) ,
76- target_key : seedTrackFeatures . body . key ,
77- target_mode : seedTrackFeatures . body . mode ,
78- target_time_signature : seedTrackFeatures . body . time_signature ,
79- min_popularity : 20 ,
8066 } ) ;
8167
8268 const tracks = response . body . tracks ;
@@ -90,7 +76,7 @@ const getTrackRecommendations = async (seedTrackId) => {
9076 selectedTracks . push ( track ) ;
9177 totalDurationMs += track . duration_ms ;
9278 }
93-
79+
9480 if ( totalDurationMs >= TARGET_DURATION_MS - MARGIN_MS ) {
9581 break ;
9682 }
@@ -99,45 +85,45 @@ const getTrackRecommendations = async (seedTrackId) => {
9985 return selectedTracks ;
10086} ;
10187
102- const createPlaylist = async ( userId , name , description , trackUris ) => {
103- const playlistResponse = await spotifyApi . createPlaylist ( userId , {
104- name,
105- description,
106- } ) ;
88+ const createPlaylist = async ( userId , name , description , trackUris , spotifyApi ) => {
89+ const playlistResponse = await spotifyApi . createPlaylist ( userId , {
90+ name,
91+ description,
92+ } ) ;
10793
108- const playlistId = playlistResponse . body . id ;
109- await spotifyApi . addTracksToPlaylist ( playlistId , trackUris ) ;
94+ const playlistId = playlistResponse . body . id ;
95+ await spotifyApi . addTracksToPlaylist ( playlistId , trackUris ) ;
11096
111- const randomImageIndex = Math . floor ( Math . random ( ) * playlistImages . length ) ;
112- const imagePath = playlistImages [ randomImageIndex ] ;
113-
114- const imageData = await convertImageToBase64 ( imagePath ) ;
115- await spotifyApi . uploadCustomPlaylistCoverImage ( playlistId , imageData ) ;
97+ const randomImageIndex = Math . floor ( Math . random ( ) * playlistImages . length ) ;
98+ const imagePath = playlistImages [ randomImageIndex ] ;
99+
100+ const imageData = await convertImageToBase64 ( imagePath ) ;
101+ await spotifyApi . uploadCustomPlaylistCoverImage ( playlistId , imageData ) ;
116102
117- return playlistId ;
103+ return playlistId ;
118104} ;
119105
120- const createPlaylistFromSeedTrack = async ( userId , seedTrackId ) => {
121- const seedTrack = await spotifyApi . getTrack ( seedTrackId ) ;
122- const seedTrackName = seedTrack . body . name ;
106+ const createPlaylistFromSeedTrack = async ( userId , seedTrackId , spotifyApi ) => {
107+ const seedTrack = await spotifyApi . getTrack ( seedTrackId ) ;
108+ const seedTrackName = seedTrack . body . name ;
123109
124- const recommendations = await getTrackRecommendations ( seedTrackId ) ;
125- const trackUris = recommendations . map ( track => track . uri ) ;
110+ const recommendations = await getTrackRecommendations ( seedTrackId , spotifyApi ) ;
111+ const trackUris = recommendations . map ( track => track . uri ) ;
126112
127- const playlistName = 'Groovz' ;
128- const description = `Similar songs to ${ seedTrackName } ` ;
113+ const playlistName = 'Groovz' ;
114+ const description = `Similar songs to ${ seedTrackName } ` ;
129115
130- const playlistId = await createPlaylist ( userId , playlistName , description , trackUris ) ;
131- return playlistId ;
116+ const playlistId = await createPlaylist ( userId , playlistName , description , trackUris , spotifyApi ) ;
117+ return playlistId ;
132118} ;
133119
134120module . exports = {
135- getAuthUrl,
136- setAccessToken,
137- getUserPlaylists,
138- getLikedSongs,
139- getPlaylist,
140- getPlaylistTracks,
141- getTrackRecommendations,
142- createPlaylistFromSeedTrack,
121+ getAuthUrl,
122+ setAccessToken,
123+ getUserPlaylists,
124+ getLikedSongs,
125+ getPlaylist,
126+ getPlaylistTracks,
127+ getTrackRecommendations,
128+ createPlaylistFromSeedTrack,
143129} ;
0 commit comments