Skip to content

Commit bea9ffd

Browse files
committed
- playing around with the spotify API
1 parent abe7595 commit bea9ffd

File tree

1 file changed

+33
-51
lines changed

1 file changed

+33
-51
lines changed

tools/spotify_test.html

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -145,67 +145,49 @@
145145
}
146146

147147
async function getPlaylistTracks(input) {
148-
// Check if input is a playlist URL
148+
let playlistId;
149+
149150
if (input.includes('spotify.com/playlist/')) {
150-
const playlistId = input.split('/playlist/')[1]?.split('?')[0];
151-
if (!playlistId) {
152-
throw new Error('Invalid playlist URL format');
151+
// Extract playlist ID using regex for more reliable parsing
152+
const match = input.match(/playlist\/([a-zA-Z0-9]+)/);
153+
if (!match || !match[1]) {
154+
throw new Error('Could not extract playlist ID from URL');
153155
}
156+
playlistId = match[1];
157+
} else {
158+
throw new Error('Invalid playlist URL format');
159+
}
154160

155-
const response = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`, {
156-
headers: {
157-
'Authorization': `Bearer ${localStorage.getItem('spotify_access_token')}`
158-
}
159-
});
161+
console.log('Attempting to fetch playlist:', playlistId); // For debugging
160162

161-
if (!response.ok) {
162-
throw new Error('Failed to fetch playlist data');
163+
const response = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`, {
164+
headers: {
165+
'Authorization': `Bearer ${localStorage.getItem('spotify_access_token')}`
163166
}
167+
});
164168

165-
const data = await response.json();
166-
return data.items.map(item => ({
167-
id: item.track.id,
168-
name: item.track.name,
169-
artist: item.track.artists[0].name,
170-
year: item.track.album.release_date.split('-')[0]
171-
}));
169+
if (!response.ok) {
170+
throw new Error(`API request failed with status ${response.status}`);
172171
}
173-
// Handle direct track URLs input
174-
else {
175-
const trackUrls = input.split('\n').filter(url => url.trim());
176-
const tracks = [];
177-
178-
for (const url of trackUrls) {
179-
if (!url.includes('spotify.com/track/')) {
180-
continue;
181-
}
182-
183-
const trackId = url.split('/track/')[1]?.split('?')[0];
184-
if (!trackId) {
185-
continue;
186-
}
187-
188-
const response = await fetch(`https://api.spotify.com/v1/tracks/${trackId}`, {
189-
headers: {
190-
'Authorization': `Bearer ${localStorage.getItem('spotify_access_token')}`
191-
}
192-
});
193172

194-
if (!response.ok) {
195-
continue;
196-
}
173+
const data = await response.json();
197174

198-
const track = await response.json();
199-
tracks.push({
200-
id: track.id,
201-
name: track.name,
202-
artist: track.artists[0].name,
203-
year: track.album.release_date.split('-')[0]
204-
});
205-
}
206-
207-
return tracks;
175+
if (!data.items || !Array.isArray(data.items)) {
176+
throw new Error('Invalid playlist data received');
208177
}
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);
209191
}
210192

211193
function createQRCard(track) {

0 commit comments

Comments
 (0)