@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
"""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
|
||||||
|
|
||||||
@@ -143,12 +144,22 @@ 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):
|
def _extract_rehydration_data(self, url, *, retries=1):
|
||||||
html = self.request(url).text
|
try:
|
||||||
data = text.extr(
|
html = self.request(url).text
|
||||||
html, '<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" '
|
data = text.extr(
|
||||||
'type="application/json">', '</script>')
|
html, '<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" '
|
||||||
return util.json_loads(data)["__DEFAULT_SCOPE__"]
|
'type="application/json">', '</script>')
|
||||||
|
return util.json_loads(data)["__DEFAULT_SCOPE__"]
|
||||||
|
except JSONDecodeError:
|
||||||
|
# We failed to retrieve rehydration data. This happens relatively
|
||||||
|
# frequently, so retry if we're told to do so.
|
||||||
|
self.log.warning("%s: Failed to retrieve rehydration data, trying "
|
||||||
|
"%d more time%s", url, retries,
|
||||||
|
"" if retries == 1 else "s")
|
||||||
|
if retries > 0:
|
||||||
|
return self._extract_rehydration_data(url, retries=retries-1)
|
||||||
|
raise
|
||||||
|
|
||||||
def _extract_audio(self, post):
|
def _extract_audio(self, post):
|
||||||
audio = post["music"]
|
audio = post["music"]
|
||||||
@@ -179,7 +190,7 @@ class TiktokExtractor(Extractor):
|
|||||||
elif status == 10204:
|
elif status == 10204:
|
||||||
self.log.error("%s: Requested post not available", url)
|
self.log.error("%s: Requested post not available", url)
|
||||||
elif status == 10231:
|
elif status == 10231:
|
||||||
self.log.error("%s: Region locked - Try downloading with a"
|
self.log.error("%s: Region locked - Try downloading with a "
|
||||||
"VPN/proxy connection", url)
|
"VPN/proxy connection", url)
|
||||||
else:
|
else:
|
||||||
self.log.error(
|
self.log.error(
|
||||||
|
|||||||
Reference in New Issue
Block a user