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