Last active
December 12, 2023 11:48
-
-
Save majames/f3f69a1637c13f0cafa339159343b0c2 to your computer and use it in GitHub Desktop.
useful script for getting all of the songs in a spotify playlist that were released in 2022 and 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This script can be executed via bun.sh: https://bun.sh/ | |
* | |
* bun get-2023-songs.ts | |
*/ | |
import fs from "fs"; | |
import axios from "axios"; | |
// auth token can be retrieved by visiting the spotify API docs (linked below) | |
const AUTH_TOKEN = "xxxx"; | |
// can be retrieved from the share link URL of a playlist | |
const PLAYLIST_ID = "69TYgvNeCR797uHYe0xzDG" | |
interface Track { | |
name: string; | |
album: { release_date: string }; | |
} | |
const tracks: Track[] = []; | |
// https://developer.spotify.com/documentation/web-api/reference/get-playlists-tracks | |
let apiUrl: string | undefined = | |
`https://api.spotify.com/v1/playlists/${PLAYLIST_ID}/tracks?limit=50&offset=0&fields=(next,items.track(name,album(release_date),artists(name)))`; | |
while (apiUrl) { | |
const res = await axios.get<{ | |
items: { track: Track }[]; | |
next?: string; | |
}>(apiUrl, { | |
headers: { | |
Authorization: AUTH_TOKEN, | |
}, | |
}); | |
const { items, next } = res.data; | |
if (items.length > 0) { | |
tracks.push(...items.map(({ track }) => track)); | |
} | |
apiUrl = next; | |
} | |
fs.writeFileSync("./tracks.json", JSON.stringify(tracks, null, 2)); | |
fs.writeFileSync( | |
"./tracks-2023.json", | |
JSON.stringify( | |
tracks.filter(({ album: { release_date } }) => | |
release_date.startsWith("2023") | |
), | |
null, | |
2 | |
) | |
); | |
fs.writeFileSync( | |
"./tracks-2022.json", | |
JSON.stringify( | |
tracks.filter(({ album: { release_date } }) => | |
release_date.startsWith("2022") | |
), | |
null, | |
2 | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment