-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.ts
51 lines (46 loc) · 1.55 KB
/
spotify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// spotify.ts
import { getAccessToken, refreshAccessToken } from "./auth.ts";
import { readTextFile } from "./utils.ts";
// Function to play a track
export async function playTrack(trackUri?: string) {
let accessToken = await getAccessToken();
const body = trackUri
? JSON.stringify({ uris: [trackUri] })
: JSON.stringify({});
const response = await fetch("https://api.spotify.com/v1/me/player/play", {
method: "PUT",
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: trackUri ? body : undefined,
});
if (response.ok) {
trackUri
? console.log(`Track ${trackUri} is now playing!`)
: console.log("Playback resumed.");
} else {
console.error("Failed to play/resume track:", await response.text());
if (response.status === 401) {
console.log("Access token expired. Refreshing token...");
const tokenInfo = await readTextFile("./spotify_token.json");
const refreshedToken = await refreshAccessToken(
JSON.parse(tokenInfo).refresh_token,
);
console.log(`New access token: ${refreshedToken}`);
}
}
}
// Function to pause playback
export async function pausePlayback() {
const accessToken = await getAccessToken();
const response = await fetch("https://api.spotify.com/v1/me/player/pause", {
method: "PUT",
headers: { "Authorization": `Bearer ${accessToken}` },
});
if (response.ok) {
console.log("Playback paused.");
} else {
console.error("Failed to pause playback:", await response.text());
}
}