mirror of
https://github.com/neongamerbot-qk/hackclub-nest
synced 2024-11-22 00:13:39 +00:00
enhancement(lint): Fix lint errors for hackclub-spotify-bot/src/spotify.js
Co-authored-by: NeonGamerBot-QK <saahilattud@gmail.com> Signed-off-by: zeon-neon[bot] <136533918+zeon-neon[bot]@users.noreply.github.com>
This commit is contained in:
parent
7499857a57
commit
0bf03719c2
1 changed files with 199 additions and 196 deletions
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
let token = null;
|
let token = null;
|
||||||
let authStuff = null;
|
let authStuff = null;
|
||||||
const client_id = process.env.SPOTIFY_CLIENT_ID;
|
const client_id = process.env.SPOTIFY_CLIENT_ID;
|
||||||
|
@ -16,9 +15,9 @@ async function fetchWebApi(endpoint, method, body) {
|
||||||
// console.debug(text)
|
// console.debug(text)
|
||||||
// abs nothing is wrong
|
// abs nothing is wrong
|
||||||
return JSON.parse(text.trim());
|
return JSON.parse(text.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLoginUrl() {
|
function getLoginUrl() {
|
||||||
const state = generateRandomString(16);
|
const state = generateRandomString(16);
|
||||||
const scope = [
|
const scope = [
|
||||||
// "ugc-image-upload",
|
// "ugc-image-upload",
|
||||||
|
@ -46,9 +45,9 @@ async function fetchWebApi(endpoint, method, body) {
|
||||||
"https://accounts.spotify.com/authorize?" +
|
"https://accounts.spotify.com/authorize?" +
|
||||||
`response_type=code&grant_type=client_credentials&client_id=${client_id}&scope=${scope}&redirect_uri=${redirect_uri}&state=${state}`
|
`response_type=code&grant_type=client_credentials&client_id=${client_id}&scope=${scope}&redirect_uri=${redirect_uri}&state=${state}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateRandomString(length) {
|
function generateRandomString(length) {
|
||||||
let result = "";
|
let result = "";
|
||||||
const characters =
|
const characters =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
@ -59,8 +58,8 @@ async function fetchWebApi(endpoint, method, body) {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
async function refreshToken(refresh_token) {
|
async function refreshToken(refresh_token) {
|
||||||
try {
|
try {
|
||||||
// var refresh_token = req.query.refresh_token;
|
// var refresh_token = req.query.refresh_token;
|
||||||
const authOptions = {
|
const authOptions = {
|
||||||
|
@ -109,20 +108,24 @@ async function fetchWebApi(endpoint, method, body) {
|
||||||
// try again asap because we NEED THAT TOKEN
|
// try again asap because we NEED THAT TOKEN
|
||||||
refreshToken(refresh_token);
|
refreshToken(refresh_token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function saveCredentials(creds) {
|
function saveCredentials(creds) {
|
||||||
require('fs').writeFileSync('data/credentials.json', JSON.stringify(creds, null, 2));
|
require("fs").writeFileSync(
|
||||||
|
"data/credentials.json",
|
||||||
|
JSON.stringify(creds, null, 2),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
function getCredentials() {
|
function getCredentials() {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(require('fs').readFileSync('data/credentials.json', 'utf8'));
|
return JSON.parse(
|
||||||
|
require("fs").readFileSync("data/credentials.json", "utf8"),
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
function spotifyRoutes(app) {
|
function spotifyRoutes(app) {
|
||||||
app.get('/spotify/callback', async (req,res) => {
|
app.get("/spotify/callback", async (req, res) => {
|
||||||
const code = req.query.code || null;
|
const code = req.query.code || null;
|
||||||
const state = req.query.state || null;
|
const state = req.query.state || null;
|
||||||
|
|
||||||
|
@ -166,7 +169,7 @@ function spotifyRoutes(app) {
|
||||||
.then((auth) => {
|
.then((auth) => {
|
||||||
// console.log(auth);
|
// console.log(auth);
|
||||||
authStuff = auth;
|
authStuff = auth;
|
||||||
saveCredentials(auth)
|
saveCredentials(auth);
|
||||||
token = auth.access_token;
|
token = auth.access_token;
|
||||||
if (auth.expires_in) {
|
if (auth.expires_in) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -174,23 +177,23 @@ function spotifyRoutes(app) {
|
||||||
}, auth.expires_in * 1000);
|
}, auth.expires_in * 1000);
|
||||||
}
|
}
|
||||||
res.status(200).end("Successfully logged in!");
|
res.status(200).end("Successfully logged in!");
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
function addSongToPlaylist(url) {
|
function addSongToPlaylist(url) {
|
||||||
fetchWebApi('v1/playlists/3gRv97fvllFFLVdCH6XzsE/tracks', 'POST', {
|
fetchWebApi("v1/playlists/3gRv97fvllFFLVdCH6XzsE/tracks", "POST", {
|
||||||
uris: [url],
|
uris: [url],
|
||||||
position: 0,
|
position: 0,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getLoginUrl,
|
getLoginUrl,
|
||||||
refreshToken,
|
refreshToken,
|
||||||
saveCredentials,
|
saveCredentials,
|
||||||
getCredentials,
|
getCredentials,
|
||||||
spotifyRoutes,
|
spotifyRoutes,
|
||||||
addSongToPlaylist
|
addSongToPlaylist,
|
||||||
|
|
||||||
// getToken
|
// getToken
|
||||||
}
|
};
|
||||||
|
|
Loading…
Reference in a new issue