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