trakt / script.trakt

Trakt.tv movie and TV show scrobbler for Kodi
GNU General Public License v2.0
320 stars 149 forks source link

Sync stops with error: "[script.trakt] trakt.interfaces.base: Request failed: "POST /sync/history" - 429: "Rate limit exceeded" (Rate Limit Exceeded)" #619

Open waywit opened 1 year ago

waywit commented 1 year ago

Expected Behavior

The Trak.tv add-on should respect the defined API usage rules defined by trakt.tv here: https://github.com/trakt/api-help/issues/220

Current Behavior

The Trakt.tv add-on does not respect the boundaries and receives a "[script.trakt] trakt.interfaces.base: Request failed: "POST /sync/history" - 429: "Rate limit exceeded" (Rate Limit Exceeded)" Error. This issue is not shown in UI, so that the user thinks that everything is fine, but now there are many information missing in trakt.tv.

Possible Solution

I have some ideas to implement:

  1. per run only submit the maximum allowed requests to the trat.tv API. BUT inform the user about this and he can try again 10 minutes later to submit the missing changes.
  2. Respect the request limits in software and wait/slowdown the requests or cut them into smaller packets so that every change can be submitted in on single run, but this one will take longer.

Steps to Reproduce (for bugs)

  1. Setup Kodi with enough change that should be submitted to trakt.tv
  2. Enable Debug logging in Trakt.tv add-on
  3. Start an update by running the Trakt.tv add-on
  4. Check Logfile for message after collecting big amount of changes: "[script.trakt] trakt.interfaces.base: Request failed: "POST /sync/history" - 429: "Rate limit exceeded" (Rate Limit Exceeded)"

Context

Actually my Kodi library cannot be synced with trakt, only single changes are transported to trakt.tv.

Your Environment

waywit commented 9 months ago

Hi, 6 month without any response :-( Is the Trakt Addon still in development? I would like to know if I can use Trakt.TV in the future anymore or if I will stuck here with not possible to update my trait.tv status?

Maybe the solution can be found in the trakt.tv API information: https://trakt.docs.apiary.io/#introduction/required-headers

"All API methods are rate limited. A 429 HTTP status code is returned when the limit has been exceeded. Check the headers for detailed info, then try your API call in Retry-After seconds."

Thanks and best regards...

razzeee commented 9 months ago

This would be needed to be implemented in https://github.com/fuzeman/trakt.py/

waywit commented 9 months ago

Hi, thanks for your response. I have done a dirty Fix in your script... I found out, that the rate limit was violated between to API requests doing different things, time between both calls <1s... So I added a 1 second "time.sleep(1)" which solved the actual issue and my collection can be synced again... but I am not sure where this needs to be originally be fixed...

As you can see here that there are 4 POST API calls in 1 second, where 3 of them violate the API restrictions by trakt.tv:

2024-01-03 20:09:15.378 T:2503099 debug <general>: [script.trakt] resources.lib.syncEpisodes: [traktUpdateEpisodes] Shows to update {'shows': [{'title': ...}]}]}]} 2024-01-03 20:09:15.584 T:2503099 debug <general>: [script.trakt] urllib3.connectionpool: https://api.trakt.tv:443 "POST /sync/history HTTP/1.1" 201 None 2024-01-03 20:09:15.585 T:2503099 debug <general>: [script.trakt] resources.lib.syncEpisodes: [traktUpdateEpisodes] Shows to update {'shows': [{'title': ...}]}]}]} 2024-01-03 20:09:15.709 T:2503099 debug <general>: [script.trakt] urllib3.connectionpool: https://api.trakt.tv:443 "POST /sync/history HTTP/1.1" 429 None 2024-01-03 20:09:15.709 T:2503099 warning <general>: [script.trakt] trakt.interfaces.base: Request failed: "POST /sync/history" - 429: "Rate limit exceeded" (Rate Limit Exceeded) 2024-01-03 20:09:15.709 T:2503099 debug <general>: [script.trakt] resources.lib.syncEpisodes: [traktUpdateEpisodes] Shows to update {'shows': [{'title': ...}]}]}]} 2024-01-03 20:09:15.838 T:2503099 debug <general>: [script.trakt] urllib3.connectionpool: https://api.trakt.tv:443 "POST /sync/history HTTP/1.1" 429 None 2024-01-03 20:09:15.838 T:2503099 warning <general>: [script.trakt] trakt.interfaces.base: Request failed: "POST /sync/history" - 429: "Rate limit exceeded" (Rate Limit Exceeded) 2024-01-03 20:09:15.839 T:2503099 debug <general>: [script.trakt] resources.lib.syncEpisodes: [traktUpdateEpisodes] Shows to update {'shows': [{'title': ...}]}]}]} 2024-01-03 20:09:15.963 T:2503099 debug <general>: [script.trakt] urllib3.connectionpool: https://api.trakt.tv:443 "POST /sync/history HTTP/1.1" 429 None 2024-01-03 20:09:15.963 T:2503099 warning <general>: [script.trakt] trakt.interfaces.base: Request failed: "POST /sync/history" - 429: "Rate limit exceeded" (Rate Limit Exceeded) 2024-01-03 20:09:15.963 T:2503099 debug <general>: [script.trakt] resources.lib.syncEpisodes: [traktUpdateEpisodes] Finished with 0 error(s)

From my point of view the issue is not an mass update issue, but a fast POST API issue... Perhaps you can take a look on it and maybe find a fix for it, where you can wait 1 second between every POST API call?

Many thanks in advance :-)

razzeee commented 9 months ago

As stated, that should be done in trakt.py (both projects have been around before trakt introduced api limits)

https://github.com/fuzeman/trakt.py/issues/86

waywit commented 9 months ago

Puh... the linked issue is older than 3 years, without any progress :-( I think the chances to get fixed are very low, maybe time to look for an alternative for trakt.tv...

dan-riley commented 9 months ago

I ran into this issue as well, installing the plugin on an existing Kodi installation and trying to sync old history. I was also able to fix it with a sleep call. Specifically, in .kodi\addons\script.trakt\resources\lib\traktapi.py, under addToHistory(self, mediaObject), before result = Trakt['sync/history'].add(mediaObject), added time.sleep(1). I also noticed there's quite a few things it tries to sync each time that don't seem to exist in Trakt, like special episodes...so would like to figure out how to get it to stop doing that each time!

nathanpw commented 8 months ago

Hello and thank you for this great Kodi plugin/script! I have run into the same rate limiting issue/error. For me it seems to be due to many show updates, which eventually clear itself if you run it multiple times. Sometimes, updates of episodes that aren't tracked on tmdb (which keep trying to update) also cause issue (or "blockages"). Anyhow the time.sleep(1) hack did make this more consistent and reliable for me. However, as mentioned it is an improper fix and a proper implementation should be made upstream in the trakt api library being used.

I did notice a different trakt api python interface had a solution: https://github.com/moogar0880/PyTrakt/pull/138

I was hoping to look at this more, unfortunately I don't have the time. I would assume implementing a new trakt api interface would be more time consuming and problematic.

Just thought I would share my experience with this. Thanks again for your efforts and support!

razzeee commented 8 months ago

Looking at that repo, it also seems pretty dead, so not sure if that would improve anything