Spotify Work + Error Language = EN - US
This commit is contained in:
parent
139b540a2c
commit
0b4979f009
@ -15,6 +15,13 @@ This is a **light-weight** youtube downloader and searcher.
|
|||||||
npm install play-dl@latest
|
npm install play-dl@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Importing
|
||||||
|
```ts
|
||||||
|
import * as play from 'play-dl' // ES-6 import or TS import
|
||||||
|
|
||||||
|
const play = require('play-dl') //JS importing
|
||||||
|
```
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
- [YouTube](https://github.com/play-dl/play-dl/tree/main/examples/YouTube)
|
- [YouTube](https://github.com/play-dl/play-dl/tree/main/examples/YouTube)
|
||||||
- [Spotify](https://github.com/play-dl/play-dl/tree/main/examples/Spotify)
|
- [Spotify](https://github.com/play-dl/play-dl/tree/main/examples/Spotify)
|
||||||
|
|||||||
@ -1,5 +1,88 @@
|
|||||||
# Spotify
|
# Spotify
|
||||||
|
|
||||||
|
## Main
|
||||||
|
### spotify(url : `string`)
|
||||||
|
*This returns data from a track | playlist | album url.*
|
||||||
|
|
||||||
|
```js
|
||||||
|
let data = await spotify(url) //Gets the data
|
||||||
|
|
||||||
|
console.log(data.type) // Console logs the type of data that you got.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Authorization()
|
||||||
|
*This creates basic spotify data to be stored locally.*
|
||||||
|
|
||||||
|
```js
|
||||||
|
Authorization() //After then you will be asked client-id, client-secret, redirect url, market, redirected URL.
|
||||||
|
```
|
||||||
|
|
||||||
|
### is_expired()
|
||||||
|
*This tells that whether the access token is expired or not*
|
||||||
|
|
||||||
|
**Returns :** `boolean`
|
||||||
|
|
||||||
|
```js
|
||||||
|
if(is_expired()){
|
||||||
|
await RefreshToken()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### RefreshToken()
|
||||||
|
*This refreshes the access token.*
|
||||||
|
|
||||||
|
**Returns :** `boolean` for telling whether access token is refreshed or not
|
||||||
|
|
||||||
|
```js
|
||||||
|
await RefreshToken()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Classes [ Returned by spotify() function ]
|
||||||
|
### SpotifyVideo
|
||||||
|
*Don't go by the name. This is class for a spotify track.*
|
||||||
|
|
||||||
|
#### type `property`
|
||||||
|
*This will always return as "track" for this class.*
|
||||||
|
|
||||||
|
#### toJSON() `function`
|
||||||
|
*converts class into a json format*
|
||||||
|
|
||||||
|
### SpotifyPlaylist
|
||||||
|
*This is a spotify playlist class.*
|
||||||
|
|
||||||
|
#### fetch() `function`
|
||||||
|
*This will fetch tracks in a playlist upto 1000 tracks only.*
|
||||||
|
|
||||||
|
```js
|
||||||
|
let data = await spotify(playlist_url)
|
||||||
|
|
||||||
|
await data.fetch() // Fetches tracks more than 100 tracks in playlist
|
||||||
|
```
|
||||||
|
|
||||||
|
#### type `property`
|
||||||
|
*This will always return as "playlist" for this class.*
|
||||||
|
|
||||||
|
#### toJSON() `function`
|
||||||
|
*converts class into a json format*
|
||||||
|
|
||||||
|
### SpotifyAlbum
|
||||||
|
*This is a spotify albun class.*
|
||||||
|
|
||||||
|
#### fetch() `function`
|
||||||
|
*This will fetch tracks in a album upto 500 tracks only.*
|
||||||
|
|
||||||
|
```js
|
||||||
|
let data = await spotify(playlist_url)
|
||||||
|
|
||||||
|
await data.fetch() // Fetches tracks more than 50 tracks in album
|
||||||
|
```
|
||||||
|
|
||||||
|
#### type `property`
|
||||||
|
*This will always return as "album" for this class.*
|
||||||
|
|
||||||
|
#### toJSON() `function`
|
||||||
|
*converts class into a json format*
|
||||||
|
|
||||||
## Validate
|
## Validate
|
||||||
### sp_validate(url : `string`)
|
### sp_validate(url : `string`)
|
||||||
*This checks that given url is spotify url or not.*
|
*This checks that given url is spotify url or not.*
|
||||||
@ -12,13 +95,3 @@ if(!check) // Invalid Spotify URL
|
|||||||
|
|
||||||
if(check === 'track') // Spotify Track URL
|
if(check === 'track') // Spotify Track URL
|
||||||
```
|
```
|
||||||
|
|
||||||
## Main
|
|
||||||
### spotify(url : `string`)
|
|
||||||
*This returns data from a track | playlist | album url.*
|
|
||||||
|
|
||||||
```js
|
|
||||||
let data = spotify(url) //Gets the data
|
|
||||||
|
|
||||||
console.log(data.type) // Console logs the type of data that you got.
|
|
||||||
```
|
|
||||||
3
examples/Spotify/authorize.js
Normal file
3
examples/Spotify/authorize.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
const { Authorization } = require('play-dl');
|
||||||
|
|
||||||
|
Authorization()
|
||||||
@ -14,7 +14,9 @@ client.on('messageCreate', async message => {
|
|||||||
guildId : message.guild.id,
|
guildId : message.guild.id,
|
||||||
adapterCreator: message.guild.voiceAdapterCreator
|
adapterCreator: message.guild.voiceAdapterCreator
|
||||||
})
|
})
|
||||||
|
if(play.is_expired()){
|
||||||
|
await play.RefreshToken() // This will check if access token has expired or not. If yes, then refresh the token.
|
||||||
|
}
|
||||||
let args = message.content.split('play ')[1].split(' ')[0]
|
let args = message.content.split('play ')[1].split(' ')[0]
|
||||||
let sp_data = await play.spotify(args) // This will get spotify data from the url [ I used track url, make sure to make a logic for playlist, album ]
|
let sp_data = await play.spotify(args) // This will get spotify data from the url [ I used track url, make sure to make a logic for playlist, album ]
|
||||||
let searched = await play.search(`${sp_data.name}`, { limit : 1 }) // This will search the found track on youtube.
|
let searched = await play.search(`${sp_data.name}`, { limit : 1 }) // This will search the found track on youtube.
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
import got, { Response } from "got/dist/source";
|
||||||
|
import { SpotifyDataOptions } from ".";
|
||||||
|
|
||||||
|
|
||||||
interface SpotifyTrackAlbum{
|
interface SpotifyTrackAlbum{
|
||||||
@ -90,8 +92,10 @@ export class SpotifyPlaylist{
|
|||||||
id : string;
|
id : string;
|
||||||
thumbnail : SpotifyThumbnail;
|
thumbnail : SpotifyThumbnail;
|
||||||
owner : SpotifyArtists;
|
owner : SpotifyArtists;
|
||||||
tracks : SpotifyVideo[]
|
tracksCount : number;
|
||||||
constructor(data : any){
|
private spotifyData : SpotifyDataOptions;
|
||||||
|
private fetched_tracks : Map<string, SpotifyVideo[]>
|
||||||
|
constructor(data : any, spotifyData : SpotifyDataOptions){
|
||||||
this.name = data.name
|
this.name = data.name
|
||||||
this.type = "playlist"
|
this.type = "playlist"
|
||||||
this.collaborative = data.collaborative
|
this.collaborative = data.collaborative
|
||||||
@ -104,11 +108,56 @@ export class SpotifyPlaylist{
|
|||||||
url : data.owner.external_urls.spotify,
|
url : data.owner.external_urls.spotify,
|
||||||
id : data.owner.id
|
id : data.owner.id
|
||||||
}
|
}
|
||||||
|
this.tracksCount = Number(data.tracks.total)
|
||||||
let videos: SpotifyVideo[] = []
|
let videos: SpotifyVideo[] = []
|
||||||
data.tracks.items.forEach((v : any) => {
|
data.tracks.items.forEach((v : any) => {
|
||||||
videos.push(new SpotifyVideo(v.track))
|
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(){
|
toJSON(){
|
||||||
@ -121,7 +170,6 @@ export class SpotifyPlaylist{
|
|||||||
id : this.id,
|
id : this.id,
|
||||||
thumbnail : this.thumbnail,
|
thumbnail : this.thumbnail,
|
||||||
owner : this.owner,
|
owner : this.owner,
|
||||||
tracks : this.tracks
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,16 +178,19 @@ export class SpotifyAlbum{
|
|||||||
name : string
|
name : string
|
||||||
type : "track" | "playlist" | "album"
|
type : "track" | "playlist" | "album"
|
||||||
url : string
|
url : string
|
||||||
|
id : string;
|
||||||
thumbnail : SpotifyThumbnail
|
thumbnail : SpotifyThumbnail
|
||||||
artists : SpotifyArtists[]
|
artists : SpotifyArtists[]
|
||||||
copyrights : SpotifyCopyright[]
|
copyrights : SpotifyCopyright[]
|
||||||
release_date : string;
|
release_date : string;
|
||||||
release_date_precision : string;
|
release_date_precision : string;
|
||||||
total_tracks : number
|
trackCount : number
|
||||||
tracks : SpotifyTracks[]
|
private spotifyData : SpotifyDataOptions;
|
||||||
constructor(data : any){
|
private fetched_tracks : Map<string, SpotifyTracks[]>
|
||||||
|
constructor(data : any, spotifyData : SpotifyDataOptions){
|
||||||
this.name = data.name
|
this.name = data.name
|
||||||
this.type = "album"
|
this.type = "album"
|
||||||
|
this.id = data.id
|
||||||
this.url = data.external_urls.spotify
|
this.url = data.external_urls.spotify
|
||||||
this.thumbnail = data.images[0]
|
this.thumbnail = data.images[0]
|
||||||
let artists : SpotifyArtists[] = []
|
let artists : SpotifyArtists[] = []
|
||||||
@ -154,12 +205,56 @@ export class SpotifyAlbum{
|
|||||||
this.copyrights = data.copyrights
|
this.copyrights = data.copyrights
|
||||||
this.release_date = data.release_date
|
this.release_date = data.release_date
|
||||||
this.release_date_precision = data.release_date_precision
|
this.release_date_precision = data.release_date_precision
|
||||||
this.total_tracks = data.total_tracks
|
this.trackCount = data.total_tracks
|
||||||
let videos: SpotifyTracks[] = []
|
let videos: SpotifyTracks[] = []
|
||||||
data.tracks.items.forEach((v : any) => {
|
data.tracks.items.forEach((v : any) => {
|
||||||
videos.push(new SpotifyTracks(v))
|
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(){
|
toJSON(){
|
||||||
@ -173,7 +268,6 @@ export class SpotifyAlbum{
|
|||||||
release_date : this.release_date,
|
release_date : this.release_date,
|
||||||
release_date_precision : this.release_date_precision,
|
release_date_precision : this.release_date_precision,
|
||||||
total_tracks : this.total_tracks,
|
total_tracks : this.total_tracks,
|
||||||
tracks : this.tracks
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ if(fs.existsSync('.data/spotify.data')){
|
|||||||
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString())
|
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SpotifyDataOptions{
|
export interface SpotifyDataOptions{
|
||||||
client_id : string;
|
client_id : string;
|
||||||
client_secret : string;
|
client_secret : string;
|
||||||
redirect_url : string;
|
redirect_url : string;
|
||||||
@ -43,7 +43,7 @@ export async function spotify(url : string): Promise<SpotifyAlbum | SpotifyPlayl
|
|||||||
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||||
}
|
}
|
||||||
}).catch((err) => {return 0})
|
}).catch((err) => {return 0})
|
||||||
if(typeof response !== 'number') return new SpotifyAlbum(JSON.parse(response.body))
|
if(typeof response !== 'number') return new SpotifyAlbum(JSON.parse(response.body), spotifyData)
|
||||||
else throw new Error('Failed to get spotify Album Data')
|
else throw new Error('Failed to get spotify Album Data')
|
||||||
}
|
}
|
||||||
else if(url.indexOf('playlist/') !== -1){
|
else if(url.indexOf('playlist/') !== -1){
|
||||||
@ -53,7 +53,7 @@ export async function spotify(url : string): Promise<SpotifyAlbum | SpotifyPlayl
|
|||||||
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||||
}
|
}
|
||||||
}).catch((err) => {return 0})
|
}).catch((err) => {return 0})
|
||||||
if(typeof response !== 'number') return new SpotifyAlbum(JSON.parse(response.body))
|
if(typeof response !== 'number') return new SpotifyPlaylist(JSON.parse(response.body), spotifyData)
|
||||||
else throw new Error('Failed to get spotify Playlist Data')
|
else throw new Error('Failed to get spotify Playlist Data')
|
||||||
}
|
}
|
||||||
else throw new Error('URL is out of scope for play-dl.')
|
else throw new Error('URL is out of scope for play-dl.')
|
||||||
|
|||||||
@ -109,7 +109,7 @@ export class PlayList{
|
|||||||
}
|
}
|
||||||
|
|
||||||
page(number : number): Video[]{
|
page(number : number): Video[]{
|
||||||
if(!number) throw new Error('Given Page number is not provided')
|
if(!number) throw new Error('Page number is not provided')
|
||||||
if(!this.fetched_videos.has(`page${number}`)) throw new Error('Given Page number is invalid')
|
if(!this.fetched_videos.has(`page${number}`)) throw new Error('Given Page number is invalid')
|
||||||
return this.fetched_videos.get(`page${number}`) as Video[]
|
return this.fetched_videos.get(`page${number}`) as Video[]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,9 @@ export async function search(search :string, options? : ParseSearchInterface): P
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let body = await url_get(url)
|
let body = await url_get(url, {
|
||||||
|
headers : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
|
||||||
|
})
|
||||||
let data = ParseSearchResult(body, options)
|
let data = ParseSearchResult(body, options)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ export async function video_basic_info(url : string, cookie? : string){
|
|||||||
else video_id = url
|
else video_id = url
|
||||||
let new_url = `https://www.youtube.com/watch?v=${video_id}`
|
let new_url = `https://www.youtube.com/watch?v=${video_id}`
|
||||||
let body = await url_get(new_url, {
|
let body = await url_get(new_url, {
|
||||||
headers : (cookie) ? { 'cookie' : cookie } : {}
|
headers : (cookie) ? { 'cookie' : cookie, 'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' } : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
|
||||||
})
|
})
|
||||||
let player_response = JSON.parse(body.split("var ytInitialPlayerResponse = ")[1].split("}};")[0] + '}}')
|
let player_response = JSON.parse(body.split("var ytInitialPlayerResponse = ")[1].split("}};")[0] + '}}')
|
||||||
let initial_response = JSON.parse(body.split("var ytInitialData = ")[1].split("}};")[0] + '}}')
|
let initial_response = JSON.parse(body.split("var ytInitialData = ")[1].split("}};")[0] + '}}')
|
||||||
@ -129,7 +129,9 @@ export async function playlist_info(url : string, parseIncomplete : boolean = fa
|
|||||||
else Playlist_id = url
|
else Playlist_id = url
|
||||||
let new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`
|
let new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`
|
||||||
|
|
||||||
let body = await url_get(new_url)
|
let body = await url_get(new_url, {
|
||||||
|
headers : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
|
||||||
|
})
|
||||||
let response = JSON.parse(body.split("var ytInitialData = ")[1].split(";</script>")[0])
|
let response = JSON.parse(body.split("var ytInitialData = ")[1].split(";</script>")[0])
|
||||||
if(response.alerts){
|
if(response.alerts){
|
||||||
if(response.alerts[0].alertWithButtonRenderer?.type === 'INFO') {
|
if(response.alerts[0].alertWithButtonRenderer?.type === 'INFO') {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user