[TikTok] Pass cookies to mobile API (#1994)

Authored by: MinePlayersPE
This commit is contained in:
MinePlayersPE 2021-12-15 00:10:13 +07:00 committed by GitHub
parent db77c49c84
commit 53dad39e30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,8 +26,9 @@ class TikTokBaseIE(InfoExtractor):
_MANIFEST_APP_VERSION = '291' _MANIFEST_APP_VERSION = '291'
_APP_NAME = 'trill' _APP_NAME = 'trill'
_AID = 1180 _AID = 1180
_API_HOSTNAME = 'api-t2.tiktokv.com' _API_HOSTNAME = 'api-h2.tiktokv.com'
_UPLOADER_URL_FORMAT = 'https://www.tiktok.com/@%s' _UPLOADER_URL_FORMAT = 'https://www.tiktok.com/@%s'
_WEBPAGE_HOST = 'https://www.tiktok.com/'
QUALITIES = ('360p', '540p', '720p') QUALITIES = ('360p', '540p', '720p')
def _call_api(self, ep, query, video_id, fatal=True, def _call_api(self, ep, query, video_id, fatal=True,
@ -68,6 +69,9 @@ def _call_api(self, ep, query, video_id, fatal=True,
'cp': 'cbfhckdckkde1', 'cp': 'cbfhckdckkde1',
} }
self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choice('0123456789abcdef') for _ in range(160))) self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choice('0123456789abcdef') for _ in range(160)))
webpage_cookies = self._get_cookies(self._WEBPAGE_HOST)
if webpage_cookies.get('sid_tt'):
self._set_cookie(self._API_HOSTNAME, 'sid_tt', webpage_cookies['sid_tt'].value)
return self._download_json( return self._download_json(
'https://%s/aweme/v1/%s/' % (self._API_HOSTNAME, ep), video_id=video_id, 'https://%s/aweme/v1/%s/' % (self._API_HOSTNAME, ep), video_id=video_id,
fatal=fatal, note=note, errnote=errnote, headers={ fatal=fatal, note=note, errnote=errnote, headers={
@ -176,6 +180,7 @@ def extract_addr(addr, add_meta={}):
user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info, user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info,
'sec_uid', 'id', 'uid', 'unique_id', 'sec_uid', 'id', 'uid', 'unique_id',
expected_type=str_or_none, get_all=False)) expected_type=str_or_none, get_all=False))
labels = traverse_obj(aweme_detail, ('hybrid_label', ..., 'text'), expected_type=str)
contained_music_track = traverse_obj( contained_music_track = traverse_obj(
music_info, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type=str) music_info, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type=str)
@ -206,7 +211,11 @@ def extract_addr(addr, add_meta={}):
'timestamp': int_or_none(aweme_detail.get('create_time')), 'timestamp': int_or_none(aweme_detail.get('create_time')),
'formats': formats, 'formats': formats,
'thumbnails': thumbnails, 'thumbnails': thumbnails,
'duration': int_or_none(traverse_obj(video_info, 'duration', ('download_addr', 'duration')), scale=1000) 'duration': int_or_none(traverse_obj(video_info, 'duration', ('download_addr', 'duration')), scale=1000),
'availability': self._availability(
is_private='Private' in labels,
needs_subscription='Friends only' in labels,
is_unlisted='Followers only' in labels)
} }
def _parse_aweme_video_web(self, aweme_detail, webpage_url): def _parse_aweme_video_web(self, aweme_detail, webpage_url):
@ -340,7 +349,9 @@ class TikTokIE(TikTokBaseIE):
def _extract_aweme_app(self, aweme_id): def _extract_aweme_app(self, aweme_id):
aweme_detail = self._call_api('aweme/detail', {'aweme_id': aweme_id}, aweme_id, aweme_detail = self._call_api('aweme/detail', {'aweme_id': aweme_id}, aweme_id,
note='Downloading video details', errnote='Unable to download video details')['aweme_detail'] note='Downloading video details', errnote='Unable to download video details').get('aweme_detail')
if not aweme_detail:
raise ExtractorError('Video not available', video_id=aweme_id)
return self._parse_aweme_video_app(aweme_detail) return self._parse_aweme_video_app(aweme_detail)
def _real_extract(self, url): def _real_extract(self, url):
@ -542,6 +553,7 @@ class DouyinIE(TikTokIE):
_AID = 1128 _AID = 1128
_API_HOSTNAME = 'aweme.snssdk.com' _API_HOSTNAME = 'aweme.snssdk.com'
_UPLOADER_URL_FORMAT = 'https://www.douyin.com/user/%s' _UPLOADER_URL_FORMAT = 'https://www.douyin.com/user/%s'
_WEBPAGE_HOST = 'https://www.douyin.com/'
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) video_id = self._match_id(url)