This commit is contained in:
nin0 2025-04-25 04:59:33 -04:00
parent 55054e7c9e
commit 182f12f32c
Signed by: nin0
SSH key fingerprint: SHA256:NOoDnFVvZNFvqfXCIhzr6oCTDImZAbTTuyAysZ8Ufk8
4 changed files with 22 additions and 18 deletions

View file

@ -10,7 +10,7 @@
"tsx": "^4.19.3" "tsx": "^4.19.3"
}, },
"scripts": { "scripts": {
"dev": "tsx --watch --env-file-if-exists=.env index.ts", "dev": "tsx --watch --env-file-if-exists=.env src/index.ts",
"build": "node build.mjs", "build": "node build.mjs",
"start": "node build.mjs && node dist/index.js" "start": "node build.mjs && node dist/index.js"
} }

0
src/SpotifyAPI.ts Normal file
View file

View file

@ -1,7 +1,7 @@
import Fastify from "fastify"; import Fastify from "fastify";
import { Yapper } from "./Yapper"; import { Yapper } from "./Yapper";
import path from "path"; import path from "path";
import { readFileSync, writeFileSync } from "fs"; import { readFileSync, stat, writeFileSync } from "fs";
const state = new Proxy( const state = new Proxy(
JSON.parse(readFileSync(path.join(__dirname, "state.json"), "utf-8")), JSON.parse(readFileSync(path.join(__dirname, "state.json"), "utf-8")),
@ -53,24 +53,28 @@ fastify.get("/auth", async (request, reply) => {
fastify.get("/callback", async (request, reply) => { fastify.get("/callback", async (request, reply) => {
if ((request.query as any).error) return reply.code(400).send(); if ((request.query as any).error) return reply.code(400).send();
const tokenRes = await fetch("https://accounts.spotify.com/api/token", { const tokenRes = await (
method: "POST", await fetch("https://accounts.spotify.com/api/token", {
headers: { method: "POST",
"Content-Type": "application/x-www-form-urlencoded", headers: {
Authorization: "Content-Type": "application/x-www-form-urlencoded",
"Basic " + Authorization:
Buffer.from( "Basic " +
process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET Buffer.from(
).toString("base64") process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET
}, ).toString("base64")
body: new URLSearchParams({ },
code: (request.query as any).code, body: new URLSearchParams({
redirect_uri: `https://${request.hostname}/callback`, code: (request.query as any).code,
grant_type: "authorization_code" redirect_uri: `https://${request.hostname}/callback`,
grant_type: "authorization_code"
})
}) })
}); ).json();
console.log(await tokenRes.json()); state.accessToken = tokenRes.access_token;
state.explodesAt = Date.now() + tokenRes.expires_in * 1000;
state.refreshToken = tokenRes.refresh_token;
}); });
try { try {