Skip to content

Instantly share code, notes, and snippets.

@BlankParticle
Created June 12, 2023 04:20
Show Gist options
  • Save BlankParticle/693a93794478fd82b97c1fcd463836f6 to your computer and use it in GitHub Desktop.
Save BlankParticle/693a93794478fd82b97c1fcd463836f6 to your computer and use it in GitHub Desktop.
Legacy Implementation of Spot API
// 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