Skip to content

Commit 1593435

Browse files
committed
- playing around with the spotify API
1 parent a093eb7 commit 1593435

File tree

2 files changed

+49
-62
lines changed

2 files changed

+49
-62
lines changed

tools/spotify_qr.html

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<title>Spotify Track QR Generator</title>
5-
<script src="https://cdn.jsdelivr.net/npm/qrcode.js@1.0.0/qrcode.min.js"></script>
5+
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
66
<script src="https://cdn.tailwindcss.com"></script>
77
</head>
88
<body class="bg-gray-100 min-h-screen flex items-center justify-center">
@@ -59,32 +59,27 @@ <h1 class="text-2xl font-bold text-center mb-6 text-gray-800">Spotify Track QR G
5959
const qrcode = document.getElementById('qrcode');
6060
const downloadButton = document.getElementById('downloadButton');
6161

62-
// Clear previous QR code
6362
qrcode.innerHTML = '';
6463

65-
// Generate new QR code
66-
QRCode.toCanvas(qrcode, url, {
64+
new QRCode(qrcode, {
65+
text: url,
6766
width: 256,
68-
margin: 2,
69-
color: {
70-
dark: '#000000',
71-
light: '#ffffff'
72-
}
73-
}, function (error) {
74-
if (error) {
75-
console.error(error);
76-
alert('Error generating QR code');
77-
return;
78-
}
67+
height: 256,
68+
colorDark: "#000000",
69+
colorLight: "#ffffff",
70+
correctLevel: QRCode.CorrectLevel.H
71+
});
7972

80-
// Show QR code and download button
81-
qrcode.classList.remove('hidden');
82-
downloadButton.classList.remove('hidden');
73+
qrcode.classList.remove('hidden');
74+
downloadButton.classList.remove('hidden');
8375

84-
// Update download link
76+
setTimeout(() => {
8577
const downloadLink = document.getElementById('downloadLink');
86-
downloadLink.href = qrcode.toDataURL('image/png');
87-
});
78+
const canvas = qrcode.querySelector('canvas');
79+
if (canvas) {
80+
downloadLink.href = canvas.toDataURL('image/png');
81+
}
82+
}, 100);
8883
}
8984
</script>
9085
</body>

tools/spotify_test.html

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -140,54 +140,46 @@
140140
}
141141
});
142142

143-
playerInstance.addListener('ready', ({ device_id }) => {
144-
fetch('https://api.spotify.com/v1/me/player', {
145-
method: 'PUT',
146-
headers: {
147-
'Authorization': `Bearer ${token}`,
148-
'Content-Type': 'application/json'
149-
},
150-
body: JSON.stringify({
151-
device_ids: [device_id],
152-
play: false
153-
})
154-
}).then(response => {
155-
if (response.status === 204) {
156-
// Add a small delay to ensure the device is fully registered
157-
setTimeout(() => {
158-
const trackId = getUrlParameter('track');
159-
if (trackId) {
160-
playTrackById(trackId, token);
161-
}
162-
}, 1000);
143+
let deviceReady = false;
144+
playerInstance.addListener('ready', async ({ device_id }) => {
145+
deviceReady = true;
146+
try {
147+
await fetch('https://api.spotify.com/v1/me/player', {
148+
method: 'PUT',
149+
headers: {
150+
'Authorization': `Bearer ${token}`,
151+
'Content-Type': 'application/json'
152+
},
153+
body: JSON.stringify({
154+
device_ids: [device_id],
155+
play: false
156+
})
157+
});
158+
159+
const trackId = getUrlParameter('track');
160+
if (trackId) {
161+
await new Promise(resolve => setTimeout(resolve, 1000));
162+
await fetch('https://api.spotify.com/v1/me/player/play', {
163+
method: 'PUT',
164+
headers: {
165+
'Authorization': `Bearer ${token}`,
166+
'Content-Type': 'application/json'
167+
},
168+
body: JSON.stringify({
169+
uris: [`spotify:track:${trackId}`]
170+
})
171+
});
172+
updatePlayPauseButton(true);
163173
}
164-
});
174+
} catch (error) {
175+
console.error('Error during player initialization:', error);
176+
}
165177
});
166178

167179
playPauseButton.onclick = togglePlayPause;
168180
playerInstance.connect();
169181
}
170182

171-
async function playTrackById(trackId, token) {
172-
try {
173-
const response = await fetch('https://api.spotify.com/v1/me/player/play', {
174-
method: 'PUT',
175-
headers: {
176-
'Authorization': `Bearer ${token}`,
177-
'Content-Type': 'application/json'
178-
},
179-
body: JSON.stringify({
180-
uris: [`spotify:track:${trackId}`]
181-
})
182-
});
183-
if (response.status === 204) {
184-
updatePlayPauseButton(true);
185-
}
186-
} catch (error) {
187-
console.error('Error playing track:', error);
188-
}
189-
}
190-
191183
const storedClientId = localStorage.getItem('spotify_client_id');
192184
if (storedClientId) {
193185
document.getElementById('clientId').value = storedClientId;

0 commit comments

Comments
 (0)