[tiktok] Address review comments on _extract_rehydration_data()
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
"""Extractors for https://www.tiktok.com/"""
|
"""Extractors for https://www.tiktok.com/"""
|
||||||
|
|
||||||
from json.decoder import JSONDecodeError
|
|
||||||
from .common import Extractor, Message
|
from .common import Extractor, Message
|
||||||
from .. import text, util, ytdl, exception
|
from .. import text, util, ytdl, exception
|
||||||
|
|
||||||
@@ -101,26 +100,26 @@ class TiktokExtractor(Extractor):
|
|||||||
def _sanitize_url(self, url):
|
def _sanitize_url(self, url):
|
||||||
return text.ensure_http_scheme(url.replace("/photo/", "/video/", 1))
|
return text.ensure_http_scheme(url.replace("/photo/", "/video/", 1))
|
||||||
|
|
||||||
def _extract_rehydration_data(self, url, *, retries=None):
|
def _extract_rehydration_data(self, url):
|
||||||
if retries is None:
|
tries = 0
|
||||||
retries = self._retries
|
while True:
|
||||||
try:
|
try:
|
||||||
html = self.request(url).text
|
html = self.request(url).text
|
||||||
data = text.extr(
|
data = text.extr(
|
||||||
html, '<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" '
|
html, '<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" '
|
||||||
'type="application/json">', '</script>')
|
'type="application/json">', '</script>')
|
||||||
return util.json_loads(data)["__DEFAULT_SCOPE__"]
|
return util.json_loads(data)["__DEFAULT_SCOPE__"]
|
||||||
except JSONDecodeError:
|
except ValueError:
|
||||||
# We failed to retrieve rehydration data. This happens relatively
|
# We failed to retrieve rehydration data. This happens
|
||||||
# frequently when making many requests, so retry.
|
# relatively frequently when making many requests, so
|
||||||
self.log.warning("%s: Failed to retrieve rehydration data, trying "
|
# retry.
|
||||||
"%d more time%s and delaying for %d second(s)",
|
tries += 1
|
||||||
url, retries, "" if retries == 1 else "s",
|
self.log.warning("%s: Failed to retrieve rehydration data "
|
||||||
self._timeout)
|
"(%s/%s)", url.rpartition("/")[2], tries,
|
||||||
self.sleep(self._timeout, "retry")
|
self._retries)
|
||||||
if retries > 0:
|
self.sleep(self._timeout, "retry")
|
||||||
return self._extract_rehydration_data(url, retries=retries-1)
|
if tries >= self._retries:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def _extract_audio(self, post):
|
def _extract_audio(self, post):
|
||||||
audio = post["music"]
|
audio = post["music"]
|
||||||
|
|||||||
Reference in New Issue
Block a user