Skip to content

Commit abe7595

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

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed

tools/spotify_test.html

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,68 @@
144144
return hashParams;
145145
}
146146

147-
async function getPlaylistTracks(playlistUrl) {
148-
const playlistId = playlistUrl.split('/playlist/')[1].split('?')[0];
149-
const response = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`, {
150-
headers: {
151-
'Authorization': `Bearer ${localStorage.getItem('spotify_access_token')}`
147+
async function getPlaylistTracks(input) {
148+
// Check if input is a playlist URL
149+
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');
152153
}
153-
});
154-
const data = await response.json();
155-
return data.items.map(item => ({
156-
id: item.track.id,
157-
name: item.track.name,
158-
artist: item.track.artists[0].name,
159-
year: item.track.album.release_date.split('-')[0]
160-
}));
154+
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+
});
160+
161+
if (!response.ok) {
162+
throw new Error('Failed to fetch playlist data');
163+
}
164+
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+
}));
172+
}
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+
});
193+
194+
if (!response.ok) {
195+
continue;
196+
}
197+
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;
208+
}
161209
}
162210

163211
function createQRCard(track) {
@@ -186,11 +234,21 @@ <h3 class="text-xl font-bold">${track.name}</h3>
186234
}
187235

188236
async function createQRCodesFromPlaylist() {
189-
const playlistUrl = prompt("Enter Spotify playlist URL:");
190-
if (!playlistUrl) return;
237+
const input = prompt(
238+
"Enter either:\n" +
239+
"1. Spotify playlist URL (e.g., https://open.spotify.com/playlist/...)\n" +
240+
"2. List of Spotify track URLs (one per line)"
241+
);
242+
243+
if (!input) return;
191244

192245
try {
193-
const tracks = await getPlaylistTracks(playlistUrl);
246+
const tracks = await getPlaylistTracks(input);
247+
248+
if (tracks.length === 0) {
249+
throw new Error('No valid tracks found');
250+
}
251+
194252
const printArea = document.getElementById('printArea');
195253
printArea.innerHTML = '';
196254

@@ -200,7 +258,7 @@ <h3 class="text-xl font-bold">${track.name}</h3>
200258

201259
window.print();
202260
} catch (error) {
203-
alert('Error processing playlist: ' + error.message);
261+
alert('Error processing tracks: ' + error.message);
204262
}
205263
}
206264

0 commit comments

Comments
 (0)