145145 }
146146
147147 async function getPlaylistTracks ( input ) {
148- let playlistId ;
149-
150- if ( input . includes ( 'spotify.com/playlist/' ) ) {
151- // Extract playlist ID using regex for more reliable parsing
152- const match = input . match ( / p l a y l i s t \/ ( [ a - z A - Z 0 - 9 ] + ) / ) ;
153- if ( ! match || ! match [ 1 ] ) {
154- throw new Error ( 'Could not extract playlist ID from URL' ) ;
155- }
156- playlistId = match [ 1 ] ;
157- } else {
158- throw new Error ( 'Invalid playlist URL format' ) ;
148+ let playlistId = input . match ( / p l a y l i s t \/ ( [ a - z A - Z 0 - 9 ] + ) / ) ?. [ 1 ] ;
149+ if ( ! playlistId ) {
150+ throw new Error ( 'Could not extract playlist ID from URL' ) ;
159151 }
160152
161- console . log ( 'Attempting to fetch playlist:' , playlistId ) ; // For debugging
162-
163153 const response = await fetch ( `https://api.spotify.com/v1/playlists/${ playlistId } /tracks` , {
164154 headers : {
165155 'Authorization' : `Bearer ${ localStorage . getItem ( 'spotify_access_token' ) } `
172162
173163 const data = await response . json ( ) ;
174164
175- if ( ! data . items || ! Array . isArray ( data . items ) ) {
176- throw new Error ( 'Invalid playlist data received' ) ;
177- }
178-
179- return data . items . map ( item => {
180- if ( ! item . track ) {
181- console . warn ( 'Skipping invalid track item' ) ;
182- return null ;
183- }
184- return {
185- id : item . track . id ,
186- name : item . track . name ,
187- artist : item . track . artists [ 0 ] . name ,
188- year : item . track . album . release_date . split ( '-' ) [ 0 ]
189- } ;
190- } ) . filter ( track => track !== null ) ;
165+ return data . items
166+ . filter ( item => item ?. track && item . track . album ?. release_date )
167+ . map ( item => ( {
168+ id : item . track . id || '' ,
169+ name : item . track . name || 'Unknown Title' ,
170+ artist : item . track . artists ?. [ 0 ] ?. name || 'Unknown Artist' ,
171+ year : item . track . album . release_date . split ( '-' ) [ 0 ] || 'Unknown Year'
172+ } ) ) ;
191173 }
192174
193175 function createQRCard ( track ) {
@@ -216,19 +198,14 @@ <h3 class="text-xl font-bold">${track.name}</h3>
216198 }
217199
218200 async function createQRCodesFromPlaylist ( ) {
219- const input = prompt (
220- "Enter either:\n" +
221- "1. Spotify playlist URL (e.g., https://open.spotify.com/playlist/...)\n" +
222- "2. List of Spotify track URLs (one per line)"
223- ) ;
224-
201+ const input = prompt ( "Enter Spotify playlist URL:" ) ;
225202 if ( ! input ) return ;
226203
227204 try {
228205 const tracks = await getPlaylistTracks ( input ) ;
229206
230207 if ( tracks . length === 0 ) {
231- throw new Error ( 'No valid tracks found' ) ;
208+ throw new Error ( 'No valid tracks found in playlist ' ) ;
232209 }
233210
234211 const printArea = document . getElementById ( 'printArea' ) ;
@@ -241,7 +218,7 @@ <h3 class="text-xl font-bold">${track.name}</h3>
241218 window . print ( ) ;
242219 } catch ( error ) {
243220 alert ( 'Error processing tracks: ' + error . message ) ;
244- console . log ( error ) ;
221+ console . error ( 'Detailed error:' , error ) ;
245222 }
246223 }
247224
0 commit comments