mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
SoundCloud work + prettier
This commit is contained in:
@@ -1,315 +1,328 @@
|
||||
import { request } from "../YouTube/utils/request";
|
||||
import { SpotifyDataOptions } from ".";
|
||||
import { request } from '../YouTube/utils/request';
|
||||
import { SpotifyDataOptions } from '.';
|
||||
|
||||
|
||||
interface SpotifyTrackAlbum{
|
||||
name : string;
|
||||
url : string;
|
||||
id : string;
|
||||
release_date : string;
|
||||
release_date_precision : string;
|
||||
total_tracks : number;
|
||||
interface SpotifyTrackAlbum {
|
||||
name: string;
|
||||
url: string;
|
||||
id: string;
|
||||
release_date: string;
|
||||
release_date_precision: string;
|
||||
total_tracks: number;
|
||||
}
|
||||
|
||||
interface SpotifyArtists{
|
||||
name : string;
|
||||
url : string;
|
||||
id : string;
|
||||
interface SpotifyArtists {
|
||||
name: string;
|
||||
url: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface SpotifyThumbnail{
|
||||
height : number;
|
||||
width : number
|
||||
url : string
|
||||
interface SpotifyThumbnail {
|
||||
height: number;
|
||||
width: number;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface SpotifyCopyright{
|
||||
text : string;
|
||||
type : string;
|
||||
interface SpotifyCopyright {
|
||||
text: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export class SpotifyVideo{
|
||||
name : string;
|
||||
type : "track" | "playlist" | "album"
|
||||
id : string;
|
||||
url : string;
|
||||
explicit : boolean;
|
||||
durationInSec : number;
|
||||
durationInMs : number;
|
||||
artists : SpotifyArtists[]
|
||||
album : SpotifyTrackAlbum
|
||||
thumbnail : SpotifyThumbnail
|
||||
constructor(data : any){
|
||||
this.name = data.name
|
||||
this.id = data.id
|
||||
this.type = "track"
|
||||
this.url = data.external_urls.spotify
|
||||
this.explicit = data.explicit
|
||||
this.durationInMs = data.duration_ms
|
||||
this.durationInSec = Math.round(this.durationInMs/1000)
|
||||
let artists : SpotifyArtists[] = []
|
||||
data.artists.forEach((v : any) => {
|
||||
export class SpotifyVideo {
|
||||
name: string;
|
||||
type: 'track' | 'playlist' | 'album';
|
||||
id: string;
|
||||
url: string;
|
||||
explicit: boolean;
|
||||
durationInSec: number;
|
||||
durationInMs: number;
|
||||
artists: SpotifyArtists[];
|
||||
album: SpotifyTrackAlbum;
|
||||
thumbnail: SpotifyThumbnail;
|
||||
constructor(data: any) {
|
||||
this.name = data.name;
|
||||
this.id = data.id;
|
||||
this.type = 'track';
|
||||
this.url = data.external_urls.spotify;
|
||||
this.explicit = data.explicit;
|
||||
this.durationInMs = data.duration_ms;
|
||||
this.durationInSec = Math.round(this.durationInMs / 1000);
|
||||
const artists: SpotifyArtists[] = [];
|
||||
data.artists.forEach((v: any) => {
|
||||
artists.push({
|
||||
name : v.name,
|
||||
id : v.id,
|
||||
url : v.external_urls.spotify
|
||||
})
|
||||
})
|
||||
this.artists = artists
|
||||
name: v.name,
|
||||
id: v.id,
|
||||
url: v.external_urls.spotify
|
||||
});
|
||||
});
|
||||
this.artists = artists;
|
||||
this.album = {
|
||||
name : data.album.name,
|
||||
url : data.external_urls.spotify,
|
||||
id : data.album.id,
|
||||
release_date : data.album.release_date,
|
||||
release_date_precision : data.album.release_date_precision,
|
||||
total_tracks : data.album.total_tracks
|
||||
}
|
||||
this.thumbnail = data.album.images[0]
|
||||
name: data.album.name,
|
||||
url: data.external_urls.spotify,
|
||||
id: data.album.id,
|
||||
release_date: data.album.release_date,
|
||||
release_date_precision: data.album.release_date_precision,
|
||||
total_tracks: data.album.total_tracks
|
||||
};
|
||||
this.thumbnail = data.album.images[0];
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
toJSON() {
|
||||
return {
|
||||
name : this.name,
|
||||
id : this.id,
|
||||
type : this.type,
|
||||
url : this.url,
|
||||
explicit : this.explicit,
|
||||
durationInMs : this.durationInMs,
|
||||
durationInSec : this.durationInSec,
|
||||
artists : this.artists,
|
||||
album : this.album,
|
||||
thumbnail : this.thumbnail
|
||||
}
|
||||
name: this.name,
|
||||
id: this.id,
|
||||
type: this.type,
|
||||
url: this.url,
|
||||
explicit: this.explicit,
|
||||
durationInMs: this.durationInMs,
|
||||
durationInSec: this.durationInSec,
|
||||
artists: this.artists,
|
||||
album: this.album,
|
||||
thumbnail: this.thumbnail
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class SpotifyPlaylist{
|
||||
name : string;
|
||||
type : "track" | "playlist" | "album"
|
||||
collaborative : boolean;
|
||||
description : string;
|
||||
url : string;
|
||||
id : string;
|
||||
thumbnail : SpotifyThumbnail;
|
||||
owner : SpotifyArtists;
|
||||
tracksCount : number;
|
||||
private spotifyData : SpotifyDataOptions;
|
||||
private fetched_tracks : Map<string, SpotifyVideo[]>
|
||||
constructor(data : any, spotifyData : SpotifyDataOptions){
|
||||
this.name = data.name
|
||||
this.type = "playlist"
|
||||
this.collaborative = data.collaborative
|
||||
this.description = data.description
|
||||
this.url = data.external_urls.spotify
|
||||
this.id = data.id
|
||||
this.thumbnail = data.images[0]
|
||||
export class SpotifyPlaylist {
|
||||
name: string;
|
||||
type: 'track' | 'playlist' | 'album';
|
||||
collaborative: boolean;
|
||||
description: string;
|
||||
url: string;
|
||||
id: string;
|
||||
thumbnail: SpotifyThumbnail;
|
||||
owner: SpotifyArtists;
|
||||
tracksCount: number;
|
||||
private spotifyData: SpotifyDataOptions;
|
||||
private fetched_tracks: Map<string, SpotifyVideo[]>;
|
||||
constructor(data: any, spotifyData: SpotifyDataOptions) {
|
||||
this.name = data.name;
|
||||
this.type = 'playlist';
|
||||
this.collaborative = data.collaborative;
|
||||
this.description = data.description;
|
||||
this.url = data.external_urls.spotify;
|
||||
this.id = data.id;
|
||||
this.thumbnail = data.images[0];
|
||||
this.owner = {
|
||||
name : data.owner.display_name,
|
||||
url : data.owner.external_urls.spotify,
|
||||
id : data.owner.id
|
||||
}
|
||||
this.tracksCount = Number(data.tracks.total)
|
||||
let videos: SpotifyVideo[] = []
|
||||
data.tracks.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyVideo(v.track))
|
||||
})
|
||||
this.fetched_tracks = new Map()
|
||||
this.fetched_tracks.set('1', videos)
|
||||
this.spotifyData = spotifyData
|
||||
name: data.owner.display_name,
|
||||
url: data.owner.external_urls.spotify,
|
||||
id: data.owner.id
|
||||
};
|
||||
this.tracksCount = Number(data.tracks.total);
|
||||
const videos: SpotifyVideo[] = [];
|
||||
data.tracks.items.forEach((v: any) => {
|
||||
videos.push(new SpotifyVideo(v.track));
|
||||
});
|
||||
this.fetched_tracks = new Map();
|
||||
this.fetched_tracks.set('1', videos);
|
||||
this.spotifyData = spotifyData;
|
||||
}
|
||||
|
||||
async fetch(){
|
||||
let fetching : number;
|
||||
if(this.tracksCount > 1000) fetching = 1000
|
||||
else fetching = this.tracksCount
|
||||
if(fetching <= 100) return
|
||||
let work = []
|
||||
for(let i = 2; i <= Math.ceil(fetching/100); i++){
|
||||
work.push(new Promise(async (resolve, reject) => {
|
||||
let response = await request(`https://api.spotify.com/v1/playlists/${this.id}/tracks?offset=${(i-1)*100}&limit=100&market=${this.spotifyData.market}`, {
|
||||
headers : {
|
||||
"Authorization" : `${this.spotifyData.token_type} ${this.spotifyData.access_token}`
|
||||
}
|
||||
}).catch((err) => reject(`Response Error : \n${err}`))
|
||||
let videos: SpotifyVideo[] = []
|
||||
if(typeof response !== 'string') return
|
||||
let json_data = JSON.parse(response)
|
||||
json_data.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyVideo(v.track))
|
||||
async fetch() {
|
||||
let fetching: number;
|
||||
if (this.tracksCount > 1000) fetching = 1000;
|
||||
else fetching = this.tracksCount;
|
||||
if (fetching <= 100) return;
|
||||
const work = [];
|
||||
for (let i = 2; i <= Math.ceil(fetching / 100); i++) {
|
||||
work.push(
|
||||
new Promise(async (resolve, reject) => {
|
||||
const response = await request(
|
||||
`https://api.spotify.com/v1/playlists/${this.id}/tracks?offset=${
|
||||
(i - 1) * 100
|
||||
}&limit=100&market=${this.spotifyData.market}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${this.spotifyData.token_type} ${this.spotifyData.access_token}`
|
||||
}
|
||||
}
|
||||
).catch((err) => reject(`Response Error : \n${err}`));
|
||||
const videos: SpotifyVideo[] = [];
|
||||
if (typeof response !== 'string') return;
|
||||
const json_data = JSON.parse(response);
|
||||
json_data.items.forEach((v: any) => {
|
||||
videos.push(new SpotifyVideo(v.track));
|
||||
});
|
||||
this.fetched_tracks.set(`${i}`, videos);
|
||||
resolve('Success');
|
||||
})
|
||||
this.fetched_tracks.set(`${i}`, videos)
|
||||
resolve('Success')
|
||||
}))
|
||||
);
|
||||
}
|
||||
await Promise.allSettled(work)
|
||||
return this
|
||||
await Promise.allSettled(work);
|
||||
return this;
|
||||
}
|
||||
|
||||
page(num : number){
|
||||
if(!num) throw new Error('Page number is not provided')
|
||||
if(!this.fetched_tracks.has(`${num}`)) throw new Error('Given Page number is invalid')
|
||||
return this.fetched_tracks.get(`${num}`)
|
||||
page(num: number) {
|
||||
if (!num) throw new Error('Page number is not provided');
|
||||
if (!this.fetched_tracks.has(`${num}`)) throw new Error('Given Page number is invalid');
|
||||
return this.fetched_tracks.get(`${num}`);
|
||||
}
|
||||
|
||||
get total_pages(){
|
||||
return this.fetched_tracks.size
|
||||
get total_pages() {
|
||||
return this.fetched_tracks.size;
|
||||
}
|
||||
|
||||
get total_tracks(){
|
||||
let page_number: number = this.total_pages
|
||||
return (page_number - 1) * 100 + (this.fetched_tracks.get(`page${page_number}`) as SpotifyVideo[]).length
|
||||
get total_tracks() {
|
||||
const page_number: number = this.total_pages;
|
||||
return (page_number - 1) * 100 + (this.fetched_tracks.get(`page${page_number}`) as SpotifyVideo[]).length;
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
toJSON() {
|
||||
return {
|
||||
name : this.name,
|
||||
type : this.type,
|
||||
collaborative : this.collaborative,
|
||||
description : this.description,
|
||||
url : this.url,
|
||||
id : this.id,
|
||||
thumbnail : this.thumbnail,
|
||||
owner : this.owner,
|
||||
}
|
||||
name: this.name,
|
||||
type: this.type,
|
||||
collaborative: this.collaborative,
|
||||
description: this.description,
|
||||
url: this.url,
|
||||
id: this.id,
|
||||
thumbnail: this.thumbnail,
|
||||
owner: this.owner
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class SpotifyAlbum{
|
||||
name : string
|
||||
type : "track" | "playlist" | "album"
|
||||
url : string
|
||||
id : string;
|
||||
thumbnail : SpotifyThumbnail
|
||||
artists : SpotifyArtists[]
|
||||
copyrights : SpotifyCopyright[]
|
||||
release_date : string;
|
||||
release_date_precision : string;
|
||||
trackCount : number
|
||||
private spotifyData : SpotifyDataOptions;
|
||||
private fetched_tracks : Map<string, SpotifyTracks[]>
|
||||
constructor(data : any, spotifyData : SpotifyDataOptions){
|
||||
this.name = data.name
|
||||
this.type = "album"
|
||||
this.id = data.id
|
||||
this.url = data.external_urls.spotify
|
||||
this.thumbnail = data.images[0]
|
||||
let artists : SpotifyArtists[] = []
|
||||
data.artists.forEach((v : any) => {
|
||||
export class SpotifyAlbum {
|
||||
name: string;
|
||||
type: 'track' | 'playlist' | 'album';
|
||||
url: string;
|
||||
id: string;
|
||||
thumbnail: SpotifyThumbnail;
|
||||
artists: SpotifyArtists[];
|
||||
copyrights: SpotifyCopyright[];
|
||||
release_date: string;
|
||||
release_date_precision: string;
|
||||
trackCount: number;
|
||||
private spotifyData: SpotifyDataOptions;
|
||||
private fetched_tracks: Map<string, SpotifyTracks[]>;
|
||||
constructor(data: any, spotifyData: SpotifyDataOptions) {
|
||||
this.name = data.name;
|
||||
this.type = 'album';
|
||||
this.id = data.id;
|
||||
this.url = data.external_urls.spotify;
|
||||
this.thumbnail = data.images[0];
|
||||
const artists: SpotifyArtists[] = [];
|
||||
data.artists.forEach((v: any) => {
|
||||
artists.push({
|
||||
name : v.name,
|
||||
id : v.id,
|
||||
url : v.external_urls.spotify
|
||||
})
|
||||
})
|
||||
this.artists = artists
|
||||
this.copyrights = data.copyrights
|
||||
this.release_date = data.release_date
|
||||
this.release_date_precision = data.release_date_precision
|
||||
this.trackCount = data.total_tracks
|
||||
let videos: SpotifyTracks[] = []
|
||||
data.tracks.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyTracks(v))
|
||||
})
|
||||
this.fetched_tracks = new Map()
|
||||
this.fetched_tracks.set('1', videos)
|
||||
this.spotifyData = spotifyData
|
||||
name: v.name,
|
||||
id: v.id,
|
||||
url: v.external_urls.spotify
|
||||
});
|
||||
});
|
||||
this.artists = artists;
|
||||
this.copyrights = data.copyrights;
|
||||
this.release_date = data.release_date;
|
||||
this.release_date_precision = data.release_date_precision;
|
||||
this.trackCount = data.total_tracks;
|
||||
const videos: SpotifyTracks[] = [];
|
||||
data.tracks.items.forEach((v: any) => {
|
||||
videos.push(new SpotifyTracks(v));
|
||||
});
|
||||
this.fetched_tracks = new Map();
|
||||
this.fetched_tracks.set('1', videos);
|
||||
this.spotifyData = spotifyData;
|
||||
}
|
||||
|
||||
async fetch(){
|
||||
let fetching : number;
|
||||
if(this.trackCount > 500) fetching = 500
|
||||
else fetching = this.trackCount
|
||||
if(fetching <= 50) return
|
||||
let work = []
|
||||
for(let i = 2; i <= Math.ceil(fetching/50); i++){
|
||||
work.push(new Promise(async (resolve, reject) => {
|
||||
let response = await request(`https://api.spotify.com/v1/albums/${this.id}/tracks?offset=${(i-1)*50}&limit=50&market=${this.spotifyData.market}`, {
|
||||
headers : {
|
||||
"Authorization" : `${this.spotifyData.token_type} ${this.spotifyData.access_token}`
|
||||
}
|
||||
}).catch((err) => reject(`Response Error : \n${err}`))
|
||||
let videos: SpotifyTracks[] = []
|
||||
if(typeof response !== 'string') return
|
||||
let json_data = JSON.parse(response)
|
||||
json_data.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyTracks(v))
|
||||
async fetch() {
|
||||
let fetching: number;
|
||||
if (this.trackCount > 500) fetching = 500;
|
||||
else fetching = this.trackCount;
|
||||
if (fetching <= 50) return;
|
||||
const work = [];
|
||||
for (let i = 2; i <= Math.ceil(fetching / 50); i++) {
|
||||
work.push(
|
||||
new Promise(async (resolve, reject) => {
|
||||
const response = await request(
|
||||
`https://api.spotify.com/v1/albums/${this.id}/tracks?offset=${(i - 1) * 50}&limit=50&market=${
|
||||
this.spotifyData.market
|
||||
}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${this.spotifyData.token_type} ${this.spotifyData.access_token}`
|
||||
}
|
||||
}
|
||||
).catch((err) => reject(`Response Error : \n${err}`));
|
||||
const videos: SpotifyTracks[] = [];
|
||||
if (typeof response !== 'string') return;
|
||||
const json_data = JSON.parse(response);
|
||||
json_data.items.forEach((v: any) => {
|
||||
videos.push(new SpotifyTracks(v));
|
||||
});
|
||||
this.fetched_tracks.set(`${i}`, videos);
|
||||
resolve('Success');
|
||||
})
|
||||
this.fetched_tracks.set(`${i}`, videos)
|
||||
resolve('Success')
|
||||
}))
|
||||
);
|
||||
}
|
||||
await Promise.allSettled(work)
|
||||
return this
|
||||
await Promise.allSettled(work);
|
||||
return this;
|
||||
}
|
||||
|
||||
page(num : number){
|
||||
if(!num) throw new Error('Page number is not provided')
|
||||
if(!this.fetched_tracks.has(`${num}`)) throw new Error('Given Page number is invalid')
|
||||
return this.fetched_tracks.get(`${num}`)
|
||||
page(num: number) {
|
||||
if (!num) throw new Error('Page number is not provided');
|
||||
if (!this.fetched_tracks.has(`${num}`)) throw new Error('Given Page number is invalid');
|
||||
return this.fetched_tracks.get(`${num}`);
|
||||
}
|
||||
|
||||
get total_pages(){
|
||||
return this.fetched_tracks.size
|
||||
get total_pages() {
|
||||
return this.fetched_tracks.size;
|
||||
}
|
||||
|
||||
get total_tracks(){
|
||||
let page_number: number = this.total_pages
|
||||
return (page_number - 1) * 100 + (this.fetched_tracks.get(`page${page_number}`) as SpotifyVideo[]).length
|
||||
get total_tracks() {
|
||||
const page_number: number = this.total_pages;
|
||||
return (page_number - 1) * 100 + (this.fetched_tracks.get(`page${page_number}`) as SpotifyVideo[]).length;
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
toJSON() {
|
||||
return {
|
||||
name : this.name,
|
||||
type : this.type,
|
||||
url : this.url,
|
||||
thumbnail : this.thumbnail,
|
||||
artists : this.artists,
|
||||
copyrights : this.copyrights,
|
||||
release_date : this.release_date,
|
||||
release_date_precision : this.release_date_precision,
|
||||
total_tracks : this.total_tracks,
|
||||
}
|
||||
name: this.name,
|
||||
type: this.type,
|
||||
url: this.url,
|
||||
thumbnail: this.thumbnail,
|
||||
artists: this.artists,
|
||||
copyrights: this.copyrights,
|
||||
release_date: this.release_date,
|
||||
release_date_precision: this.release_date_precision,
|
||||
total_tracks: this.total_tracks
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class SpotifyTracks{
|
||||
name : string;
|
||||
type : "track" | "playlist" | "album"
|
||||
id : string;
|
||||
url : string;
|
||||
explicit : boolean;
|
||||
durationInSec : number;
|
||||
durationInMs : number;
|
||||
artists : SpotifyArtists[]
|
||||
constructor(data : any){
|
||||
this.name = data.name
|
||||
this.id = data.id
|
||||
this.type = "track"
|
||||
this.url = data.external_urls.spotify
|
||||
this.explicit = data.explicit
|
||||
this.durationInMs = data.duration_ms
|
||||
this.durationInSec = Math.round(this.durationInMs/1000)
|
||||
let artists : SpotifyArtists[] = []
|
||||
data.artists.forEach((v : any) => {
|
||||
class SpotifyTracks {
|
||||
name: string;
|
||||
type: 'track' | 'playlist' | 'album';
|
||||
id: string;
|
||||
url: string;
|
||||
explicit: boolean;
|
||||
durationInSec: number;
|
||||
durationInMs: number;
|
||||
artists: SpotifyArtists[];
|
||||
constructor(data: any) {
|
||||
this.name = data.name;
|
||||
this.id = data.id;
|
||||
this.type = 'track';
|
||||
this.url = data.external_urls.spotify;
|
||||
this.explicit = data.explicit;
|
||||
this.durationInMs = data.duration_ms;
|
||||
this.durationInSec = Math.round(this.durationInMs / 1000);
|
||||
const artists: SpotifyArtists[] = [];
|
||||
data.artists.forEach((v: any) => {
|
||||
artists.push({
|
||||
name : v.name,
|
||||
id : v.id,
|
||||
url : v.external_urls.spotify
|
||||
})
|
||||
})
|
||||
this.artists = artists
|
||||
name: v.name,
|
||||
id: v.id,
|
||||
url: v.external_urls.spotify
|
||||
});
|
||||
});
|
||||
this.artists = artists;
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
toJSON() {
|
||||
return {
|
||||
name : this.name,
|
||||
id : this.id,
|
||||
type : this.type,
|
||||
url : this.url,
|
||||
explicit : this.explicit,
|
||||
durationInMs : this.durationInMs,
|
||||
durationInSec : this.durationInSec,
|
||||
artists : this.artists,
|
||||
}
|
||||
name: this.name,
|
||||
id: this.id,
|
||||
type: this.type,
|
||||
url: this.url,
|
||||
explicit: this.explicit,
|
||||
durationInMs: this.durationInMs,
|
||||
durationInSec: this.durationInSec,
|
||||
artists: this.artists
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,171 +1,134 @@
|
||||
import { request } from "../YouTube/utils/request";
|
||||
import { SpotifyAlbum, SpotifyPlaylist, SpotifyVideo } from "./classes"
|
||||
import readline from 'readline'
|
||||
import fs from 'fs'
|
||||
import { request } from '../YouTube/utils/request';
|
||||
import { SpotifyAlbum, SpotifyPlaylist, SpotifyVideo } from './classes';
|
||||
import fs from 'fs';
|
||||
|
||||
var spotifyData : SpotifyDataOptions;
|
||||
if(fs.existsSync('.data/spotify.data')){
|
||||
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString())
|
||||
let spotifyData: SpotifyDataOptions;
|
||||
if (fs.existsSync('.data/spotify.data')) {
|
||||
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString());
|
||||
}
|
||||
|
||||
export interface SpotifyDataOptions{
|
||||
client_id : string;
|
||||
client_secret : string;
|
||||
redirect_url : string;
|
||||
authorization_code? :string;
|
||||
access_token? : string;
|
||||
refresh_token? : string;
|
||||
token_type? : string;
|
||||
expires_in? : number;
|
||||
expiry? : number;
|
||||
market? : string;
|
||||
export interface SpotifyDataOptions {
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
redirect_url: string;
|
||||
authorization_code?: string;
|
||||
access_token?: string;
|
||||
refresh_token?: string;
|
||||
token_type?: string;
|
||||
expires_in?: number;
|
||||
expiry?: number;
|
||||
market?: string;
|
||||
}
|
||||
|
||||
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>{
|
||||
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.indexOf('track/') !== -1){
|
||||
let trackID = url.split('track/')[1].split('&')[0].split('?')[0]
|
||||
let response = await request(`https://api.spotify.com/v1/tracks/${trackID}?market=${spotifyData.market}`, {
|
||||
headers : {
|
||||
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
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.indexOf('track/') !== -1) {
|
||||
const trackID = url.split('track/')[1].split('&')[0].split('?')[0];
|
||||
const response = await request(`https://api.spotify.com/v1/tracks/${trackID}?market=${spotifyData.market}`, {
|
||||
headers: {
|
||||
Authorization: `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
}
|
||||
}).catch((err) => {return 0})
|
||||
if(typeof response !== 'number') return new SpotifyVideo(JSON.parse(response))
|
||||
else throw new Error('Failed to get spotify Track Data')
|
||||
}
|
||||
else if(url.indexOf('album/') !== -1){
|
||||
let albumID = url.split('album/')[1].split('&')[0].split('?')[0]
|
||||
let response = await request(`https://api.spotify.com/v1/albums/${albumID}?market=${spotifyData.market}`, {
|
||||
headers : {
|
||||
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
}).catch((err: Error) => {
|
||||
return err;
|
||||
});
|
||||
if (response instanceof Error) throw response;
|
||||
return new SpotifyVideo(JSON.parse(response));
|
||||
} else if (url.indexOf('album/') !== -1) {
|
||||
const albumID = url.split('album/')[1].split('&')[0].split('?')[0];
|
||||
const response = await request(`https://api.spotify.com/v1/albums/${albumID}?market=${spotifyData.market}`, {
|
||||
headers: {
|
||||
Authorization: `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
}
|
||||
}).catch((err) => {return 0})
|
||||
if(typeof response !== 'number') return new SpotifyAlbum(JSON.parse(response), spotifyData)
|
||||
else throw new Error('Failed to get spotify Album Data')
|
||||
}
|
||||
else if(url.indexOf('playlist/') !== -1){
|
||||
let playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0]
|
||||
let response = await request(`https://api.spotify.com/v1/playlists/${playlistID}?market=${spotifyData.market}`, {
|
||||
headers : {
|
||||
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
}).catch((err: Error) => {
|
||||
return err;
|
||||
});
|
||||
if (response instanceof Error) throw response;
|
||||
return new SpotifyAlbum(JSON.parse(response), spotifyData);
|
||||
} else if (url.indexOf('playlist/') !== -1) {
|
||||
const playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0];
|
||||
const response = await request(
|
||||
`https://api.spotify.com/v1/playlists/${playlistID}?market=${spotifyData.market}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
}
|
||||
}
|
||||
}).catch((err) => {return 0})
|
||||
if(typeof response !== 'number') return new SpotifyPlaylist(JSON.parse(response), spotifyData)
|
||||
else throw new Error('Failed to get spotify Playlist Data')
|
||||
}
|
||||
else throw new Error('URL is out of scope for play-dl.')
|
||||
).catch((err: Error) => {
|
||||
return err;
|
||||
});
|
||||
if (response instanceof Error) throw response;
|
||||
return new SpotifyPlaylist(JSON.parse(response), spotifyData);
|
||||
} else throw new Error('URL is out of scope for play-dl.');
|
||||
}
|
||||
|
||||
export function sp_validate(url : string): "track" | "playlist" | "album" | boolean{
|
||||
if(!url.match(pattern)) return false
|
||||
if(url.indexOf('track/') !== -1){
|
||||
return "track"
|
||||
}
|
||||
else if(url.indexOf('album/') !== -1){
|
||||
return "album"
|
||||
}
|
||||
else if(url.indexOf('playlist/') !== -1){
|
||||
return "playlist"
|
||||
}
|
||||
else return false
|
||||
export function sp_validate(url: string): 'track' | 'playlist' | 'album' | boolean {
|
||||
if (!url.match(pattern)) return false;
|
||||
if (url.indexOf('track/') !== -1) {
|
||||
return 'track';
|
||||
} else if (url.indexOf('album/') !== -1) {
|
||||
return 'album';
|
||||
} else if (url.indexOf('playlist/') !== -1) {
|
||||
return 'playlist';
|
||||
} else return false;
|
||||
}
|
||||
|
||||
export function Authorization(){
|
||||
let ask = readline.createInterface({
|
||||
input : process.stdin,
|
||||
output : process.stdout
|
||||
})
|
||||
|
||||
let client_id : string, client_secret : string, redirect_url : string, market : string;
|
||||
ask.question('Client ID : ', (id) => {
|
||||
client_id = id
|
||||
ask.question('Client Secret : ', (secret) => {
|
||||
client_secret = secret
|
||||
ask.question('Redirect URL : ', (url) => {
|
||||
redirect_url = url
|
||||
console.log('\nMarket Selection URL : \nhttps://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements \n')
|
||||
ask.question('Market : ', (mar) => {
|
||||
if(mar.length === 2) market = mar
|
||||
else {
|
||||
console.log('Invalid Market, Selecting IN as market')
|
||||
market = 'IN'
|
||||
}
|
||||
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`)
|
||||
ask.question('Redirected URL : ',async (url) => {
|
||||
if (!fs.existsSync('.data')) fs.mkdirSync('.data')
|
||||
spotifyData = {
|
||||
client_id,
|
||||
client_secret,
|
||||
redirect_url,
|
||||
authorization_code : url.split('code=')[1],
|
||||
market
|
||||
}
|
||||
let check = await SpotifyAuthorize(spotifyData)
|
||||
if(check === false) throw new Error('Failed to get access Token.')
|
||||
ask.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<boolean>{
|
||||
let response = await request(`https://accounts.spotify.com/api/token`, {
|
||||
headers : {
|
||||
"Authorization" : `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`,
|
||||
"Content-Type" : "application/x-www-form-urlencoded"
|
||||
export async function SpotifyAuthorize(data: SpotifyDataOptions): Promise<boolean> {
|
||||
const response = await request(`https://accounts.spotify.com/api/token`, {
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body : `grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(data.redirect_url)}`,
|
||||
method : "POST"
|
||||
}).catch(() => {
|
||||
return 0
|
||||
})
|
||||
|
||||
if(typeof response === 'number') return false
|
||||
let resp_json = JSON.parse(response)
|
||||
body: `grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(
|
||||
data.redirect_url
|
||||
)}`,
|
||||
method: 'POST'
|
||||
}).catch((err: Error) => {
|
||||
return err;
|
||||
});
|
||||
if (response instanceof Error) throw response;
|
||||
const resp_json = JSON.parse(response);
|
||||
spotifyData = {
|
||||
client_id : data.client_id,
|
||||
client_secret : data.client_secret,
|
||||
redirect_url : data.redirect_url,
|
||||
access_token : resp_json.access_token,
|
||||
refresh_token : resp_json.refresh_token,
|
||||
expires_in : Number(resp_json.expires_in),
|
||||
expiry : Date.now() + ((resp_json.expires_in - 1) * 1000),
|
||||
token_type : resp_json.token_type,
|
||||
market : data.market
|
||||
}
|
||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4))
|
||||
return true
|
||||
client_id: data.client_id,
|
||||
client_secret: data.client_secret,
|
||||
redirect_url: data.redirect_url,
|
||||
access_token: resp_json.access_token,
|
||||
refresh_token: resp_json.refresh_token,
|
||||
expires_in: Number(resp_json.expires_in),
|
||||
expiry: Date.now() + (resp_json.expires_in - 1) * 1000,
|
||||
token_type: resp_json.token_type,
|
||||
market: data.market
|
||||
};
|
||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4));
|
||||
return true;
|
||||
}
|
||||
|
||||
export function is_expired(){
|
||||
if(Date.now() >= (spotifyData.expiry as number)) return true
|
||||
else return false
|
||||
export function is_expired() {
|
||||
if (Date.now() >= (spotifyData.expiry as number)) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
export async function RefreshToken(): Promise<true | false>{
|
||||
let response = await request(`https://accounts.spotify.com/api/token`, {
|
||||
headers : {
|
||||
"Authorization" : `Basic ${Buffer.from(`${spotifyData.client_id}:${spotifyData.client_secret}`).toString('base64')}`,
|
||||
"Content-Type" : "application/x-www-form-urlencoded"
|
||||
export async function refreshToken(): Promise<true | false> {
|
||||
const response = await request(`https://accounts.spotify.com/api/token`, {
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(`${spotifyData.client_id}:${spotifyData.client_secret}`).toString(
|
||||
'base64'
|
||||
)}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body : `grant_type=refresh_token&refresh_token=${spotifyData.refresh_token}`,
|
||||
method : "POST"
|
||||
}).catch(() => {
|
||||
return 0
|
||||
})
|
||||
|
||||
if(typeof response === 'number') return false
|
||||
let resp_json = JSON.parse(response)
|
||||
spotifyData.access_token = resp_json.access_token
|
||||
spotifyData.expires_in = Number(resp_json.expires_in)
|
||||
spotifyData.expiry = Date.now() + ((resp_json.expires_in - 1) * 1000)
|
||||
spotifyData.token_type = resp_json.token_type
|
||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4))
|
||||
return true
|
||||
}
|
||||
body: `grant_type=refresh_token&refresh_token=${spotifyData.refresh_token}`,
|
||||
method: 'POST'
|
||||
}).catch((err: Error) => {
|
||||
return err;
|
||||
});
|
||||
if (response instanceof Error) throw response;
|
||||
const resp_json = JSON.parse(response);
|
||||
spotifyData.access_token = resp_json.access_token;
|
||||
spotifyData.expires_in = Number(resp_json.expires_in);
|
||||
spotifyData.expiry = Date.now() + (resp_json.expires_in - 1) * 1000;
|
||||
spotifyData.token_type = resp_json.token_type;
|
||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4));
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user