]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/tiktok.py
fix tiktok download
[yt-dlp.git] / youtube_dlc / extractor / tiktok.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 from datetime import datetime
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 int_or_none,
9 str_or_none,
10 try_get
11 )
12
13
14 class TikTokBaseIE(InfoExtractor):
15 def _extract_aweme(self, props_data, webpage, url):
16 video_data = try_get(props_data, lambda x: x['pageProps'], expected_type=dict)
17 video_info = try_get(
18 video_data, lambda x: x['itemInfo']['itemStruct'], dict)
19 author_info = try_get(
20 video_data, lambda x: x['itemInfo']['itemStruct']['author'], dict)
21 share_info = try_get(video_data, lambda x: x['itemInfo']['shareMeta'], dict)
22
23 unique_id = str_or_none(author_info.get('uniqueId'))
24 timestamp = try_get(video_info, lambda x: int(x['createTime']), int)
25 date = datetime.fromtimestamp(timestamp).strftime('%Y%m%d')
26
27 height = try_get(video_info, lambda x: x['video']['height'], int)
28 width = try_get(video_info, lambda x: x['video']['width'], int)
29 thumbnails = []
30 thumbnails.append({
31 'url': video_info.get('thumbnail') or self._og_search_thumbnail(webpage),
32 'width': width,
33 'height': height
34 })
35
36 url = ''
37 if not url:
38 url = try_get(video_info, lambda x: x['video']['playAddr'])
39 if not url:
40 url = try_get(video_info, lambda x: x['video']['downloadAddr'])
41 formats = []
42 formats.append({
43 'url': url,
44 'ext': 'mp4',
45 'height': height,
46 'width': width
47 })
48
49 tracker = try_get(props_data, lambda x: x['initialProps']['$wid'])
50 return {
51 'comment_count': int_or_none(video_info.get('commentCount')),
52 'duration': try_get(video_info, lambda x: x['video']['videoMeta']['duration'], int),
53 'height': height,
54 'id': str_or_none(video_info.get('id')),
55 'like_count': int_or_none(video_info.get('diggCount')),
56 'repost_count': int_or_none(video_info.get('shareCount')),
57 'thumbnail': try_get(video_info, lambda x: x['covers'][0]),
58 'timestamp': timestamp,
59 'width': width,
60 'title': str_or_none(share_info.get('title')) or self._og_search_title(webpage),
61 'creator': str_or_none(author_info.get('nickName')),
62 'uploader': unique_id,
63 'uploader_id': str_or_none(author_info.get('userId')),
64 'uploader_url': 'https://www.tiktok.com/@' + unique_id,
65 'thumbnails': thumbnails,
66 'upload_date': date,
67 'webpage_url': self._og_search_url(webpage),
68 'description': str_or_none(video_info.get('text')) or str_or_none(share_info.get('desc')),
69 'ext': 'mp4',
70 'formats': formats,
71 'http_headers': {
72 'Referer': url,
73 'Cookie': 'tt_webid=%s; tt_webid_v2=%s' % (tracker, tracker),
74 }
75 }
76
77
78 class TikTokIE(TikTokBaseIE):
79 _VALID_URL = r'https?://www\.tiktok\.com/@[\w\._]+/video/(?P<id>\d+)'
80
81 _TESTS = [{
82 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610',
83 'md5': '34a7543afd5a151b0840ba6736fb633b',
84 'info_dict': {
85 'comment_count': int,
86 'creator': 'facestoriesbyleenabh',
87 'description': 'md5:a9f6c0c44a1ff2249cae610372d0ae95',
88 'duration': 13,
89 'ext': 'mp4',
90 'formats': list,
91 'height': 1280,
92 'id': '6748451240264420610',
93 'like_count': int,
94 'repost_count': int,
95 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
96 'thumbnails': list,
97 'timestamp': 1571246252,
98 'title': 'facestoriesbyleenabh on TikTok',
99 'upload_date': '20191016',
100 'uploader': 'leenabhushan',
101 'uploader_id': '6691488002098119685',
102 'uploader_url': r're:https://www.tiktok.com/@leenabhushan',
103 'webpage_url': r're:https://www.tiktok.com/@leenabhushan/(video/)?6748451240264420610',
104 'width': 720,
105 }
106 }, {
107 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en',
108 'md5': '06b9800d47d5fe51a19e322dd86e61c9',
109 'info_dict': {
110 'comment_count': int,
111 'creator': 'patroX',
112 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94',
113 'duration': 27,
114 'ext': 'mp4',
115 'formats': list,
116 'height': 960,
117 'id': '6742501081818877190',
118 'like_count': int,
119 'repost_count': int,
120 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
121 'thumbnails': list,
122 'timestamp': 1569860870,
123 'title': 'patroX on TikTok',
124 'upload_date': '20190930',
125 'uploader': 'patroxofficial',
126 'uploader_id': '18702747',
127 'uploader_url': r're:https://www.tiktok.com/@patroxofficial',
128 'webpage_url': r're:https://www.tiktok.com/@patroxofficial/(video/)?6742501081818877190',
129 'width': 540,
130 }
131 }]
132
133 def _real_extract(self, url):
134 video_id = self._match_id(url)
135
136 # If we only call once, we get a 403 when downlaoding the video.
137 webpage = self._download_webpage(url, video_id, note='Downloading video webpage')
138 webpage = self._download_webpage(url, video_id, note='Downloading video webpage')
139 json_string = self._search_regex(
140 r'id=\"__NEXT_DATA__\"\s+type=\"application\/json\"\s*[^>]+>\s*(?P<json_string_ld>[^<]+)',
141 webpage, 'json_string', group='json_string_ld')
142 json_data = self._parse_json(json_string, video_id)
143 props_data = try_get(json_data, lambda x: x['props'], expected_type=dict)
144
145 # Chech statusCode for success
146 if props_data.get('pageProps').get('statusCode') == 0:
147 return self._extract_aweme(props_data, webpage, url)
148
149 raise ExtractorError('Video not available', video_id=video_id)