]>
Commit | Line | Data |
---|---|---|
f7f18f90 | 1 | import itertools |
b801cd71 | 2 | import json |
bd9ff55b | 3 | import random |
b801cd71 | 4 | import re |
bd9ff55b M |
5 | import string |
6 | import time | |
1ead840d KS |
7 | |
8 | from .common import InfoExtractor | |
b801cd71 | 9 | from ..compat import compat_urllib_parse_unquote, compat_urllib_parse_urlparse |
1ead840d | 10 | from ..utils import ( |
ce18a19b | 11 | ExtractorError, |
88afe056 | 12 | HEADRequest, |
b801cd71 | 13 | LazyList, |
11e1c2e3 | 14 | UnsupportedError, |
a39a7ba8 | 15 | get_element_by_id, |
ff91cf74 | 16 | get_first, |
1ead840d | 17 | int_or_none, |
34921b43 | 18 | join_nonempty, |
b801cd71 | 19 | qualities, |
e0585e65 | 20 | srt_subtitles_timecode, |
1ead840d | 21 | str_or_none, |
bd9ff55b M |
22 | traverse_obj, |
23 | try_get, | |
943d5ab1 | 24 | url_or_none, |
1ead840d KS |
25 | ) |
26 | ||
27 | ||
0fd6661e | 28 | class TikTokBaseIE(InfoExtractor): |
046cab39 M |
29 | _APP_VERSIONS = [('20.9.3', '293'), ('20.4.3', '243'), ('20.2.1', '221'), ('20.1.2', '212'), ('20.0.4', '204')] |
30 | _WORKING_APP_VERSION = None | |
943d5ab1 M |
31 | _APP_NAME = 'trill' |
32 | _AID = 1180 | |
53dad39e | 33 | _API_HOSTNAME = 'api-h2.tiktokv.com' |
943d5ab1 | 34 | _UPLOADER_URL_FORMAT = 'https://www.tiktok.com/@%s' |
53dad39e | 35 | _WEBPAGE_HOST = 'https://www.tiktok.com/' |
be1f331f | 36 | QUALITIES = ('360p', '540p', '720p', '1080p') |
a39a7ba8 | 37 | _session_initialized = False |
ce18a19b | 38 | |
b801cd71 | 39 | @staticmethod |
40 | def _create_url(user_id, video_id): | |
41 | return f'https://www.tiktok.com/@{user_id or "_"}/video/{video_id}' | |
42 | ||
a39a7ba8 | 43 | def _get_sigi_state(self, webpage, display_id): |
44 | return self._parse_json(get_element_by_id( | |
45 | 'SIGI_STATE|sigi-persisted-data', webpage, escape_value=False), display_id) | |
46 | ||
47 | def _real_initialize(self): | |
48 | if self._session_initialized: | |
49 | return | |
50 | self._request_webpage(HEADRequest('https://www.tiktok.com'), None, note='Setting up session', fatal=False) | |
51 | TikTokBaseIE._session_initialized = True | |
52 | ||
046cab39 M |
53 | def _call_api_impl(self, ep, query, manifest_app_version, video_id, fatal=True, |
54 | note='Downloading API JSON', errnote='Unable to download API page'): | |
55 | self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choice('0123456789abcdef') for _ in range(160))) | |
56 | webpage_cookies = self._get_cookies(self._WEBPAGE_HOST) | |
57 | if webpage_cookies.get('sid_tt'): | |
58 | self._set_cookie(self._API_HOSTNAME, 'sid_tt', webpage_cookies['sid_tt'].value) | |
59 | return self._download_json( | |
60 | 'https://%s/aweme/v1/%s/' % (self._API_HOSTNAME, ep), video_id=video_id, | |
61 | fatal=fatal, note=note, errnote=errnote, headers={ | |
62 | 'User-Agent': f'com.ss.android.ugc.trill/{manifest_app_version} (Linux; U; Android 10; en_US; Pixel 4; Build/QQ3A.200805.001; Cronet/58.0.2991.0)', | |
63 | 'Accept': 'application/json', | |
64 | }, query=query) | |
65 | ||
66 | def _build_api_query(self, query, app_version, manifest_app_version): | |
67 | return { | |
0fd6661e | 68 | **query, |
046cab39 M |
69 | 'version_name': app_version, |
70 | 'version_code': manifest_app_version, | |
71 | 'build_number': app_version, | |
72 | 'manifest_version_code': manifest_app_version, | |
73 | 'update_version_code': manifest_app_version, | |
0930b11f | 74 | 'openudid': ''.join(random.choice('0123456789abcdef') for _ in range(16)), |
75 | 'uuid': ''.join([random.choice(string.digits) for _ in range(16)]), | |
bd9ff55b M |
76 | '_rticket': int(time.time() * 1000), |
77 | 'ts': int(time.time()), | |
78 | 'device_brand': 'Google', | |
79 | 'device_type': 'Pixel 4', | |
80 | 'device_platform': 'android', | |
81 | 'resolution': '1080*1920', | |
82 | 'dpi': 420, | |
83 | 'os_version': '10', | |
84 | 'os_api': '29', | |
85 | 'carrier_region': 'US', | |
86 | 'sys_region': 'US', | |
87 | 'region': 'US', | |
943d5ab1 | 88 | 'app_name': self._APP_NAME, |
bd9ff55b M |
89 | 'app_language': 'en', |
90 | 'language': 'en', | |
91 | 'timezone_name': 'America/New_York', | |
92 | 'timezone_offset': '-14400', | |
93 | 'channel': 'googleplay', | |
94 | 'ac': 'wifi', | |
95 | 'mcc_mnc': '310260', | |
96 | 'is_my_cn': 0, | |
943d5ab1 | 97 | 'aid': self._AID, |
bd9ff55b M |
98 | 'ssmix': 'a', |
99 | 'as': 'a1qwert123', | |
100 | 'cp': 'cbfhckdckkde1', | |
101 | } | |
046cab39 M |
102 | |
103 | def _call_api(self, ep, query, video_id, fatal=True, | |
104 | note='Downloading API JSON', errnote='Unable to download API page'): | |
105 | if not self._WORKING_APP_VERSION: | |
106 | app_version = self._configuration_arg('app_version', [''], ie_key=TikTokIE.ie_key())[0] | |
107 | manifest_app_version = self._configuration_arg('manifest_app_version', [''], ie_key=TikTokIE.ie_key())[0] | |
108 | if app_version and manifest_app_version: | |
109 | self._WORKING_APP_VERSION = (app_version, manifest_app_version) | |
110 | self.write_debug('Imported app version combo from extractor arguments') | |
111 | elif app_version or manifest_app_version: | |
112 | self.report_warning('Only one of the two required version params are passed as extractor arguments', only_once=True) | |
113 | ||
114 | if self._WORKING_APP_VERSION: | |
115 | app_version, manifest_app_version = self._WORKING_APP_VERSION | |
116 | real_query = self._build_api_query(query, app_version, manifest_app_version) | |
117 | return self._call_api_impl(ep, real_query, manifest_app_version, video_id, fatal, note, errnote) | |
118 | ||
119 | for count, (app_version, manifest_app_version) in enumerate(self._APP_VERSIONS, start=1): | |
120 | real_query = self._build_api_query(query, app_version, manifest_app_version) | |
121 | try: | |
122 | res = self._call_api_impl(ep, real_query, manifest_app_version, video_id, fatal, note, errnote) | |
123 | self._WORKING_APP_VERSION = (app_version, manifest_app_version) | |
124 | return res | |
125 | except ExtractorError as e: | |
126 | if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0: | |
127 | if count == len(self._APP_VERSIONS): | |
128 | if fatal: | |
129 | raise e | |
130 | else: | |
131 | self.report_warning(str(e.cause or e.msg)) | |
132 | return | |
133 | self.report_warning('%s. Retrying... (attempt %s of %s)' % (str(e.cause or e.msg), count, len(self._APP_VERSIONS))) | |
134 | continue | |
135 | raise e | |
0fd6661e | 136 | |
e0585e65 M |
137 | def _get_subtitles(self, aweme_detail, aweme_id): |
138 | # TODO: Extract text positioning info | |
139 | subtitles = {} | |
140 | captions_info = traverse_obj( | |
141 | aweme_detail, ('interaction_stickers', ..., 'auto_video_caption_info', 'auto_captions', ...), expected_type=dict, default=[]) | |
142 | for caption in captions_info: | |
143 | caption_url = traverse_obj(caption, ('url', 'url_list', ...), expected_type=url_or_none, get_all=False) | |
144 | if not caption_url: | |
145 | continue | |
146 | caption_json = self._download_json( | |
147 | caption_url, aweme_id, note='Downloading captions', errnote='Unable to download captions', fatal=False) | |
148 | if not caption_json: | |
149 | continue | |
150 | subtitles.setdefault(caption.get('language', 'en'), []).append({ | |
151 | 'ext': 'srt', | |
152 | 'data': '\n\n'.join( | |
153 | f'{i + 1}\n{srt_subtitles_timecode(line["start_time"] / 1000)} --> {srt_subtitles_timecode(line["end_time"] / 1000)}\n{line["text"]}' | |
154 | for i, line in enumerate(caption_json['utterances']) if line.get('text')) | |
155 | }) | |
156 | return subtitles | |
157 | ||
943d5ab1 | 158 | def _parse_aweme_video_app(self, aweme_detail): |
0fd6661e | 159 | aweme_id = aweme_detail['aweme_id'] |
bd9ff55b M |
160 | video_info = aweme_detail['video'] |
161 | ||
162 | def parse_url_key(url_key): | |
163 | format_id, codec, res, bitrate = self._search_regex( | |
164 | r'v[^_]+_(?P<id>(?P<codec>[^_]+)_(?P<res>\d+p)_(?P<bitrate>\d+))', url_key, | |
165 | 'url key', default=(None, None, None, None), group=('id', 'codec', 'res', 'bitrate')) | |
166 | if not format_id: | |
167 | return {}, None | |
168 | return { | |
169 | 'format_id': format_id, | |
170 | 'vcodec': 'h265' if codec == 'bytevc1' else codec, | |
171 | 'tbr': int_or_none(bitrate, scale=1000) or None, | |
172 | 'quality': qualities(self.QUALITIES)(res), | |
173 | }, res | |
174 | ||
175 | known_resolutions = {} | |
176 | ||
177 | def extract_addr(addr, add_meta={}): | |
178 | parsed_meta, res = parse_url_key(addr.get('url_key', '')) | |
179 | if res: | |
180 | known_resolutions.setdefault(res, {}).setdefault('height', add_meta.get('height')) | |
181 | known_resolutions[res].setdefault('width', add_meta.get('width')) | |
182 | parsed_meta.update(known_resolutions.get(res, {})) | |
183 | add_meta.setdefault('height', int_or_none(res[:-1])) | |
184 | return [{ | |
185 | 'url': url, | |
186 | 'filesize': int_or_none(addr.get('data_size')), | |
187 | 'ext': 'mp4', | |
188 | 'acodec': 'aac', | |
0fd6661e M |
189 | 'source_preference': -2 if 'aweme/v1' in url else -1, # Downloads from API might get blocked |
190 | **add_meta, **parsed_meta, | |
34921b43 | 191 | 'format_note': join_nonempty( |
192 | add_meta.get('format_note'), '(API)' if 'aweme/v1' in url else None, delim=' ') | |
bd9ff55b M |
193 | } for url in addr.get('url_list') or []] |
194 | ||
195 | # Hack: Add direct video links first to prioritize them when removing duplicate formats | |
196 | formats = [] | |
197 | if video_info.get('play_addr'): | |
198 | formats.extend(extract_addr(video_info['play_addr'], { | |
199 | 'format_id': 'play_addr', | |
200 | 'format_note': 'Direct video', | |
201 | 'vcodec': 'h265' if traverse_obj( | |
be1f331f | 202 | video_info, 'is_bytevc1', 'is_h265') else 'h264', # TODO: Check for "direct iOS" videos, like https://www.tiktok.com/@cookierun_dev/video/7039716639834656002 |
bd9ff55b M |
203 | 'width': video_info.get('width'), |
204 | 'height': video_info.get('height'), | |
205 | })) | |
206 | if video_info.get('download_addr'): | |
207 | formats.extend(extract_addr(video_info['download_addr'], { | |
208 | 'format_id': 'download_addr', | |
209 | 'format_note': 'Download video%s' % (', watermarked' if video_info.get('has_watermark') else ''), | |
210 | 'vcodec': 'h264', | |
211 | 'width': video_info.get('width'), | |
212 | 'height': video_info.get('height'), | |
0fd6661e | 213 | 'preference': -2 if video_info.get('has_watermark') else -1, |
bd9ff55b M |
214 | })) |
215 | if video_info.get('play_addr_h264'): | |
216 | formats.extend(extract_addr(video_info['play_addr_h264'], { | |
217 | 'format_id': 'play_addr_h264', | |
218 | 'format_note': 'Direct video', | |
219 | 'vcodec': 'h264', | |
220 | })) | |
221 | if video_info.get('play_addr_bytevc1'): | |
222 | formats.extend(extract_addr(video_info['play_addr_bytevc1'], { | |
223 | 'format_id': 'play_addr_bytevc1', | |
224 | 'format_note': 'Direct video', | |
225 | 'vcodec': 'h265', | |
226 | })) | |
227 | ||
228 | for bitrate in video_info.get('bit_rate', []): | |
229 | if bitrate.get('play_addr'): | |
230 | formats.extend(extract_addr(bitrate['play_addr'], { | |
231 | 'format_id': bitrate.get('gear_name'), | |
232 | 'format_note': 'Playback video', | |
233 | 'tbr': try_get(bitrate, lambda x: x['bit_rate'] / 1000), | |
234 | 'vcodec': 'h265' if traverse_obj( | |
235 | bitrate, 'is_bytevc1', 'is_h265') else 'h264', | |
943d5ab1 | 236 | 'fps': bitrate.get('FPS'), |
bd9ff55b M |
237 | })) |
238 | ||
239 | self._remove_duplicate_formats(formats) | |
6134fbeb M |
240 | auth_cookie = self._get_cookies(self._WEBPAGE_HOST).get('sid_tt') |
241 | if auth_cookie: | |
242 | for f in formats: | |
be1f331f | 243 | self._set_cookie(compat_urllib_parse_urlparse(f['url']).hostname, 'sid_tt', auth_cookie.value) |
0fd6661e | 244 | self._sort_formats(formats, ('quality', 'codec', 'size', 'br')) |
bd9ff55b M |
245 | |
246 | thumbnails = [] | |
247 | for cover_id in ('cover', 'ai_dynamic_cover', 'animated_cover', 'ai_dynamic_cover_bak', | |
248 | 'origin_cover', 'dynamic_cover'): | |
249 | cover = video_info.get(cover_id) | |
250 | if cover: | |
251 | for cover_url in cover['url_list']: | |
252 | thumbnails.append({ | |
253 | 'id': cover_id, | |
254 | 'url': cover_url, | |
255 | }) | |
256 | ||
257 | stats_info = aweme_detail.get('statistics', {}) | |
258 | author_info = aweme_detail.get('author', {}) | |
259 | music_info = aweme_detail.get('music', {}) | |
943d5ab1 M |
260 | user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info, |
261 | 'sec_uid', 'id', 'uid', 'unique_id', | |
262 | expected_type=str_or_none, get_all=False)) | |
a8549f19 | 263 | labels = traverse_obj(aweme_detail, ('hybrid_label', ..., 'text'), expected_type=str, default=[]) |
bd9ff55b M |
264 | |
265 | contained_music_track = traverse_obj( | |
266 | music_info, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type=str) | |
267 | contained_music_author = traverse_obj( | |
268 | music_info, ('matched_song', 'author'), ('matched_pgc_sound', 'author'), 'author', expected_type=str) | |
269 | ||
270 | is_generic_og_trackname = music_info.get('is_original_sound') and music_info.get('title') == 'original sound - %s' % music_info.get('owner_handle') | |
271 | if is_generic_og_trackname: | |
272 | music_track, music_author = contained_music_track or 'original sound', contained_music_author | |
273 | else: | |
274 | music_track, music_author = music_info.get('title'), music_info.get('author') | |
275 | ||
276 | return { | |
277 | 'id': aweme_id, | |
a39a7ba8 | 278 | 'extractor_key': TikTokIE.ie_key(), |
279 | 'extractor': TikTokIE.IE_NAME, | |
280 | 'webpage_url': self._create_url(author_info.get('uid'), aweme_id), | |
5fa3c9a8 | 281 | 'title': aweme_detail.get('desc'), |
a44ca5a4 | 282 | 'description': aweme_detail.get('desc'), |
bd9ff55b M |
283 | 'view_count': int_or_none(stats_info.get('play_count')), |
284 | 'like_count': int_or_none(stats_info.get('digg_count')), | |
285 | 'repost_count': int_or_none(stats_info.get('share_count')), | |
286 | 'comment_count': int_or_none(stats_info.get('comment_count')), | |
287 | 'uploader': str_or_none(author_info.get('unique_id')), | |
943d5ab1 | 288 | 'creator': str_or_none(author_info.get('nickname')), |
bd9ff55b | 289 | 'uploader_id': str_or_none(author_info.get('uid')), |
943d5ab1 | 290 | 'uploader_url': user_url, |
bd9ff55b M |
291 | 'track': music_track, |
292 | 'album': str_or_none(music_info.get('album')) or None, | |
293 | 'artist': music_author, | |
294 | 'timestamp': int_or_none(aweme_detail.get('create_time')), | |
295 | 'formats': formats, | |
e0585e65 | 296 | 'subtitles': self.extract_subtitles(aweme_detail, aweme_id), |
bd9ff55b | 297 | 'thumbnails': thumbnails, |
53dad39e M |
298 | 'duration': int_or_none(traverse_obj(video_info, 'duration', ('download_addr', 'duration')), scale=1000), |
299 | 'availability': self._availability( | |
300 | is_private='Private' in labels, | |
301 | needs_subscription='Friends only' in labels, | |
302 | is_unlisted='Followers only' in labels) | |
bd9ff55b M |
303 | } |
304 | ||
0481e266 | 305 | def _parse_aweme_video_web(self, aweme_detail, webpage_url): |
943d5ab1 | 306 | video_info = aweme_detail['video'] |
11aa91a1 | 307 | author_info = traverse_obj(aweme_detail, 'authorInfo', 'author', expected_type=dict, default={}) |
943d5ab1 M |
308 | music_info = aweme_detail.get('music') or {} |
309 | stats_info = aweme_detail.get('stats') or {} | |
310 | user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info, | |
311 | 'secUid', 'id', 'uid', 'uniqueId', | |
11aa91a1 M |
312 | expected_type=str_or_none, get_all=False) |
313 | or aweme_detail.get('authorSecId')) | |
943d5ab1 M |
314 | |
315 | formats = [] | |
316 | play_url = video_info.get('playAddr') | |
317 | width = video_info.get('width') | |
318 | height = video_info.get('height') | |
319 | if isinstance(play_url, str): | |
320 | formats = [{ | |
321 | 'url': self._proto_relative_url(play_url), | |
322 | 'ext': 'mp4', | |
323 | 'width': width, | |
324 | 'height': height, | |
325 | }] | |
326 | elif isinstance(play_url, list): | |
327 | formats = [{ | |
328 | 'url': self._proto_relative_url(url), | |
329 | 'ext': 'mp4', | |
330 | 'width': width, | |
331 | 'height': height, | |
332 | } for url in traverse_obj(play_url, (..., 'src'), expected_type=url_or_none, default=[]) if url] | |
333 | ||
334 | download_url = url_or_none(video_info.get('downloadAddr')) or traverse_obj(video_info, ('download', 'url'), expected_type=url_or_none) | |
335 | if download_url: | |
336 | formats.append({ | |
337 | 'format_id': 'download', | |
338 | 'url': self._proto_relative_url(download_url), | |
339 | 'ext': 'mp4', | |
340 | 'width': width, | |
341 | 'height': height, | |
342 | }) | |
343 | self._remove_duplicate_formats(formats) | |
344 | self._sort_formats(formats) | |
345 | ||
346 | thumbnails = [] | |
347 | for thumbnail_name in ('thumbnail', 'cover', 'dynamicCover', 'originCover'): | |
348 | if aweme_detail.get(thumbnail_name): | |
349 | thumbnails = [{ | |
350 | 'url': self._proto_relative_url(aweme_detail[thumbnail_name]), | |
351 | 'width': width, | |
352 | 'height': height | |
353 | }] | |
354 | ||
355 | return { | |
356 | 'id': traverse_obj(aweme_detail, 'id', 'awemeId', expected_type=str_or_none), | |
357 | 'title': aweme_detail.get('desc'), | |
358 | 'duration': try_get(aweme_detail, lambda x: x['video']['duration'], int), | |
359 | 'view_count': int_or_none(stats_info.get('playCount')), | |
360 | 'like_count': int_or_none(stats_info.get('diggCount')), | |
361 | 'repost_count': int_or_none(stats_info.get('shareCount')), | |
362 | 'comment_count': int_or_none(stats_info.get('commentCount')), | |
363 | 'timestamp': int_or_none(aweme_detail.get('createTime')), | |
364 | 'creator': str_or_none(author_info.get('nickname')), | |
11aa91a1 M |
365 | 'uploader': str_or_none(author_info.get('uniqueId') or aweme_detail.get('author')), |
366 | 'uploader_id': str_or_none(author_info.get('id') or aweme_detail.get('authorId')), | |
943d5ab1 M |
367 | 'uploader_url': user_url, |
368 | 'track': str_or_none(music_info.get('title')), | |
369 | 'album': str_or_none(music_info.get('album')) or None, | |
370 | 'artist': str_or_none(music_info.get('authorName')), | |
371 | 'formats': formats, | |
372 | 'thumbnails': thumbnails, | |
373 | 'description': str_or_none(aweme_detail.get('desc')), | |
374 | 'http_headers': { | |
0481e266 | 375 | 'Referer': webpage_url |
943d5ab1 M |
376 | } |
377 | } | |
378 | ||
0fd6661e M |
379 | |
380 | class TikTokIE(TikTokBaseIE): | |
b801cd71 | 381 | _VALID_URL = r'https?://www\.tiktok\.com/(?:embed|@(?P<user_id>[\w\.-]+)/video)/(?P<id>\d+)' |
0fd6661e M |
382 | |
383 | _TESTS = [{ | |
384 | 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610', | |
0481e266 | 385 | 'md5': '736bb7a466c6f0a6afeb597da1e6f5b7', |
0fd6661e M |
386 | 'info_dict': { |
387 | 'id': '6748451240264420610', | |
388 | 'ext': 'mp4', | |
389 | 'title': '#jassmanak #lehanga #leenabhushan', | |
390 | 'description': '#jassmanak #lehanga #leenabhushan', | |
391 | 'duration': 13, | |
0481e266 | 392 | 'height': 1024, |
393 | 'width': 576, | |
0fd6661e M |
394 | 'uploader': 'leenabhushan', |
395 | 'uploader_id': '6691488002098119685', | |
0481e266 | 396 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA_Eb4t1vodM1IuTy_cvp9CY22RAb59xqrO0Xtz9CYQJvgXaDvZxYnZYRzDWhhgJmy', |
0fd6661e M |
397 | 'creator': 'facestoriesbyleenabh', |
398 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
399 | 'upload_date': '20191016', | |
400 | 'timestamp': 1571246252, | |
401 | 'view_count': int, | |
402 | 'like_count': int, | |
403 | 'repost_count': int, | |
404 | 'comment_count': int, | |
a44ca5a4 | 405 | 'artist': 'Ysrbeats', |
406 | 'album': 'Lehanga', | |
407 | 'track': 'Lehanga', | |
0fd6661e M |
408 | } |
409 | }, { | |
410 | 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en', | |
0481e266 | 411 | 'md5': '6f3cf8cdd9b28cb8363fe0a9a160695b', |
0fd6661e M |
412 | 'info_dict': { |
413 | 'id': '6742501081818877190', | |
414 | 'ext': 'mp4', | |
415 | 'title': 'md5:5e2a23877420bb85ce6521dbee39ba94', | |
416 | 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94', | |
417 | 'duration': 27, | |
418 | 'height': 960, | |
419 | 'width': 540, | |
420 | 'uploader': 'patrox', | |
421 | 'uploader_id': '18702747', | |
0481e266 | 422 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws', |
0fd6661e M |
423 | 'creator': 'patroX', |
424 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
425 | 'upload_date': '20190930', | |
426 | 'timestamp': 1569860870, | |
427 | 'view_count': int, | |
428 | 'like_count': int, | |
429 | 'repost_count': int, | |
430 | 'comment_count': int, | |
a44ca5a4 | 431 | 'artist': 'Evan Todd, Jessica Keenan Wynn, Alice Lee, Barrett Wilbert Weed & Jon Eidson', |
432 | 'track': 'Big Fun', | |
0fd6661e M |
433 | } |
434 | }, { | |
96f13f01 M |
435 | # Banned audio, only available on the app |
436 | 'url': 'https://www.tiktok.com/@barudakhb_/video/6984138651336838402', | |
437 | 'info_dict': { | |
438 | 'id': '6984138651336838402', | |
439 | 'ext': 'mp4', | |
440 | 'title': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥', | |
441 | 'description': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥', | |
442 | 'uploader': 'barudakhb_', | |
443 | 'creator': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6', | |
444 | 'uploader_id': '6974687867511718913', | |
445 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAbhBwQC-R1iKoix6jDFsF-vBdfx2ABoDjaZrM9fX6arU3w71q3cOWgWuTXn1soZ7d', | |
446 | 'track': 'Boka Dance', | |
447 | 'artist': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6', | |
448 | 'timestamp': 1626121503, | |
449 | 'duration': 18, | |
450 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
451 | 'upload_date': '20210712', | |
452 | 'view_count': int, | |
453 | 'like_count': int, | |
454 | 'repost_count': int, | |
455 | 'comment_count': int, | |
456 | } | |
457 | }, { | |
458 | # Sponsored video, only available with feed workaround | |
459 | 'url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_/video/7042692929109986561', | |
460 | 'info_dict': { | |
461 | 'id': '7042692929109986561', | |
462 | 'ext': 'mp4', | |
463 | 'title': 'Slap and Run!', | |
464 | 'description': 'Slap and Run!', | |
465 | 'uploader': 'user440922249', | |
466 | 'creator': 'Slap And Run', | |
467 | 'uploader_id': '7036055384943690754', | |
468 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_', | |
469 | 'track': 'Promoted Music', | |
470 | 'timestamp': 1639754738, | |
471 | 'duration': 30, | |
472 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
473 | 'upload_date': '20211217', | |
474 | 'view_count': int, | |
475 | 'like_count': int, | |
476 | 'repost_count': int, | |
477 | 'comment_count': int, | |
478 | }, | |
a39a7ba8 | 479 | 'expected_warnings': ['trying with webpage', 'Unable to find video in feed'] |
5fa3c9a8 HTL |
480 | }, { |
481 | # Video without title and description | |
482 | 'url': 'https://www.tiktok.com/@pokemonlife22/video/7059698374567611694', | |
483 | 'info_dict': { | |
484 | 'id': '7059698374567611694', | |
485 | 'ext': 'mp4', | |
b801cd71 | 486 | 'title': 'TikTok video #7059698374567611694', |
5fa3c9a8 HTL |
487 | 'description': '', |
488 | 'uploader': 'pokemonlife22', | |
489 | 'creator': 'Pokemon', | |
490 | 'uploader_id': '6820838815978423302', | |
491 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA0tF1nBwQVVMyrGu3CqttkNgM68Do1OXUFuCY0CRQk8fEtSVDj89HqoqvbSTmUP2W', | |
492 | 'track': 'original sound', | |
493 | 'timestamp': 1643714123, | |
494 | 'duration': 6, | |
495 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
496 | 'upload_date': '20220201', | |
497 | 'artist': 'Pokemon', | |
498 | 'view_count': int, | |
499 | 'like_count': int, | |
500 | 'repost_count': int, | |
501 | 'comment_count': int, | |
502 | }, | |
a39a7ba8 | 503 | }, { |
504 | # hydration JSON is sent in a <script> element | |
505 | 'url': 'https://www.tiktok.com/@denidil6/video/7065799023130643713', | |
506 | 'info_dict': { | |
507 | 'id': '7065799023130643713', | |
508 | 'ext': 'mp4', | |
509 | 'title': '#denidil#денидил', | |
510 | 'description': '#denidil#денидил', | |
511 | 'uploader': 'denidil6', | |
512 | 'uploader_id': '7046664115636405250', | |
513 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAsvMSzFdQ4ikl3uR2TEJwMBbB2yZh2Zxwhx-WCo3rbDpAharE3GQCrFuJArI3C8QJ', | |
514 | 'artist': 'Holocron Music', | |
515 | 'album': 'Wolf Sounds (1 Hour) Enjoy the Company of the Animal That Is the Majestic King of the Night', | |
516 | 'track': 'Wolf Sounds (1 Hour) Enjoy the Company of the Animal That Is the Majestic King of the Night', | |
517 | 'timestamp': 1645134536, | |
518 | 'duration': 26, | |
519 | 'upload_date': '20220217', | |
520 | 'view_count': int, | |
521 | 'like_count': int, | |
522 | 'repost_count': int, | |
523 | 'comment_count': int, | |
524 | }, | |
525 | 'expected_warnings': ['trying feed workaround', 'Unable to find video in feed'] | |
e0585e65 M |
526 | }, { |
527 | # Auto-captions available | |
528 | 'url': 'https://www.tiktok.com/@hankgreen1/video/7047596209028074758', | |
529 | 'only_matching': True | |
0fd6661e M |
530 | }] |
531 | ||
b801cd71 | 532 | @classmethod |
533 | def _extract_urls(cls, webpage): | |
534 | return [mobj.group('url') for mobj in re.finditer( | |
535 | rf'<(?:script|iframe)[^>]+\bsrc=(["\'])(?P<url>{cls._VALID_URL})', webpage)] | |
536 | ||
0fd6661e | 537 | def _extract_aweme_app(self, aweme_id): |
e540c56f M |
538 | try: |
539 | aweme_detail = self._call_api('aweme/detail', {'aweme_id': aweme_id}, aweme_id, | |
540 | note='Downloading video details', errnote='Unable to download video details').get('aweme_detail') | |
541 | if not aweme_detail: | |
542 | raise ExtractorError('Video not available', video_id=aweme_id) | |
543 | except ExtractorError as e: | |
a39a7ba8 | 544 | self.report_warning(f'{e.orig_msg}; trying feed workaround') |
e540c56f M |
545 | feed_list = self._call_api('feed', {'aweme_id': aweme_id}, aweme_id, |
546 | note='Downloading video feed', errnote='Unable to download video feed').get('aweme_list') or [] | |
be1f331f | 547 | aweme_detail = next((aweme for aweme in feed_list if str(aweme.get('aweme_id')) == aweme_id), None) |
e540c56f M |
548 | if not aweme_detail: |
549 | raise ExtractorError('Unable to find video in feed', video_id=aweme_id) | |
943d5ab1 | 550 | return self._parse_aweme_video_app(aweme_detail) |
0fd6661e | 551 | |
ce18a19b | 552 | def _real_extract(self, url): |
b801cd71 | 553 | video_id, user_id = self._match_valid_url(url).group('id', 'user_id') |
bd9ff55b M |
554 | try: |
555 | return self._extract_aweme_app(video_id) | |
556 | except ExtractorError as e: | |
a39a7ba8 | 557 | self.report_warning(f'{e}; trying with webpage') |
bd9ff55b | 558 | |
a39a7ba8 | 559 | url = self._create_url(user_id, video_id) |
560 | webpage = self._download_webpage(url, video_id, headers={'User-Agent': 'User-Agent:Mozilla/5.0'}) | |
135dfa2c | 561 | next_data = self._search_nextjs_data(webpage, video_id, default='{}') |
135dfa2c | 562 | if next_data: |
11aa91a1 M |
563 | status = traverse_obj(next_data, ('props', 'pageProps', 'statusCode'), expected_type=int) or 0 |
564 | video_data = traverse_obj(next_data, ('props', 'pageProps', 'itemInfo', 'itemStruct'), expected_type=dict) | |
565 | else: | |
a39a7ba8 | 566 | sigi_data = self._get_sigi_state(webpage, video_id) |
11aa91a1 M |
567 | status = traverse_obj(sigi_data, ('VideoPage', 'statusCode'), expected_type=int) or 0 |
568 | video_data = traverse_obj(sigi_data, ('ItemModule', video_id), expected_type=dict) | |
569 | ||
1418a043 | 570 | if status == 0: |
11aa91a1 | 571 | return self._parse_aweme_video_web(video_data, url) |
1418a043 | 572 | elif status == 10216: |
573 | raise ExtractorError('This video is private', expected=True) | |
6fb11ca8 | 574 | raise ExtractorError('Video not available', video_id=video_id) |
f7f18f90 A |
575 | |
576 | ||
0fd6661e | 577 | class TikTokUserIE(TikTokBaseIE): |
f7f18f90 | 578 | IE_NAME = 'tiktok:user' |
0fd6661e | 579 | _VALID_URL = r'https?://(?:www\.)?tiktok\.com/@(?P<id>[\w\.-]+)/?(?:$|[#?])' |
f7f18f90 | 580 | _TESTS = [{ |
526d74ec | 581 | 'url': 'https://tiktok.com/@corgibobaa?lang=en', |
f7f18f90 A |
582 | 'playlist_mincount': 45, |
583 | 'info_dict': { | |
584 | 'id': '6935371178089399301', | |
0481e266 | 585 | 'title': 'corgibobaa', |
b3187433 | 586 | 'thumbnail': r're:https://.+_1080x1080\.webp' |
f7f18f90 | 587 | }, |
0481e266 | 588 | 'expected_warnings': ['Retrying'] |
5fa3c9a8 HTL |
589 | }, { |
590 | 'url': 'https://www.tiktok.com/@6820838815978423302', | |
591 | 'playlist_mincount': 5, | |
592 | 'info_dict': { | |
593 | 'id': '6820838815978423302', | |
594 | 'title': '6820838815978423302', | |
595 | 'thumbnail': r're:https://.+_1080x1080\.webp' | |
596 | }, | |
597 | 'expected_warnings': ['Retrying'] | |
f7f18f90 A |
598 | }, { |
599 | 'url': 'https://www.tiktok.com/@meme', | |
600 | 'playlist_mincount': 593, | |
601 | 'info_dict': { | |
602 | 'id': '79005827461758976', | |
0481e266 | 603 | 'title': 'meme', |
b3187433 | 604 | 'thumbnail': r're:https://.+_1080x1080\.webp' |
f7f18f90 | 605 | }, |
0481e266 | 606 | 'expected_warnings': ['Retrying'] |
f7f18f90 A |
607 | }] |
608 | ||
0fd6661e M |
609 | r''' # TODO: Fix by adding _signature to api_url |
610 | def _entries(self, webpage, user_id, username): | |
611 | secuid = self._search_regex(r'\"secUid\":\"(?P<secUid>[^\"]+)', webpage, username) | |
f7f18f90 A |
612 | verifyfp_cookie = self._get_cookies('https://www.tiktok.com').get('s_v_web_id') |
613 | if not verifyfp_cookie: | |
614 | raise ExtractorError('Improper cookies (missing s_v_web_id).', expected=True) | |
615 | api_url = f'https://m.tiktok.com/api/post/item_list/?aid=1988&cookie_enabled=true&count=30&verifyFp={verifyfp_cookie.value}&secUid={secuid}&cursor=' | |
616 | cursor = '0' | |
617 | for page in itertools.count(): | |
0fd6661e | 618 | data_json = self._download_json(api_url + cursor, username, note='Downloading Page %d' % page) |
f7f18f90 A |
619 | for video in data_json.get('itemList', []): |
620 | video_id = video['id'] | |
621 | video_url = f'https://www.tiktok.com/@{user_id}/video/{video_id}' | |
bd9ff55b | 622 | yield self._url_result(video_url, 'TikTok', video_id, str_or_none(video.get('desc'))) |
526d74ec | 623 | if not data_json.get('hasMore'): |
f7f18f90 A |
624 | break |
625 | cursor = data_json['cursor'] | |
0fd6661e M |
626 | ''' |
627 | ||
b3187433 | 628 | def _video_entries_api(self, webpage, user_id, username): |
0fd6661e M |
629 | query = { |
630 | 'user_id': user_id, | |
631 | 'count': 21, | |
632 | 'max_cursor': 0, | |
633 | 'min_cursor': 0, | |
634 | 'retry_type': 'no_retry', | |
0930b11f | 635 | 'device_id': ''.join(random.choice(string.digits) for _ in range(19)), # Some endpoints don't like randomized device_id, so it isn't directly set in _call_api. |
0fd6661e M |
636 | } |
637 | ||
638 | max_retries = self.get_param('extractor_retries', 3) | |
639 | for page in itertools.count(1): | |
640 | for retries in itertools.count(): | |
641 | try: | |
642 | post_list = self._call_api('aweme/post', query, username, | |
643 | note='Downloading user video list page %d%s' % (page, f' (attempt {retries})' if retries != 0 else ''), | |
644 | errnote='Unable to download user video list') | |
645 | except ExtractorError as e: | |
646 | if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0 and retries != max_retries: | |
647 | self.report_warning('%s. Retrying...' % str(e.cause or e.msg)) | |
648 | continue | |
649 | raise | |
650 | break | |
b3187433 | 651 | yield from post_list.get('aweme_list', []) |
0fd6661e M |
652 | if not post_list.get('has_more'): |
653 | break | |
654 | query['max_cursor'] = post_list['max_cursor'] | |
f7f18f90 | 655 | |
b3187433 | 656 | def _entries_api(self, user_id, videos): |
657 | for video in videos: | |
658 | yield { | |
659 | **self._parse_aweme_video_app(video), | |
660 | 'extractor_key': TikTokIE.ie_key(), | |
661 | 'extractor': 'TikTok', | |
662 | 'webpage_url': f'https://tiktok.com/@{user_id}/video/{video["aweme_id"]}', | |
663 | } | |
664 | ||
f7f18f90 | 665 | def _real_extract(self, url): |
0481e266 | 666 | user_name = self._match_id(url) |
667 | webpage = self._download_webpage(url, user_name, headers={ | |
0fd6661e M |
668 | 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' |
669 | }) | |
5fa3c9a8 | 670 | user_id = self._html_search_regex(r'snssdk\d*://user/profile/(\d+)', webpage, 'user ID', default=None) or user_name |
b3187433 | 671 | |
672 | videos = LazyList(self._video_entries_api(webpage, user_id, user_name)) | |
673 | thumbnail = traverse_obj(videos, (0, 'author', 'avatar_larger', 'url_list', 0)) | |
674 | ||
675 | return self.playlist_result(self._entries_api(user_id, videos), user_id, user_name, thumbnail=thumbnail) | |
943d5ab1 M |
676 | |
677 | ||
8126298c M |
678 | class TikTokBaseListIE(TikTokBaseIE): |
679 | def _entries(self, list_id, display_id): | |
680 | query = { | |
681 | self._QUERY_NAME: list_id, | |
682 | 'cursor': 0, | |
683 | 'count': 20, | |
684 | 'type': 5, | |
685 | 'device_id': ''.join(random.choice(string.digits) for i in range(19)) | |
686 | } | |
687 | ||
688 | max_retries = self.get_param('extractor_retries', 3) | |
689 | for page in itertools.count(1): | |
690 | for retries in itertools.count(): | |
691 | try: | |
692 | post_list = self._call_api(self._API_ENDPOINT, query, display_id, | |
693 | note='Downloading video list page %d%s' % (page, f' (attempt {retries})' if retries != 0 else ''), | |
694 | errnote='Unable to download video list') | |
695 | except ExtractorError as e: | |
696 | if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0 and retries != max_retries: | |
697 | self.report_warning('%s. Retrying...' % str(e.cause or e.msg)) | |
698 | continue | |
699 | raise | |
700 | break | |
701 | for video in post_list.get('aweme_list', []): | |
702 | yield { | |
703 | **self._parse_aweme_video_app(video), | |
0b77924a | 704 | 'extractor_key': TikTokIE.ie_key(), |
8126298c M |
705 | 'extractor': 'TikTok', |
706 | 'webpage_url': f'https://tiktok.com/@_/video/{video["aweme_id"]}', | |
707 | } | |
708 | if not post_list.get('has_more'): | |
709 | break | |
710 | query['cursor'] = post_list['cursor'] | |
711 | ||
712 | def _real_extract(self, url): | |
713 | list_id = self._match_id(url) | |
714 | return self.playlist_result(self._entries(list_id, list_id), list_id) | |
715 | ||
716 | ||
717 | class TikTokSoundIE(TikTokBaseListIE): | |
718 | IE_NAME = 'tiktok:sound' | |
719 | _VALID_URL = r'https?://(?:www\.)?tiktok\.com/music/[\w\.-]+-(?P<id>[\d]+)[/?#&]?' | |
720 | _QUERY_NAME = 'music_id' | |
721 | _API_ENDPOINT = 'music/aweme' | |
722 | _TESTS = [{ | |
723 | 'url': 'https://www.tiktok.com/music/Build-a-Btch-6956990112127585029?lang=en', | |
724 | 'playlist_mincount': 100, | |
725 | 'info_dict': { | |
726 | 'id': '6956990112127585029' | |
727 | }, | |
728 | 'expected_warnings': ['Retrying'] | |
729 | }, { | |
730 | # Actual entries are less than listed video count | |
731 | 'url': 'https://www.tiktok.com/music/jiefei-soap-remix-7036843036118469381', | |
732 | 'playlist_mincount': 2182, | |
733 | 'info_dict': { | |
734 | 'id': '7036843036118469381' | |
735 | }, | |
736 | 'expected_warnings': ['Retrying'] | |
737 | }] | |
738 | ||
739 | ||
740 | class TikTokEffectIE(TikTokBaseListIE): | |
741 | IE_NAME = 'tiktok:effect' | |
742 | _VALID_URL = r'https?://(?:www\.)?tiktok\.com/sticker/[\w\.-]+-(?P<id>[\d]+)[/?#&]?' | |
743 | _QUERY_NAME = 'sticker_id' | |
744 | _API_ENDPOINT = 'sticker/aweme' | |
745 | _TESTS = [{ | |
746 | 'url': 'https://www.tiktok.com/sticker/MATERIAL-GWOOORL-1258156', | |
747 | 'playlist_mincount': 100, | |
748 | 'info_dict': { | |
749 | 'id': '1258156', | |
750 | }, | |
751 | 'expected_warnings': ['Retrying'] | |
752 | }, { | |
753 | # Different entries between mobile and web, depending on region | |
754 | 'url': 'https://www.tiktok.com/sticker/Elf-Friend-479565', | |
755 | 'only_matching': True | |
756 | }] | |
757 | ||
758 | ||
759 | class TikTokTagIE(TikTokBaseListIE): | |
760 | IE_NAME = 'tiktok:tag' | |
761 | _VALID_URL = r'https?://(?:www\.)?tiktok\.com/tag/(?P<id>[^/?#&]+)' | |
762 | _QUERY_NAME = 'ch_id' | |
763 | _API_ENDPOINT = 'challenge/aweme' | |
764 | _TESTS = [{ | |
765 | 'url': 'https://tiktok.com/tag/hello2018', | |
766 | 'playlist_mincount': 39, | |
767 | 'info_dict': { | |
768 | 'id': '46294678', | |
769 | 'title': 'hello2018', | |
770 | }, | |
771 | 'expected_warnings': ['Retrying'] | |
772 | }, { | |
773 | 'url': 'https://tiktok.com/tag/fypシ?is_copy_url=0&is_from_webapp=v1', | |
774 | 'only_matching': True | |
775 | }] | |
776 | ||
777 | def _real_extract(self, url): | |
778 | display_id = self._match_id(url) | |
779 | webpage = self._download_webpage(url, display_id, headers={ | |
780 | 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' | |
781 | }) | |
782 | tag_id = self._html_search_regex(r'snssdk\d*://challenge/detail/(\d+)', webpage, 'tag ID') | |
783 | return self.playlist_result(self._entries(tag_id, display_id), tag_id, display_id) | |
784 | ||
785 | ||
943d5ab1 M |
786 | class DouyinIE(TikTokIE): |
787 | _VALID_URL = r'https?://(?:www\.)?douyin\.com/video/(?P<id>[0-9]+)' | |
788 | _TESTS = [{ | |
789 | 'url': 'https://www.douyin.com/video/6961737553342991651', | |
790 | 'md5': '10523312c8b8100f353620ac9dc8f067', | |
791 | 'info_dict': { | |
792 | 'id': '6961737553342991651', | |
793 | 'ext': 'mp4', | |
794 | 'title': '#杨超越 小小水手带你去远航❤️', | |
795 | 'uploader': '杨超越', | |
796 | 'upload_date': '20210513', | |
797 | 'timestamp': 1620905839, | |
798 | 'uploader_id': '110403406559', | |
799 | 'view_count': int, | |
800 | 'like_count': int, | |
801 | 'repost_count': int, | |
802 | 'comment_count': int, | |
803 | } | |
804 | }, { | |
805 | 'url': 'https://www.douyin.com/video/6982497745948921092', | |
806 | 'md5': 'd78408c984b9b5102904cf6b6bc2d712', | |
807 | 'info_dict': { | |
808 | 'id': '6982497745948921092', | |
809 | 'ext': 'mp4', | |
810 | 'title': '这个夏日和小羊@杨超越 一起遇见白色幻想', | |
811 | 'uploader': '杨超越工作室', | |
812 | 'upload_date': '20210708', | |
813 | 'timestamp': 1625739481, | |
814 | 'uploader_id': '408654318141572', | |
815 | 'view_count': int, | |
816 | 'like_count': int, | |
817 | 'repost_count': int, | |
818 | 'comment_count': int, | |
819 | } | |
820 | }, { | |
821 | 'url': 'https://www.douyin.com/video/6953975910773099811', | |
822 | 'md5': '72e882e24f75064c218b76c8b713c185', | |
823 | 'info_dict': { | |
824 | 'id': '6953975910773099811', | |
825 | 'ext': 'mp4', | |
826 | 'title': '#一起看海 出现在你的夏日里', | |
827 | 'uploader': '杨超越', | |
828 | 'upload_date': '20210422', | |
829 | 'timestamp': 1619098692, | |
830 | 'uploader_id': '110403406559', | |
831 | 'view_count': int, | |
832 | 'like_count': int, | |
833 | 'repost_count': int, | |
834 | 'comment_count': int, | |
835 | } | |
836 | }, { | |
837 | 'url': 'https://www.douyin.com/video/6950251282489675042', | |
838 | 'md5': 'b4db86aec367ef810ddd38b1737d2fed', | |
839 | 'info_dict': { | |
840 | 'id': '6950251282489675042', | |
841 | 'ext': 'mp4', | |
842 | 'title': '哈哈哈,成功了哈哈哈哈哈哈', | |
843 | 'uploader': '杨超越', | |
844 | 'upload_date': '20210412', | |
845 | 'timestamp': 1618231483, | |
846 | 'uploader_id': '110403406559', | |
847 | 'view_count': int, | |
848 | 'like_count': int, | |
849 | 'repost_count': int, | |
850 | 'comment_count': int, | |
851 | } | |
852 | }, { | |
853 | 'url': 'https://www.douyin.com/video/6963263655114722595', | |
854 | 'md5': '1abe1c477d05ee62efb40bf2329957cf', | |
855 | 'info_dict': { | |
856 | 'id': '6963263655114722595', | |
857 | 'ext': 'mp4', | |
858 | 'title': '#哪个爱豆的105度最甜 换个角度看看我哈哈', | |
859 | 'uploader': '杨超越', | |
860 | 'upload_date': '20210517', | |
861 | 'timestamp': 1621261163, | |
862 | 'uploader_id': '110403406559', | |
863 | 'view_count': int, | |
864 | 'like_count': int, | |
865 | 'repost_count': int, | |
866 | 'comment_count': int, | |
867 | } | |
868 | }] | |
046cab39 | 869 | _APP_VERSIONS = [('9.6.0', '960')] |
943d5ab1 M |
870 | _APP_NAME = 'aweme' |
871 | _AID = 1128 | |
872 | _API_HOSTNAME = 'aweme.snssdk.com' | |
873 | _UPLOADER_URL_FORMAT = 'https://www.douyin.com/user/%s' | |
53dad39e | 874 | _WEBPAGE_HOST = 'https://www.douyin.com/' |
943d5ab1 M |
875 | |
876 | def _real_extract(self, url): | |
877 | video_id = self._match_id(url) | |
878 | ||
879 | try: | |
880 | return self._extract_aweme_app(video_id) | |
881 | except ExtractorError as e: | |
a39a7ba8 | 882 | self.report_warning(f'{e}; trying with webpage') |
943d5ab1 M |
883 | |
884 | webpage = self._download_webpage(url, video_id) | |
885 | render_data_json = self._search_regex( | |
886 | r'<script [^>]*\bid=[\'"]RENDER_DATA[\'"][^>]*>(%7B.+%7D)</script>', | |
887 | webpage, 'render data', default=None) | |
888 | if not render_data_json: | |
889 | # TODO: Run verification challenge code to generate signature cookies | |
890 | raise ExtractorError('Fresh cookies (not necessarily logged in) are needed') | |
891 | ||
892 | render_data = self._parse_json( | |
893 | render_data_json, video_id, transform_source=compat_urllib_parse_unquote) | |
ff91cf74 | 894 | return self._parse_aweme_video_web(get_first(render_data, ('aweme', 'detail')), url) |
88afe056 | 895 | |
896 | ||
49895f06 | 897 | class TikTokVMIE(InfoExtractor): |
898 | _VALID_URL = r'https?://(?:vm|vt)\.tiktok\.com/(?P<id>\w+)' | |
88afe056 | 899 | IE_NAME = 'vm.tiktok' |
900 | ||
49895f06 | 901 | _TESTS = [{ |
902 | 'url': 'https://vm.tiktok.com/ZSe4FqkKd', | |
903 | 'info_dict': { | |
904 | 'id': '7023491746608712966', | |
905 | 'ext': 'mp4', | |
906 | 'title': 'md5:5607564db90271abbbf8294cca77eddd', | |
907 | 'description': 'md5:5607564db90271abbbf8294cca77eddd', | |
908 | 'duration': 11, | |
909 | 'upload_date': '20211026', | |
910 | 'uploader_id': '7007385080558846981', | |
911 | 'creator': 'Memes', | |
912 | 'artist': 'Memes', | |
913 | 'track': 'original sound', | |
914 | 'uploader': 'susmandem', | |
915 | 'timestamp': 1635284105, | |
916 | 'thumbnail': r're:https://.+\.webp.*', | |
917 | 'like_count': int, | |
918 | 'view_count': int, | |
919 | 'comment_count': int, | |
920 | 'repost_count': int, | |
921 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAXcNoOEOxVyBzuII_E--T0MeCrLP0ay1Sm6x_n3dluiWEoWZD0VlQOytwad4W0i0n', | |
922 | } | |
923 | }, { | |
924 | 'url': 'https://vt.tiktok.com/ZSe4FqkKd', | |
925 | 'only_matching': True, | |
926 | }] | |
927 | ||
88afe056 | 928 | def _real_extract(self, url): |
11e1c2e3 | 929 | new_url = self._request_webpage( |
930 | HEADRequest(url), self._match_id(url), headers={'User-Agent': 'facebookexternalhit/1.1'}).geturl() | |
931 | if self.suitable(new_url): # Prevent infinite loop in case redirect fails | |
932 | raise UnsupportedError(new_url) | |
933 | return self.url_result(new_url) |