mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
Spotify Work + Error Language = EN - US
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import got, { Response } from "got/dist/source";
|
||||
import { SpotifyDataOptions } from ".";
|
||||
|
||||
|
||||
interface SpotifyTrackAlbum{
|
||||
@@ -90,8 +92,10 @@ export class SpotifyPlaylist{
|
||||
id : string;
|
||||
thumbnail : SpotifyThumbnail;
|
||||
owner : SpotifyArtists;
|
||||
tracks : SpotifyVideo[]
|
||||
constructor(data : any){
|
||||
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
|
||||
@@ -104,11 +108,56 @@ export class SpotifyPlaylist{
|
||||
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.tracks = videos
|
||||
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 got(`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[] = []
|
||||
let res = response as Response<string>
|
||||
let json_data = JSON.parse(res.body)
|
||||
json_data.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyVideo(v.track))
|
||||
})
|
||||
this.fetched_tracks.set(`${i}`, videos)
|
||||
resolve('Success')
|
||||
}))
|
||||
}
|
||||
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}`)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
@@ -121,7 +170,6 @@ export class SpotifyPlaylist{
|
||||
id : this.id,
|
||||
thumbnail : this.thumbnail,
|
||||
owner : this.owner,
|
||||
tracks : this.tracks
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,16 +178,19 @@ 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;
|
||||
total_tracks : number
|
||||
tracks : SpotifyTracks[]
|
||||
constructor(data : any){
|
||||
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[] = []
|
||||
@@ -154,12 +205,56 @@ export class SpotifyAlbum{
|
||||
this.copyrights = data.copyrights
|
||||
this.release_date = data.release_date
|
||||
this.release_date_precision = data.release_date_precision
|
||||
this.total_tracks = data.total_tracks
|
||||
this.trackCount = data.total_tracks
|
||||
let videos: SpotifyTracks[] = []
|
||||
data.tracks.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyTracks(v))
|
||||
})
|
||||
this.tracks = videos
|
||||
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 got(`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[] = []
|
||||
let res = response as Response<string>
|
||||
let json_data = JSON.parse(res.body)
|
||||
json_data.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyTracks(v))
|
||||
})
|
||||
this.fetched_tracks.set(`${i}`, videos)
|
||||
resolve('Success')
|
||||
}))
|
||||
}
|
||||
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}`)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
@@ -173,7 +268,6 @@ export class SpotifyAlbum{
|
||||
release_date : this.release_date,
|
||||
release_date_precision : this.release_date_precision,
|
||||
total_tracks : this.total_tracks,
|
||||
tracks : this.tracks
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user