Samik081 / beets-beatport4

Beatport API v4 compatible beets plugin
MIT License
25 stars 2 forks source link

Fix showing token prompt if only client_id is missing #2

Closed JOJ0 closed 1 year ago

JOJ0 commented 1 year ago

This prevents showing of token prompt when a user has lying around a non valid beatport_token.json in the config dir but has valid username and password configured in the beets config. The only thing that's missing in such a situation is a valid client_id which will now be automatically fetched.

JOJ0 commented 1 year ago

Hi @Samik081, what you think about this solution? Could there be side effects?

The current alternative to how it's initially implemented is to delete the broken beatport_token.json each and every day again which is rather tedious.

Hope that helps. Please get back to me with any concerns.

Samik081 commented 1 year ago

Hey @JOJ0, thank you for spotting this bug! 👀

I see a little problem with your change - fetching client_id from Beatport creates an additional connection, which would make the total plugin loading time longer. There is no need to fetch the client_id if the token in beatport_token.json is valid. I think, that the correct solution to this issue is moving fetching client_id to the _authorize() function, so it will be fetched only if t's needed.

Would you like to work on this, or should I close the PR and fix it? :)

JOJ0 commented 1 year ago

Hi, I gave it a quick try yesterday eveneing. Latest commit is what I came up with. Is this like you ment? Somehow I can't fetch API_CLIENT_ID anymore. Even when I revert that commit I still get this error:

prod  (beets)    ~/git/beets-beatport4   fix_client_id_not_fetched  beet -vv import -L -t truss
user configuration: /home/jojo/.config/remotebeets/config.yaml
data directory: /home/jojo/.config/remotebeets
plugin paths: /home/jojo/git/whatlastgenre/plugin/beets/beetsplug
inline: adding item field disc_and_track
inline: adding album field incomplete
artresizer: method is ImageMagick
thumbnails: using ImageMagick to write metadata
thumbnails: using GIO to compute URIs
Sending event: pluginload
library database: /home/jojo/.config/remotebeets/remotebeets.db
library directory: /remote/data/music-library
Sending event: library_opened
Sending event: import_begin
beatport4: Started authorizing to the API using username and password
Could not fetch API_CLIENT_ID
Could not fetch token. Check your beatport username and password in the config, or try to get token manually.
Login at https://api.beatport.com/v4/docs/ and paste /token endpoint response from the browser: 

So, not sure what's wrong on my end. Anyway, hope you can work with this PR's change. Close or use as a starter, however you like. Thanks! :-) HTH

Samik081 commented 1 year ago

These are exactly the changes I meant, and I've done to the code locally, thank you! :)

I have also encountered the issue you mentioned - it happened, because there was double slash // within the URL for parsing JS files, and looks like Beatport started treating such URLs differently (literally today or yesterday ^^) and just redirecting to main docs page. It is laready fixed in v0.2.4.