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