play-dl / play-dl

Mainly used for Discord js Bots to play music.
https://play-dl.github.io/
GNU General Public License v3.0
206 stars 63 forks source link

[=BUG=] Spotify Download is not working #349

Open Fyphen1223 opened 1 year ago

Fyphen1223 commented 1 year ago

Describe the bug

Actual behavior

${u}`)),l=[];if(typeof o!="string")return;JSON.parse(o).items.forEach(u=>{u.track&&l.push(new E(u.track))}),this.fetched_tracks.set(`${r}`,l),s("Success")}));return await Promise.allSettled(t),this}page(e){if(!e)throw new Error("Page number is not provided");if(!this.fetched_tracks.has(`${e}`))throw new Error("Given Page number is invalid");return this.fetched_tracks.get(`${e}`)}get total_pages(){return this.fetched_tracks.size}get total_tracks(){if(this.search)return this.tracksCount;let e=this.total_pages;return(e-1)*100+this.fetched_tracks.get(`${e}`).length}async all_tracks(){await this.fetch();let e=[];for(let t of this.fetched_tracks.values())e.push(...t);return e}toJSON(){return{name:this.name,collaborative:this.collaborative,description:this.description,url:this.url,id:this.id,thumbnail:this.thumbnail,owner:this.owner,tracksCount:this.tracksCount}}};a(N,"SpotifyPlaylist");var z=class{constructor(e,t,r){this.name=e.name,this.type="album",this.id=e.id,this.search=r,this.url=e.external_urls.spotify,this.thumbnail=e.images[0];let s=[];e.artists.forEach(o=>{s.push({name:o.name,id:o.id,url:o.external_urls.spotify})}),this.artists=s,this.copyrights=e.copyrights,this.release_date=e.release_date,this.release_date_precision=e.release_date_precision,this.tracksCount=e.total_tracks;let n=[];this.search||e.tracks.items.forEach(o=>{n.push(new E(o))}),this.fetched_tracks=new Map,this.fetched_tracks.set("1",n),this.spotifyData=t}async fetch(){if(this.search)return this;let e;if(this.tracksCount>500?e=500:e=this.tracksCount,e<=50)return this;let t=[];for(let r=2;r<=Math.ceil(e/50);r++)t.push(new Promise(async(s,n)=>{let o=await h(`https://api.spotify.com/v1/albums/${this.id}/tracks?offset=${(r-1)*50}&limit=50&market=${this.spotifyData.market}`,{headers:{Authorization:`${this.spotifyData.token_type} ${this.spotifyData.access_token}`}}).catch(u=>n(`Response Error : 

TypeError: Cannot read properties of undefined (reading 'spotify')

Expected behavior

#### Code to reproduce ```js ``` #### ScreenShots

Checklist

Impxrtant322 commented 1 year ago

You probably have an undefined variable somewhere in your code. I wouldn't know though because there is no code to look at.

PedroVH commented 1 year ago

The same happens here.

Here is the code I'm trying to execute:

import play from "play-dl";

if(play.is_expired) await play.refreshToken()
console.log(await play.spotify('https://open.spotify.com/track/3m8UfZwPMMbiWfGR8Lg2cR?si=ee01102c58724355'))

Here is the stack:

TypeError: Cannot read properties of undefined (reading 'spotify')
    at new SpotifyTrack (/hd/dev/tortuga-bot/node_modules/play-dl/dist/index.js:7:23462)
    at Object.spotify (/hd/dev/tortuga-bot/node_modules/play-dl/dist/index.js:10:382)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///hd/dev/tortuga-bot/test.js:4:13

The error is thrown in the constructor of SpotifyTrack E=class{constructor(e){this.name=e.name,this.id=e.id,this.type="track",this.url=e.external_urls.spotify,..., where e.external_urls.spotify is 'undefined'.

Node.js v19.6.0 "play-dl": "^1.9.6"

MerfAvenger commented 1 year ago

I'm having the same issue as the @PedroVH (having reviewed his code on GitHub, it's very similar to mine and seems to follow the guides). I notice he doesn't seem to ever have found a fix for it, and I can't find any other threads that seem to still be relevant to this particular issue.

Using:

Node.js v19.6.0 "play-dl": "^1.9.6"

Notes

I've run the authorize.js step.

Code

Initialising Spotify:

  private async initialiseSpotify(): Promise<void> {
    this.logger.log('Initialising spotify.');

    await play.setToken({
      spotify: {
        client_id: process.env.SPOTIFY_CLIENT_ID as string,
        client_secret: process.env.SPOTIFY_CLIENT_SECRET as string,
        refresh_token: process.env.SPOTIFY_REFRESH_TOKEN as string,
        market: process.env.SPOTIFY_MARKET as string
      },
      useragent: ['DinnerJacket']
    });

    this.logger.log('Spotify initialisation complete.');
  }

Fetching a Spotify Track:

    if (!spotify) {
      this.logger.warn('Spotify service is not set up.');
    }

    if (play.is_expired()) {
      this.logger.log('Spotify authentication expired, refreshing token.');
      const success = await play.refreshToken();
      this.logger.log(`Token refresh ${success ? 'successful' : 'failure'}.`);
    }

    let stream = await spotify(url);

When fetching the track I get the below Error (note that I even check for the Spotify object) so it appears to be some internal reference to spotify that's missing even after setting the token:

TypeError: Cannot read properties of undefined (reading 'spotify')
    at new SpotifyTrack (PROJECT_DIR\node_modules\play-dl\dist\index.js:7:23462)
    at spotify (PROJECT_DIR\node_modules\play-dl\dist\index.js:10:382)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AudioManager.buildSpotifyTrack (PROJECT_DIR\dist\client\audio\AudioManager.js:134:22)