From 1666c2709f575a20c7ae1d0c76935b855e872dc9 Mon Sep 17 00:00:00 2001 From: Dan Walker Date: Sun, 25 Oct 2020 06:08:57 -0700 Subject: [PATCH 1/2] Fixed tiktok.py The JSON formatting of the response changed, corrected. #TESTING Successfully downloaded and played a tiktok video --- youtube_dlc/extractor/tiktok.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/youtube_dlc/extractor/tiktok.py b/youtube_dlc/extractor/tiktok.py index 52e5f4f1f..f27aa4250 100644 --- a/youtube_dlc/extractor/tiktok.py +++ b/youtube_dlc/extractor/tiktok.py @@ -14,17 +14,17 @@ class TikTokBaseIE(InfoExtractor): def _extract_aweme(self, video_data, webpage, url): video_info = try_get( - video_data, lambda x: x['videoData']['itemInfos'], dict) + video_data, lambda x: x['itemInfo']['itemStruct'], dict) author_info = try_get( - video_data, lambda x: x['videoData']['authorInfos'], dict) - share_info = try_get(video_data, lambda x: x['shareMeta'], dict) + video_data, lambda x: x['itemInfo']['itemStruct']['author'], dict) + share_info = try_get(video_data, lambda x: x['itemInfo']['shareMeta'], dict) unique_id = str_or_none(author_info.get('uniqueId')) timestamp = try_get(video_info, lambda x: int(x['createTime']), int) date = datetime.fromtimestamp(timestamp).strftime('%Y%m%d') - height = try_get(video_info, lambda x: x['video']['videoMeta']['height'], int) - width = try_get(video_info, lambda x: x['video']['videoMeta']['width'], int) + height = try_get(video_info, lambda x: x['video']['height'], int) + width = try_get(video_info, lambda x: x['video']['width'], int) thumbnails = [] thumbnails.append({ 'url': video_info.get('thumbnail') or self._og_search_thumbnail(webpage), @@ -34,7 +34,7 @@ def _extract_aweme(self, video_data, webpage, url): formats = [] formats.append({ - 'url': try_get(video_info, lambda x: x['video']['urls'][0]), + 'url': try_get(video_info, lambda x: x['video']['playAddr']), 'ext': 'mp4', 'height': height, 'width': width From 4f5a0ad82214456c3d4832bbf4e15e416e1a16cb Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 26 Oct 2020 02:15:46 +0100 Subject: [PATCH 2/2] [tiktok] fallback url + cookie --- youtube_dlc/extractor/tiktok.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/youtube_dlc/extractor/tiktok.py b/youtube_dlc/extractor/tiktok.py index f27aa4250..0cfd2168a 100644 --- a/youtube_dlc/extractor/tiktok.py +++ b/youtube_dlc/extractor/tiktok.py @@ -12,7 +12,8 @@ class TikTokBaseIE(InfoExtractor): - def _extract_aweme(self, video_data, webpage, url): + def _extract_aweme(self, props_data, webpage, url): + video_data = try_get(props_data, lambda x: x['pageProps'], expected_type=dict) video_info = try_get( video_data, lambda x: x['itemInfo']['itemStruct'], dict) author_info = try_get( @@ -32,14 +33,20 @@ def _extract_aweme(self, video_data, webpage, url): 'height': height }) + url = '' + if not url: + url = try_get(video_info, lambda x: x['video']['playAddr']) + if not url: + url = try_get(video_info, lambda x: x['video']['downloadAddr']) formats = [] formats.append({ - 'url': try_get(video_info, lambda x: x['video']['playAddr']), + 'url': url, 'ext': 'mp4', 'height': height, 'width': width }) + tracker = try_get(props_data, lambda x: x['initialProps']['$wid']) return { 'comment_count': int_or_none(video_info.get('commentCount')), 'duration': try_get(video_info, lambda x: x['video']['videoMeta']['duration'], int), @@ -63,6 +70,7 @@ def _extract_aweme(self, video_data, webpage, url): 'formats': formats, 'http_headers': { 'Referer': url, + 'Cookie': 'tt_webid=%s; tt_webid_v2=%s' % (tracker, tracker), } } @@ -130,10 +138,10 @@ def _real_extract(self, url): r'id=\"__NEXT_DATA__\"\s+type=\"application\/json\"\s*[^>]+>\s*(?P[^<]+)', webpage, 'json_string', group='json_string_ld') json_data = self._parse_json(json_string, video_id) - video_data = try_get(json_data, lambda x: x['props']['pageProps'], expected_type=dict) + props_data = try_get(json_data, lambda x: x['props'], expected_type=dict) # Chech statusCode for success - if video_data.get('statusCode') == 0: - return self._extract_aweme(video_data, webpage, url) + if props_data.get('pageProps').get('statusCode') == 0: + return self._extract_aweme(props_data, webpage, url) raise ExtractorError('Video not available', video_id=video_id)