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