Basic Spotify

This commit is contained in:
killer069 2021-09-09 09:25:21 +05:30
parent b55f0ad3d3
commit 139b540a2c
2 changed files with 12 additions and 17 deletions

View File

@ -4,6 +4,9 @@ import readline from 'readline'
import fs from 'fs' import fs from 'fs'
var spotifyData : SpotifyDataOptions; var spotifyData : SpotifyDataOptions;
if(fs.existsSync('.data/spotify.data')){
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString())
}
interface SpotifyDataOptions{ interface SpotifyDataOptions{
client_id : string; client_id : string;
@ -21,6 +24,7 @@ interface SpotifyDataOptions{
const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\// const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//
export async function spotify(url : string): Promise<SpotifyAlbum | SpotifyPlaylist | SpotifyVideo>{ export async function spotify(url : string): Promise<SpotifyAlbum | SpotifyPlaylist | SpotifyVideo>{
if(!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?')
if(!url.match(pattern)) throw new Error('This is not a Spotify URL') if(!url.match(pattern)) throw new Error('This is not a Spotify URL')
if(url.indexOf('track/') !== -1){ if(url.indexOf('track/') !== -1){
let trackID = url.split('track/')[1].split('&')[0].split('?')[0] let trackID = url.split('track/')[1].split('&')[0].split('?')[0]
@ -91,7 +95,7 @@ export function Authorization(){
} }
console.log('\nNow Go to your browser and Paste this url. Authroize it and paste the redirected url here. \n') console.log('\nNow Go to your browser and Paste this url. Authroize it and paste the redirected url here. \n')
console.log(`https://accounts.spotify.com/authorize?client_id=${client_id}&response_type=code&redirect_uri=${encodeURI(redirect_url)} \n`) console.log(`https://accounts.spotify.com/authorize?client_id=${client_id}&response_type=code&redirect_uri=${encodeURI(redirect_url)} \n`)
ask.question('Redirected URL : ', (url) => { ask.question('Redirected URL : ',async (url) => {
if (!fs.existsSync('.data')) fs.mkdirSync('.data') if (!fs.existsSync('.data')) fs.mkdirSync('.data')
spotifyData = { spotifyData = {
client_id, client_id,
@ -100,7 +104,8 @@ export function Authorization(){
authorization_code : url.split('code=')[1], authorization_code : url.split('code=')[1],
market market
} }
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4)) let check = await SpotifyAuthorize(spotifyData)
if(check === false) throw new Error('Failed to get access Token.')
ask.close() ask.close()
}) })
}) })
@ -109,19 +114,7 @@ export function Authorization(){
}) })
} }
export async function StartSpotify(){ async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<boolean>{
if(!fs.existsSync('.data/spotify.data')) throw new Error('Spotify Data is Missing\nDid you forgot to do authorization ?')
if(!spotifyData) spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString())
if(spotifyData.authorization_code) {
let check = await SpotifyAuthorize(spotifyData)
if(check !== false) spotifyData = check
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4))
}
}
async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<SpotifyDataOptions | false>{
let response = await got.post(`https://accounts.spotify.com/api/token?grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(data.redirect_url)}`, { let response = await got.post(`https://accounts.spotify.com/api/token?grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(data.redirect_url)}`, {
headers : { headers : {
"Authorization" : `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`, "Authorization" : `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`,
@ -133,7 +126,7 @@ async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<SpotifyDataO
if(typeof response === 'number') return false if(typeof response === 'number') return false
let resp_json = JSON.parse(response.body) let resp_json = JSON.parse(response.body)
return{ spotifyData = {
client_id : data.client_id, client_id : data.client_id,
client_secret : data.client_secret, client_secret : data.client_secret,
redirect_url : data.redirect_url, redirect_url : data.redirect_url,
@ -144,6 +137,8 @@ async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<SpotifyDataO
token_type : resp_json.token_type, token_type : resp_json.token_type,
market : data.market market : data.market
} }
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4))
return true
} }
export function is_expired(){ export function is_expired(){

View File

@ -1,6 +1,6 @@
export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, yt_validate, extractID } from "./YouTube"; export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, yt_validate, extractID } from "./YouTube";
export { spotify, sp_validate, Authorization, StartSpotify, RefreshToken, is_expired } from './Spotify' export { spotify, sp_validate, Authorization, RefreshToken, is_expired } from './Spotify'
import { sp_validate, yt_validate } from "."; import { sp_validate, yt_validate } from ".";