zotify: Failed fetching audio key!
Hello. This software has been working perfect for me up until today. I get multiple errors trying to download albums and tracks. "Failed fetching audio key!
Failed fetching audio key - gid: 34a47982845a47808079a3c77396e6ae, fileId: f50977596f4be8ce0e3f918d51009daef606fb9c
[∙∙∙] Preparing download... Traceback (most recent call last):
File "C:\ProgramData\anaconda3\Lib\site-packages\zotify\track.py", line 224, in download_track
stream = Zotify.get_content_stream(track, Zotify.DOWNLOAD_QUALITY)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\zotify\zotify.py", line 53, in get_content_stream
return cls.SESSION.content_feeder().load(content_id, VorbisOnlyAudioQuality(quality), False, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\librespot\audio\__init__.py", line 739, in load
return self.load_track(playable_id, audio_quality_picker, preload,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\librespot\audio\__init__.py", line 800, in load_track
return self.load_stream(file, track, None, preload, halt_listener)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\librespot\audio\__init__.py", line 754, in load_stream
return CdnFeedHelper.load_track(self.__session, track, file,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\librespot\audio\__init__.py", line 339, in load_track
key = session.audio_key().get_audio_key(track.gid, file.file_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\librespot\audio\__init__.py", line 277, in get_audio_key
return self.get_audio_key(gid, file_id, False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\librespot\audio\__init__.py", line 278, in get_audio_key
raise RuntimeError(
RuntimeError: Failed fetching audio key! gid: 34a47982845a47808079a3c77396e6ae, fileId: f50977596f4be8ce0e3f918d51009daef606fb9c
This now happens on full albums and individual tracks. I haven’t touched or changed anything. Does anyone know what the problem is. Thanks.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 62
While we are striving to find a long-term solution regarding this issue, I would also like to share my own workaround method, which is more on the “Give me my songs, damnit!” point of view than waiting a certain time before a download could be manually tried again.
If you don’t wanna Zotify to shutdown on encountering the infamous Audio Key Error, but instead wait a determined time window before continuously trying to resume the task you were doing, then it’s possible to do so. The Audio Key Error does not seems to impose you a temporary ban, rather limiting the success rate of a request for a key.
Knowing this, you can adapt the
__init__.pyfile in “/zotify/lib/python3.10/site-packages/librespot/audio” or wherever this specific file of Librespot is located, by changing the get_audio_key function as follow :This will give Zotify a cooldown when this error raise, then the task will continue and so on. You can adapt
time.sleep(15)to how many seconds you’d like to wait between each try.I, for myself and using a waiting time of 10 seconds, got this result in the scope of a bot using Zotify :
Just ran some tests, the requests for the raw audio files do not seem to be factored into their rate limiting algorithm at all. I was able to download 10,000 raw oggs without a single error.
The rate limiting gets triggered only for the audio keys.
I have a wild proposal that might work to overcome this issue:
Crowdsourced open database of audio keys.
When a user tries downloading a track, it queries this database first for the key.
If no key is found, it then queries Spotify’s servers and then submits the key to the database for the community to use later.
There are obviously problems with this proposal in regards to legal risk, costs, anonymity of submissions, etc.
(If you don’t want to do all of this, or don’t know anything and are afraid to break something, scroll and read on how the audio keys work and read the conclusion.)
Where to put the code. I think I found a fix for it based on this comment. Like the comment said, you gotta go to C:/users/(your name)/pipx/venvs/zotify/Lib/site-packages/zotify/track.py. In that script is all the code for downloading and even converting songs. There isn’t a
time.sleep()command in line 153 to begin with. What I think he was saying is YOU need to put atime.sleep()in line 153 or in betweenprepare_download_loader = Loader(PrintChannel.PROGRESS_INFO, "Preparing download...")andprepare_download_loader.start(). When I did this, it successfully buffered the download speed.How many seconds should I put in? What I don’t recommend putting for the amount of seconds is anything that is 9 seconds or lower, this for me does NOT fix the audio key issue. (I’ll talk about how I THINK the audio key issue works in a bit.) If you REALLY aren’t patient, go for 10-12 seconds, this doesn’t completely fix it, but it’s enough to push a few more downloads. If you want to be a bit more safe, go for 15-20 seconds. This gives you way more time to download and should keep the cooldown low enough to not activate the error. (I’ll explain about that cooldown thing in a little bit.) If you want to be REALLY safe, go for 25 or more. In my opinion, this is just too slow, BUT it is faster than real time.
How I THINK the audio key issue might work. I think the audio key issue might be going off how many songs you’re downloading in a set time, think of it like an unknown cooldown. From what I tested, (Trying to download 4 albums at once.) the amount of songs you can download without using a
time.sleep()is somewhere around 50 songs. (These songs are around 3 minutes long.) If you try closing powershell or whatever you are using and running Zotify again, you will just keep getting the same audio key error. In order for it to stop you have to wait around 10-30 minutes so you can continue WITHOUT thetime.sleep(). Again, it’s very similar to a video game cooldown. Thetime.sleep()is only TRYING to keep you from reaching the cooldown, which is the reason why putting anything under 9 seconds doesn’t work very well. Increasing the number of seconds will result in less or even no audio errors, depending on many seconds you put.Conclusion. You are most likely bound to run to this error since Spotify has limited the amount of audio keys Zotify can request. This is so far the only way I know to bypass the audio key error. If you ever do get this error, just take a 10-30 minute break from downloading before continuing to avoid it as much as possible. To the people who don’t want to do all of this, just use download in real time. It will most likely remove the audio key error completely, but the only downside to this is downloading 50 songs takes like 1 to 3 hours.
alr im gonna go get some wingstop, making an essay for fun gets you really hungry fr
I doubt it’s related to that. I’m facing the same bug on a zotify clone that I wrote in rust.
It appears to be caused by some rate limiting introduced by Spotify.
Try throttling the speed of the download and you won’t face the errors.
Edit:
I guess the --download-real-time flag is a necessity now, not just for preventing bans, but for actually being able to download the songs.
I added:
time.sleep(5)after
Printer.print(PrintChannel.DOWNLOADS, f'### Downloaded "{song_name}" to "{Path(filename).relative>at ~/.local/pipx/venvs/zotify/lib/python3.10/site-packages/zotify/track.py (I am using linux)
this delay seems to mitigate somehow the error. I am also downloading the files as mp3, so that the conversion process add more time between downloads.
That is ace! @HxDxRx . I’ve been running a big download with the 30 second wait and haven’t hit an audio key error yet.
For those with less coding experience I have created a fork of main with install and usage instructions. With it you can set delays between downloads by setting --bulk-wait-time on the command line to whatever (30 seconds sounds best).
It also includes retry logic for audio key errors which is only included if you clone the repo into your local environment and then run pin install . in the repo. This is because the requirements.txt file and setup.cfg file reference different versions of librespot, one with and one without retry logic for audio key errors.
They are indeed used for streaming too, whether on the native client or librespot. Only encrypted files are on spotify’s server, and whatever client you use needs to fetch the audio key to decrypt it for playback/download.
What you described seems like Spotify’s rate limit is indiscriminate even towards their own official client. I would imagine them to be so because if there existed some special unrestricted method for it, it would be targeted for reverse engineering like the rest.
So, here is the rust code to run the test I just mentioned:
I have compiled the results for:
There appears to be at first a hard rate limit (where no requests go through at all) and then a soft one after which requests go through roughly every 30 seconds.
I’ll cite the line where the soft limit is reached in each of the above tests
speedrun.txt L43 1sec.txt L44 5sec.txt L48
The prefix numbers indicate how many requests successfully went through consecutively.
I am on macos and the their was no sleep command already, but I added a sleep right after the
if not Zotify.CONFIG.get_bulk_wait_time()if statement, and it works, so far, and most importantly does not sleep if it is supposed to skip it…Using a large playlist for an artist, I logged the timestamps for every: successful download, skipped existing file, and audio key error (which is functioning as a rate limit error). For testing I added a 2 second wait after every skipped file event, a 15 second wait after every successful download, and a 60 second wait after every audio key error. The 60 second wait required changing the code in
librespot\audio\__init__.py.After running for several hours I calculated the cumulative sum for successful downloads over time. A simple linear regression gives a slope in units of downloads/second. The reciprocal then gives an estimate for the allowed seconds/download. The slope was 0.03047406 and the reciprocal of that is 32.81479 seconds/download. The songs had all downloaded in 2 seconds or less, so I changed the successful download wait to 30 seconds and the audio key errors stopped.
Notes: -72 songs downloaded in the first 23 minutes before the first audio key error occurred. -The rate of the audio key errors was consistent once they started, which made the regression calculation possible. -I restarted the download of the playlist to see if skipped existing files affected anything, but even with just a 1 second wait there were no audio key errors triggered until the downloading eventually resumed. This is good news when dealing with resumed large downloads.
If you use pipx you can also just replace pip with pipx and it installs fine. Make sure to uninstall zotify first using pipx or add
--forceso it replaces it.Tried it on MacOS. Works like a charm.
I’m working on exactly that - deducing the rate limit. 😅
Wrote a tool to connect to Spotify and just download the audio key in a loop whilst not making any call to any other endpoint in the process.
Running the tests with sleeps in the loop as well to test if it helps.
I’ll publish my tests with Unix timestamps for us to be able to detect a pattern.
Hmmm, then maybe I shouldn’t be using a VPN lool
Edit: Can confirm that I am getting like 0 ratelimits with the 5 sec sleep and my VPN off…
I think I may have found a solution, but it wont be as fast as originally. So first of all the explanation: if there are to many request hitting the Spotify API, wich Zotify uses, you get an error code. To solve that we add a little timer so Zotify doesn’t download to fast. To do that you go into C:/users/(your name)/pipx/venvs/zotify/Lib/site-packages/zotify/track.py. At least that was for me the case. When you open the track.py file, there should be a time.sleep command in line 153 with the value 0. I changed it to 5 for now but i think you should be able to lower it. Safe the file and start Zotify and try downloading. The value in time.sleep is corrospondend to your average song length, so if you download anything longer you can turn it down. I couldnt find the max amount for the API so i had to guess something.
I haven’t really changed anything besides the retry attempts, I set those to 5 and to skip existing and previously downloaded to true
This can either be fixed with the
--download-real-time=Trueflag, or changing your IP with a VPN.