diff options
author | Kevin Wood <endotronic@gmail.com> | 2022-11-06 09:45:45 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-06 23:15:45 +0530 |
commit | 8c188d5d09177ed213a05c900d3523867c5897fd (patch) | |
tree | 9df4a7c01962da4ee0bedba432a4b437199e876e | |
parent | e14ea7fbd92cc15ad0dccedc163f8c26f843c389 (diff) | |
download | hypervideo-pre-8c188d5d09177ed213a05c900d3523867c5897fd.tar.lz hypervideo-pre-8c188d5d09177ed213a05c900d3523867c5897fd.tar.xz hypervideo-pre-8c188d5d09177ed213a05c900d3523867c5897fd.zip |
[extractor/redgifs] Refresh auth token for 401 (#5352)
Closes #5351
Authored by: endotronic, pukkandan
-rw-r--r-- | yt_dlp/extractor/redgifs.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/yt_dlp/extractor/redgifs.py b/yt_dlp/extractor/redgifs.py index 24ac9420e..92d996ca6 100644 --- a/yt_dlp/extractor/redgifs.py +++ b/yt_dlp/extractor/redgifs.py @@ -1,4 +1,5 @@ import functools +import urllib from .common import InfoExtractor from ..compat import compat_parse_qs @@ -72,14 +73,20 @@ class RedGifsBaseInfoExtractor(InfoExtractor): self._API_HEADERS['authorization'] = f'Bearer {auth["token"]}' def _call_api(self, ep, video_id, *args, **kwargs): - if 'authorization' not in self._API_HEADERS: - self._fetch_oauth_token(video_id) - assert 'authorization' in self._API_HEADERS - - headers = dict(self._API_HEADERS) - headers['x-customheader'] = f'https://www.redgifs.com/watch/{video_id}' - data = self._download_json( - f'https://api.redgifs.com/v2/{ep}', video_id, headers=headers, *args, **kwargs) + for attempt in range(2): + if 'authorization' not in self._API_HEADERS: + self._fetch_oauth_token(video_id) + try: + headers = dict(self._API_HEADERS) + headers['x-customheader'] = f'https://www.redgifs.com/watch/{video_id}' + data = self._download_json( + f'https://api.redgifs.com/v2/{ep}', video_id, headers=headers, *args, **kwargs) + break + except ExtractorError as e: + if not attempt and isinstance(e.cause, urllib.error.HTTPError) and e.cause.code == 401: + del self._API_HEADERS['authorization'] # refresh the token + raise + if 'error' in data: raise ExtractorError(f'RedGifs said: {data["error"]}', expected=True, video_id=video_id) return data |