Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
handled JSONDecodeError when Twitter API returns 429
  • Loading branch information
Ryu1845 committed Jun 20, 2022
1 parent dcb7f38 commit 89b66f3
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions twspace_dl/twspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime

import requests
from requests.exceptions import JSONDecodeError

from . import twitter

Expand Down Expand Up @@ -239,11 +240,31 @@ def from_user_avatar(cls, user_url, auth_token):
}
user_id = twitter.user_id(user_url)
params = {"user_ids": user_id, "only_spaces": "true"}
avatar_content = requests.get(
avatar_content_res = requests.get(
"https://twitter.com/i/api/fleets/v1/avatar_content",
params=params,
headers=headers,
).json()
)
if avatar_content_res.ok:
avatar_content = avatar_content_res.json()
else:
logging.debug(avatar_content_res.text)
if avatar_content_res.status_code == requests.codes.too_many:
raise ValueError(
(
"Response status code is 429! "
"You hit Twitter's ratelimit, retry later."
)
)
if 400 <= avatar_content_res.status_code < 500:
raise ValueError(
"Response code is in the 4XX range. Bad request on our side"
)
if avatar_content_res.status_code >= 500:
raise ValueError(
"Response code is over 500. There was an error on Twitter's side"
)
raise ValueError("Can't get proper response")

try:
broadcast_id = avatar_content["users"][user_id]["spaces"]["live_content"][
Expand Down

0 comments on commit 89b66f3

Please sign in to comment.