Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spotify API Access: Spytify sometimes breaks on track change / Not authorized after sometimes #370

Closed
internetfreak opened this issue Jul 17, 2021 · 27 comments · Fixed by #379
Labels
api 📡 Related to Spotify or LastFM API bug 🐞 task ⚒ An improvement or general task to do
Milestone

Comments

@internetfreak
Copy link

What is the bug ?
When recording a lot of tracks via the program, it sometimes breaks recording. This can be noticed because the timer within the app doesn't move anymore but Spotify itself just continues playing. It seems that even if this problem happens, the playback data is still recorded but trying to stop Spytify results in force stopping it (clicking Stop again) as it waits for the track to be finished but that never happens.
Afterwards, I often have to delete the resulting file and start over with where I lost connection to record. Also, I then need to create a complete new Spotify App to access API again, otherwise the program will ask for access but doesn't detect anything.

Track used
Happens with any track that it currently playing after a longer recording session. The disconnects happen at any time, so I can't give any good time window of how much to record before it starts breaking.

Screenshots
Will attach a screenshot if helpful and it happens again.

@internetfreak
Copy link
Author

internetfreak commented Jul 17, 2021

I have a screenshot of the problem now that it happened again. This is what I see when it happens:
image

The recording continued even after stopping Spotify so with manual timing I can get the whole track anyway but then I need to reset the program anyway to continue recording

EDIT at 7:36 PM GMT+2: It seems to reliably happen after almost one hour of continuous recording

EDIT 2 at 7:41 PM GMT+2: This seems to be intentional behaviour from Spotify itself, the tokens for their API expire after one hour but there is a refresh token that can be used to get a new access token. I have not yet checked the source code of this program to see whether this is already handled or not.
See those issues for Spotify Web API:
spotify/web-api#506
spotify/web-api#414

EDIT 3: It seems that the App already handles that the token is expired but for some reason it doesn't seem to work for me. Unfortunately this makes recording while being away not optimal.

@internetfreak
Copy link
Author

v1.8 records without any interruptions but I cannot figure out what exactly could've changed between versions which definitely breaks stuff. The only downside is that v1.8 doesn't include any album art or album title and track numbers are missing as well but that can be manually fixed (even with 1.9 I needed to edit tags manually as not everything is being filled)

@jwallet
Copy link
Owner

jwallet commented Jul 19, 2021

okay so I may have to upgrade my spotify-net-api to v6

thank you for your detailed report

edit: i will try when I have a chance, but I'm wondering if spytify latest version stop working when recording wav file, or mp3 file but with lastfm api.. it will help target the issue

@jwallet
Copy link
Owner

jwallet commented Jul 19, 2021

might be related to

When using Spotify API, Spytify will now ask the API for current playback status instead of using its own logic #137

@jwallet
Copy link
Owner

jwallet commented Jul 19, 2021

ok it did not crash on my side, I have been recording for more than 10 hours.
image

I don't know if this can affect the spotify api connection, but my spotify account only serves this spotify app, so if your phone or google home connects to it and you start a new song over there, this might be something I never handled.

to be sure, you may want to create a free spotify account just for recording, connect to the desktop app with this one, and authorize spytify to connect to it, this way no other device will take control of the account. (btw it's still just a guess of what's going on if you are talking about token)

otherwise, the app works with task and threads, I don't know if this can be an issue on a pc with a single core, I only make it work on my 2-3 pc/laptops then I release the new version, so I'm limited on how many hardware (including sound card) that I can test the app on.

others things...

  • does your pc goes to sleep?
  • have you try another audio device?

@internetfreak
Copy link
Author

internetfreak commented Jul 19, 2021

Thanks for trying to fix it, I'll pull the source code later and test if my problem is fixed. The things I noted above were some of my guesses about what could happen, I tried to find out more about it from the source code (I'm a C# dev too) but I wasn't really successfull finding the issue or at least more hints for you what exactly happens.

Since I began to record I only used my account on my PC, I was careful to not use any other device that connects to spotify and possibly breaks my recordings

Tasks/Threads are no problem for my PC, I have a AMD Ryzen 7 3700X so with 8 Cores and 16 Threads there should be enough resources to make sure the recording does get what it wants. I haven't tried another sound device as I only have my onboard sound card otherwise and I am mostly recording while using the PC so the virtual audio cable helps there.
Sleep mode for my PC is disabled, the only thing that would happen is to turn of my displays after some time of being away

EDIT: I see you haven't pushed your changes yet, so unfortunately I can't test if it works now

@jwallet
Copy link
Owner

jwallet commented Jul 21, 2021

i got the same issue today, after recording for 10 hours, still don't know where it comes from, I was not running the app in debug, now I do

@internetfreak
Copy link
Author

If there's anything I could try, let me know. When I started this issue I also tried to debug a little bit, but I was clueless as well, where to debug to find anything about the issue

@jwallet
Copy link
Owner

jwallet commented Jul 21, 2021

I got the issue in debug mode, it looks like the app loose spotify handler process id so it's not bind to it, and a restart is needed

just added this part, will see... don't know why I did not handled else, maybe it goes into a deadlock 🤔
image

@jwallet
Copy link
Owner

jwallet commented Jul 22, 2021

two other exceptions came up, but it comes from spotifyapi.net, and apparently it was fixed in v6, so i will have to upgrade to it

@jwallet jwallet added api 📡 Related to Spotify or LastFM API task ⚒ An improvement or general task to do labels Jul 22, 2021
@jwallet
Copy link
Owner

jwallet commented Jul 24, 2021

@internetfreak I just released 1.10, you may want to give it a try, I let it run from monday to friday, corrected some issues here and there and it did not got any exception for the last 2 days.

@internetfreak
Copy link
Author

Thanks for letting me know. Downloaded the new version and started a long enough queue to test it. Will give feedback at a later date

@internetfreak
Copy link
Author

Encountered another problem. Although recording works fine, the app requested API access again after one hour (when the token is expired and should be renewed). Maybe v6 of the API lib will do that but I wasn't able to grant access (browser starts "loading" indefinitely, the "window can be closed" message never appears). This causes Spytify to fall back to LastFM, effectively getting not all metadata etc.
I have recreated the API application and started recording again so I'll see whether it repeats or not but still noting this here in case it's necessary info

@internetfreak
Copy link
Author

Okay, it happened again. Because my comment above might be confusing, here's a recap of what happens

This is what I was doing

  1. Opened Spytify/Spotify
  2. Obtained a fresh API app
  3. Filled the credentials
  4. Started recording

One hour later, this happens

  1. Spytify switches tracks
  2. It notices that the token expired and probably tries to refresh it
  3. A sound can be heard, some green stuff flashes briefly before it disappears (I assume it might be an error but it's not possible to read it)
  4. A new browser tab opens and I'll get asked to allow access to the developer app again
  5. I try to grant access but the browser just loads forever
  6. Spytify continues recording and splitting but as it fell back to LastFM, metadata will be missing

Effectively I still have to restart recording after hour but this time due to another error. If you need me to debug something, let me know and I'll try to help as this doesn't seem to happen on your side (yet)

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

Now I'm wondering if it's an issue with Spotify's recent update, they started to play multiple ads after every song.

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

what does your api access status is? mine is "granted"
image

@internetfreak
Copy link
Author

My api status is development mode which should be normally enough for single user and personal use according to spotify. However I've seen that the app made 26k requests on a single day, this seems quite a lot because I only recorded a few songs (about 2.5 albums)
image

Regarding ads, I have spotify premium, so ads shouldn't be an issue for me

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

did you add your account in the Users and Access section?

image

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

I've seen that the app made 26k requests on a single day, this seems quite a lot because I only recorded a few songs (about 2.5 albums)

spytify also use the api for track status, maybe I will need to reduce the refresh rate or just use it for metadata. it's indeed to high. I never looked at my own stats

image

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

I moved some issues to this discussion because they are all related to spotify latest update and spotify dashboard.

@jwallet jwallet changed the title Spytify sometimes breaks on track change Spotify API Access: Spytify sometimes breaks on track change / Not authorized after sometimes Jul 27, 2021
@internetfreak
Copy link
Author

Nope, didn't add my own account to that section as this has worked before, so maybe that's the issue for me? But then it should be noticeable quite early instead of after 60 minutes which seems odd.

As for the high request rate, if you run into a rate limit, Spotify will tell you that and let's you know when you can request again. I don't know if that is already handled by the API lib.

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

I will try to create an app in development this week and try it, maybe in development you are limited to 25k requests or something. I will probably reduce that number quite a lot anyway, like we said, it's too high

@jwallet
Copy link
Owner

jwallet commented Jul 27, 2021

also I will look if refresh token is allowed in development

@internetfreak
Copy link
Author

Spotify itself says this in regards of the developer mode:

As before, all apps in this mode can access the full breadth of Web API functionality with no additional approvals needed. We designed the development mode to fit the majority of use cases for single-user apps. If you’re exploring the APIs, building home automation, Magic Mirrors or similar use cases, this mode will fit perfectly. If you wish to onboard more than 25 users you will need to submit a quota extension request.

So according to this, refresh tokens etc should work normally. As noted earlier in this issue, 1.8 was able to record for hours (I've recorded 1.5 days worth of music at once) and I did the same with the api app as with 1.9/1.10 (only development mode; no user added to the section; only added ID & Secret to Spytify)

@jwallet
Copy link
Owner

jwallet commented Jul 28, 2021

ok so I will remove spotify status from the app to reduce api calls, I think that's the only difference between 1.8 and 1.9

@internetfreak
Copy link
Author

I have checked out your pull request and started recording again to test it. So far it works without any issue and I'm recording for more than one hour already. I have more songs lined up (at least 5-6h of music, probably more like 10-12h) and will continue recording over night but I don't expect any issues.
Guess it's safe enough for you to merge your changes, release a new version and then this can be closed for good

@internetfreak
Copy link
Author

Having recorded for almost 48h continously, I can definitely say that the fix works. I have listened to a few recordings across all tracks and I couldn't find anything negative but the plus an metadata automatically fetched for me makes it incredibly easy to extend my library. Thanks for the work, closing this

@jwallet jwallet added this to the 1.10.1 milestone Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api 📡 Related to Spotify or LastFM API bug 🐞 task ⚒ An improvement or general task to do
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants