-
-
Save BlankParticle/693a93794478fd82b97c1fcd463836f6 to your computer and use it in GitHub Desktop.
Legacy Implementation of Spot API
This file contains 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 Code used in a SvelteKit Server API route | |
import { SP_DC } from "$env/static/private"; | |
import type { RequestHandler } from "./$types"; | |
type Data = { | |
clientId: string; | |
accessToken: string; | |
accessTokenExpirationTimestampMs: number; | |
isAnonymous: boolean; | |
}; | |
export const GET = (async ({ params }) => { | |
try { | |
if (!process.env.ACCESS_TOKEN || Date.now() >= Number(process.env.ACCESS_TOKEN_EXPIRY)) { | |
const data: Data = await ( | |
await fetch("https://open.spotify.com/get_access_token", { | |
headers: { | |
Cookie: `sp_dc=${SP_DC}`, | |
}, | |
}) | |
).json(); | |
process.env.ACCESS_TOKEN = data.accessToken; | |
process.env.ACCESS_TOKEN_EXPIRY = data.accessTokenExpirationTimestampMs; | |
} | |
const url = `https://spclient.wg.spotify.com/color-lyrics/v2/track/${params.track}?format=json&vocalRemoval=false`; | |
const lyrics = await ( | |
await fetch(url, { | |
headers: { | |
"app-platform": "WebPlayer", | |
authorization: `Bearer ${process.env.ACCESS_TOKEN}`, | |
}, | |
}) | |
).text(); | |
return new Response(lyrics === "" ? JSON.stringify({ error: "Lyrics Not found" }) : lyrics, { | |
headers: { | |
"content-type": "application/json; charset=utf-8", | |
}, | |
}); | |
} catch (e) { | |
console.error(e); | |
return new Response(JSON.stringify({ error: "Unknown Error Occurred!" }), { | |
headers: { | |
"content-type": "application/json; charset=utf-8", | |
}, | |
}); | |
} | |
}) satisfies RequestHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment