7 from .common
import InfoExtractor
8 from ..compat
import compat_urllib_parse_unquote
, compat_urllib_parse_urlparse
21 srt_subtitles_timecode
,
29 class TikTokBaseIE(InfoExtractor
):
30 _APP_VERSIONS
= [('26.1.3', '260103'), ('26.1.2', '260102'), ('26.1.1', '260101'), ('25.6.2', '250602')]
31 _WORKING_APP_VERSION
= None
34 _UPLOADER_URL_FORMAT
= 'https://www.tiktok.com/@%s'
35 _WEBPAGE_HOST
= 'https://www.tiktok.com/'
36 QUALITIES
= ('360p', '540p', '720p', '1080p')
39 def _API_HOSTNAME(self
):
40 return self
._configuration
_arg
(
41 'api_hostname', ['api16-normal-c-useast1a.tiktokv.com'], ie_key
=TikTokIE
)[0]
44 def _create_url(user_id
, video_id
):
45 return f
'https://www.tiktok.com/@{user_id or "_"}/video/{video_id}'
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
)
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'):
53 self
._set
_cookie
(self
._API
_HOSTNAME
, 'odin_tt', ''.join(random
.choices('0123456789abcdef', k
=160)))
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
={
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)',
61 'Accept': 'application/json',
64 def _build_api_query(self
, query
, app_version
, manifest_app_version
):
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
,
72 'openudid': ''.join(random
.choices('0123456789abcdef', k
=16)),
73 'uuid': ''.join(random
.choices(string
.digits
, k
=16)),
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',
83 'carrier_region': 'US',
86 'app_name': self
._APP
_NAME
,
89 'timezone_name': 'America/New_York',
90 'timezone_offset': '-14400',
91 'channel': 'googleplay',
98 'cp': 'cbfhckdckkde1',
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)
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
)
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
)
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
)
123 except ExtractorError
as e
:
124 if isinstance(e
.cause
, json
.JSONDecodeError
) and e
.cause
.pos
== 0:
125 if count
== len(self
._APP
_VERSIONS
):
129 self
.report_warning(str(e
.cause
or e
.msg
))
131 self
.report_warning('%s. Retrying... (attempt %s of %s)' % (str(e
.cause
or e
.msg
), count
, len(self
._APP
_VERSIONS
)))
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)
141 raise ExtractorError('Unable to find video in feed', video_id
=aweme_id
)
142 return self
._parse
_aweme
_video
_app
(aweme_detail
)
144 def _get_subtitles(self
, aweme_detail
, aweme_id
):
145 # TODO: Extract text positioning info
147 # aweme/detail endpoint subs
148 captions_info
= traverse_obj(
149 aweme_detail
, ('interaction_stickers', ..., 'auto_video_caption_info', 'auto_captions', ...), expected_type
=dict)
150 for caption
in captions_info
:
151 caption_url
= traverse_obj(caption
, ('url', 'url_list', ...), expected_type
=url_or_none
, get_all
=False)
154 caption_json
= self
._download
_json
(
155 caption_url
, aweme_id
, note
='Downloading captions', errnote
='Unable to download captions', fatal
=False)
158 subtitles
.setdefault(caption
.get('language', 'en'), []).append({
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'))
166 for caption
in traverse_obj(aweme_detail
, ('video', 'cla_info', 'caption_infos', ...), expected_type
=dict):
167 if not caption
.get('url'):
169 subtitles
.setdefault(caption
.get('lang') or 'en', []).append({
170 'ext': remove_start(caption
.get('caption_format'), 'web'),
171 'url': caption
['url'],
175 for caption
in traverse_obj(aweme_detail
, ('video', 'subtitleInfos', ...), expected_type
=dict):
176 if not caption
.get('Url'):
178 subtitles
.setdefault(caption
.get('LanguageCodeName') or 'en', []).append({
179 'ext': remove_start(caption
.get('Format'), 'web'),
180 'url': caption
['Url'],
184 def _parse_aweme_video_app(self
, aweme_detail
):
185 aweme_id
= aweme_detail
['aweme_id']
186 video_info
= aweme_detail
['video']
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'))
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
),
201 known_resolutions
= {}
203 def extract_addr(addr
, add_meta
={}):
204 parsed_meta
, res
= parse_url_key(addr
.get('url_key', ''))
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]))
212 'filesize': int_or_none(addr
.get('data_size')),
215 'source_preference': -2 if 'aweme/v1' in url
else -1, # Downloads from API might get blocked
216 **add_meta
, **parsed_meta
,
217 'format_note': join_nonempty(
218 add_meta
.get('format_note'), '(API)' if 'aweme/v1' in url
else None, delim
=' ')
219 } for url
in addr
.get('url_list') or []]
221 # Hack: Add direct video links first to prioritize them when removing duplicate 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(
228 video_info
, 'is_bytevc1', 'is_h265') else 'h264', # TODO: Check for "direct iOS" videos, like https://www.tiktok.com/@cookierun_dev/video/7039716639834656002
229 'width': video_info
.get('width'),
230 'height': video_info
.get('height'),
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 ''),
237 'width': video_info
.get('width'),
238 'height': video_info
.get('height'),
239 'preference': -2 if video_info
.get('has_watermark') else -1,
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',
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',
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',
262 'fps': bitrate
.get('FPS'),
265 self
._remove
_duplicate
_formats
(formats
)
266 auth_cookie
= self
._get
_cookies
(self
._WEBPAGE
_HOST
).get('sid_tt')
269 self
._set
_cookie
(compat_urllib_parse_urlparse(f
['url']).hostname
, 'sid_tt', auth_cookie
.value
)
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
)
276 for cover_url
in cover
['url_list']:
282 stats_info
= aweme_detail
.get('statistics', {})
283 author_info
= aweme_detail
.get('author', {})
284 music_info
= aweme_detail
.get('music', {})
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))
288 labels
= traverse_obj(aweme_detail
, ('hybrid_label', ..., 'text'), expected_type
=str)
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)
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
299 music_track
, music_author
= music_info
.get('title'), music_info
.get('author')
303 'extractor_key': TikTokIE
.ie_key(),
304 'extractor': TikTokIE
.IE_NAME
,
305 'webpage_url': self
._create
_url
(author_info
.get('uid'), aweme_id
),
306 'title': aweme_detail
.get('desc'),
307 'description': aweme_detail
.get('desc'),
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')),
313 'creator': str_or_none(author_info
.get('nickname')),
314 'uploader_id': str_or_none(author_info
.get('uid')),
315 'uploader_url': user_url
,
316 'track': music_track
,
317 'album': str_or_none(music_info
.get('album')) or None,
318 'artist': music_author
or None,
319 'timestamp': int_or_none(aweme_detail
.get('create_time')),
321 'subtitles': self
.extract_subtitles(aweme_detail
, aweme_id
),
322 'thumbnails': thumbnails
,
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
,
327 is_unlisted
='Followers only' in labels
),
328 '_format_sort_fields': ('quality', 'codec', 'size', 'br'),
331 def _parse_aweme_video_web(self
, aweme_detail
, webpage_url
):
332 video_info
= aweme_detail
['video']
333 author_info
= traverse_obj(aweme_detail
, 'authorInfo', 'author', expected_type
=dict, default
={})
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',
338 expected_type
=str_or_none
, get_all
=False)
339 or aweme_detail
.get('authorSecId'))
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):
347 'url': self
._proto
_relative
_url
(play_url
),
352 elif isinstance(play_url
, list):
354 'url': self
._proto
_relative
_url
(url
),
358 } for url
in traverse_obj(play_url
, (..., 'src'), expected_type
=url_or_none
) if url
]
360 download_url
= url_or_none(video_info
.get('downloadAddr')) or traverse_obj(video_info
, ('download', 'url'), expected_type
=url_or_none
)
363 'format_id': 'download',
364 'url': self
._proto
_relative
_url
(download_url
),
369 self
._remove
_duplicate
_formats
(formats
)
372 for thumbnail_name
in ('thumbnail', 'cover', 'dynamicCover', 'originCover'):
373 if aweme_detail
.get(thumbnail_name
):
375 'url': self
._proto
_relative
_url
(aweme_detail
[thumbnail_name
]),
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')),
390 'uploader': str_or_none(author_info
.get('uniqueId') or aweme_detail
.get('author')),
391 'uploader_id': str_or_none(traverse_obj(author_info
, 'id', 'uid', 'authorId')),
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')),
397 'thumbnails': thumbnails
,
398 'description': str_or_none(aweme_detail
.get('desc')),
400 'Referer': webpage_url
405 class TikTokIE(TikTokBaseIE
):
406 _VALID_URL
= r
'https?://www\.tiktok\.com/(?:embed|@(?P<user_id>[\w\.-]+)?/video)/(?P<id>\d+)'
407 _EMBED_REGEX
= [rf
'<(?:script|iframe)[^>]+\bsrc=(["\'])(?P<url>{_VALID_URL})']
410 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610',
411 'md5': '736bb7a466c6f0a6afeb597da1e6f5b7',
413 'id': '6748451240264420610',
415 'title': '#jassmanak #lehanga #leenabhushan',
416 'description': '#jassmanak #lehanga #leenabhushan',
420 'uploader': 'leenabhushan',
421 'uploader_id': '6691488002098119685',
422 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA_Eb4t1vodM1IuTy_cvp9CY22RAb59xqrO0Xtz9CYQJvgXaDvZxYnZYRzDWhhgJmy',
423 'creator': 'facestoriesbyleenabh',
424 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
425 'upload_date': '20191016',
426 'timestamp': 1571246252,
430 'comment_count': int,
431 'artist': 'Ysrbeats',
436 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en',
437 'md5': '6f3cf8cdd9b28cb8363fe0a9a160695b',
439 'id': '6742501081818877190',
441 'title': 'md5:5e2a23877420bb85ce6521dbee39ba94',
442 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94',
446 'uploader': 'patrox',
447 'uploader_id': '18702747',
448 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws',
450 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
451 'upload_date': '20190930',
452 'timestamp': 1569860870,
456 'comment_count': int,
457 'artist': 'Evan Todd, Jessica Keenan Wynn, Alice Lee, Barrett Wilbert Weed & Jon Eidson',
461 # Banned audio, only available on the app
462 'url': 'https://www.tiktok.com/@barudakhb_/video/6984138651336838402',
464 'id': '6984138651336838402',
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,
476 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
477 'upload_date': '20210712',
481 'comment_count': int,
484 # Sponsored video, only available with feed workaround
485 'url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_/video/7042692929109986561',
487 'id': '7042692929109986561',
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,
498 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
499 'upload_date': '20211217',
503 'comment_count': int,
505 'expected_warnings': ['trying with webpage', 'Unable to find video in feed']
507 # Video without title and description
508 'url': 'https://www.tiktok.com/@pokemonlife22/video/7059698374567611694',
510 'id': '7059698374567611694',
512 'title': 'TikTok video #7059698374567611694',
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,
521 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
522 'upload_date': '20220201',
527 'comment_count': int,
530 # hydration JSON is sent in a <script> element
531 'url': 'https://www.tiktok.com/@denidil6/video/7065799023130643713',
533 'id': '7065799023130643713',
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,
545 'upload_date': '20220217',
549 'comment_count': int,
551 'skip': 'This video is unavailable',
553 # Auto-captions available
554 'url': 'https://www.tiktok.com/@hankgreen1/video/7047596209028074758',
555 'only_matching': True
558 def _real_extract(self
, url
):
559 video_id
, user_id
= self
._match
_valid
_url
(url
).group('id', 'user_id')
561 return self
._extract
_aweme
_app
(video_id
)
562 except ExtractorError
as e
:
563 self
.report_warning(f
'{e}; trying with webpage')
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'}
)
567 next_data
= self
._search
_nextjs
_data
(webpage
, video_id
, default
='{}')
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)
572 sigi_data
= self
._get
_sigi
_state
(webpage
, video_id
)
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)
577 return self
._parse
_aweme
_video
_web
(video_data
, url
)
578 elif status
== 10216:
579 raise ExtractorError('This video is private', expected
=True)
580 raise ExtractorError('Video not available', video_id
=video_id
)
583 class TikTokUserIE(TikTokBaseIE
):
584 IE_NAME
= 'tiktok:user'
585 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/@(?P<id>[\w\.-]+)/?(?:$|[#?])'
588 'url': 'https://tiktok.com/@corgibobaa?lang=en',
589 'playlist_mincount': 45,
591 'id': '6935371178089399301',
592 'title': 'corgibobaa',
593 'thumbnail': r
're:https://.+_1080x1080\.webp'
595 'expected_warnings': ['Retrying']
597 'url': 'https://www.tiktok.com/@6820838815978423302',
598 'playlist_mincount': 5,
600 'id': '6820838815978423302',
601 'title': '6820838815978423302',
602 'thumbnail': r
're:https://.+_1080x1080\.webp'
604 'expected_warnings': ['Retrying']
606 'url': 'https://www.tiktok.com/@meme',
607 'playlist_mincount': 593,
609 'id': '79005827461758976',
611 'thumbnail': r
're:https://.+_1080x1080\.webp'
613 'expected_warnings': ['Retrying']
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)
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='
624 for page in itertools.count():
625 data_json = self._download_json(api_url + cursor, username, note='Downloading Page %d' % page)
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}'
629 yield self._url_result(video_url, 'TikTok', video_id, str_or_none(video.get('desc')))
630 if not data_json.get('hasMore'):
632 cursor = data_json['cursor']
635 def _video_entries_api(self
, webpage
, user_id
, username
):
641 'retry_type': 'no_retry',
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.
645 for page
in itertools
.count(1):
646 for retry
in self
.RetryManager():
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')
651 except ExtractorError
as e
:
652 if isinstance(e
.cause
, json
.JSONDecodeError
) and e
.cause
.pos
== 0:
656 yield from post_list
.get('aweme_list', [])
657 if not post_list
.get('has_more'):
659 query
['max_cursor'] = post_list
['max_cursor']
661 def _entries_api(self
, user_id
, videos
):
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"]}',
670 def _real_extract(self
, url
):
671 user_name
= self
._match
_id
(url
)
672 webpage
= self
._download
_webpage
(url
, user_name
, headers
={
673 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
675 user_id
= self
._html
_search
_regex
(r
'snssdk\d*://user/profile/(\d+)', webpage
, 'user ID', default
=None) or user_name
677 videos
= LazyList(self
._video
_entries
_api
(webpage
, user_id
, user_name
))
678 thumbnail
= traverse_obj(videos
, (0, 'author', 'avatar_larger', 'url_list', 0))
680 return self
.playlist_result(self
._entries
_api
(user_id
, videos
), user_id
, user_name
, thumbnail
=thumbnail
)
683 class TikTokBaseListIE(TikTokBaseIE
): # XXX: Conventionally, base classes should end with BaseIE/InfoExtractor
684 def _entries(self
, list_id
, display_id
):
686 self
._QUERY
_NAME
: list_id
,
690 'device_id': ''.join(random
.choices(string
.digits
, k
=19))
693 for page
in itertools
.count(1):
694 for retry
in self
.RetryManager():
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')
699 except ExtractorError
as e
:
700 if isinstance(e
.cause
, json
.JSONDecodeError
) and e
.cause
.pos
== 0:
704 for video
in post_list
.get('aweme_list', []):
706 **self
._parse
_aweme
_video
_app
(video
),
707 'extractor_key': TikTokIE
.ie_key(),
708 'extractor': 'TikTok',
709 'webpage_url': f
'https://tiktok.com/@_/video/{video["aweme_id"]}',
711 if not post_list
.get('has_more'):
713 query
['cursor'] = post_list
['cursor']
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
)
720 class TikTokSoundIE(TikTokBaseListIE
):
721 IE_NAME
= 'tiktok:sound'
722 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/music/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
724 _QUERY_NAME
= 'music_id'
725 _API_ENDPOINT
= 'music/aweme'
727 'url': 'https://www.tiktok.com/music/Build-a-Btch-6956990112127585029?lang=en',
728 'playlist_mincount': 100,
730 'id': '6956990112127585029'
732 'expected_warnings': ['Retrying']
734 # Actual entries are less than listed video count
735 'url': 'https://www.tiktok.com/music/jiefei-soap-remix-7036843036118469381',
736 'playlist_mincount': 2182,
738 'id': '7036843036118469381'
740 'expected_warnings': ['Retrying']
744 class TikTokEffectIE(TikTokBaseListIE
):
745 IE_NAME
= 'tiktok:effect'
746 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/sticker/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
748 _QUERY_NAME
= 'sticker_id'
749 _API_ENDPOINT
= 'sticker/aweme'
751 'url': 'https://www.tiktok.com/sticker/MATERIAL-GWOOORL-1258156',
752 'playlist_mincount': 100,
756 'expected_warnings': ['Retrying']
758 # Different entries between mobile and web, depending on region
759 'url': 'https://www.tiktok.com/sticker/Elf-Friend-479565',
760 'only_matching': True
764 class TikTokTagIE(TikTokBaseListIE
):
765 IE_NAME
= 'tiktok:tag'
766 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/tag/(?P<id>[^/?#&]+)'
768 _QUERY_NAME
= 'ch_id'
769 _API_ENDPOINT
= 'challenge/aweme'
771 'url': 'https://tiktok.com/tag/hello2018',
772 'playlist_mincount': 39,
775 'title': 'hello2018',
777 'expected_warnings': ['Retrying']
779 'url': 'https://tiktok.com/tag/fypシ?is_copy_url=0&is_from_webapp=v1',
780 'only_matching': True
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)'
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
)
792 class DouyinIE(TikTokBaseIE
):
793 _VALID_URL
= r
'https?://(?:www\.)?douyin\.com/video/(?P<id>[0-9]+)'
795 'url': 'https://www.douyin.com/video/6961737553342991651',
796 'md5': 'a97db7e3e67eb57bf40735c022ffa228',
798 'id': '6961737553342991651',
800 'title': '#杨超越 小小水手带你去远航❤️',
801 'description': '#杨超越 小小水手带你去远航❤️',
802 'uploader_id': '110403406559',
803 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
806 'timestamp': 1620905839,
807 'upload_date': '20210513',
808 'track': '@杨超越创作的原声',
812 'comment_count': int,
815 'url': 'https://www.douyin.com/video/6982497745948921092',
816 'md5': '34a87ebff3833357733da3fe17e37c0e',
818 'id': '6982497745948921092',
820 'title': '这个夏日和小羊@杨超越 一起遇见白色幻想',
821 'description': '这个夏日和小羊@杨超越 一起遇见白色幻想',
822 'uploader_id': '408654318141572',
823 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAZJpnglcjW2f_CMVcnqA_6oVBXKWMpH0F8LIHuUu8-lA',
826 'timestamp': 1625739481,
827 'upload_date': '20210708',
828 'track': '@杨超越工作室创作的原声',
832 'comment_count': int,
835 'url': 'https://www.douyin.com/video/6953975910773099811',
836 'md5': 'dde3302460f19db59c47060ff013b902',
838 'id': '6953975910773099811',
840 'title': '#一起看海 出现在你的夏日里',
841 'description': '#一起看海 出现在你的夏日里',
842 'uploader_id': '110403406559',
843 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
846 'timestamp': 1619098692,
847 'upload_date': '20210422',
848 'track': '@杨超越创作的原声',
852 'comment_count': int,
855 'url': 'https://www.douyin.com/video/6950251282489675042',
856 'md5': 'b4db86aec367ef810ddd38b1737d2fed',
858 'id': '6950251282489675042',
860 'title': '哈哈哈,成功了哈哈哈哈哈哈',
862 'upload_date': '20210412',
863 'timestamp': 1618231483,
864 'uploader_id': '110403406559',
868 'comment_count': int,
870 'skip': 'No longer available',
872 'url': 'https://www.douyin.com/video/6963263655114722595',
873 'md5': 'cf9f11f0ec45d131445ec2f06766e122',
875 'id': '6963263655114722595',
877 'title': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
878 'description': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
879 'uploader_id': '110403406559',
880 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
883 'timestamp': 1621261163,
884 'upload_date': '20210517',
885 'track': '@杨超越创作的原声',
889 'comment_count': int,
892 _APP_VERSIONS
= [('23.3.0', '230300')]
895 _API_HOSTNAME
= 'aweme.snssdk.com'
896 _UPLOADER_URL_FORMAT
= 'https://www.douyin.com/user/%s'
897 _WEBPAGE_HOST
= 'https://www.douyin.com/'
899 def _real_extract(self
, url
):
900 video_id
= self
._match
_id
(url
)
903 return self
._extract
_aweme
_app
(video_id
)
904 except ExtractorError
as e
:
906 self
.to_screen(f
'{e}; trying with webpage')
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
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)
919 render_data = self._parse_json(
920 render_data_json, video_id, transform_source=compat_urllib_parse_unquote)
921 return self._parse_aweme_video_web(get_first(render_data, ('aweme
', 'detail
')), url)
924 class TikTokVMIE(InfoExtractor):
925 _VALID_URL = r'https?
://(?
:(?
:vm|vt
)\
.tiktok\
.com|
(?
:www\
.)tiktok\
.com
/t
)/(?P
<id>\w
+)'
926 IE_NAME = 'vm
.tiktok
'
929 'url
': 'https
://www
.tiktok
.com
/t
/ZTRC5xgJp
',
931 'id': '7170520270497680683',
933 'title
': 'md5
:c64f6152330c2efe98093ccc8597871c
',
934 'uploader_id
': '6687535061741700102',
935 'upload_date
': '20221127',
938 'comment_count
': int,
939 'uploader_url
': 'https
://www
.tiktok
.com
/@MS4wLjABAAAAObqu3WCTXxmw2xwZ3iLEHnEecEIw7ks6rxWqOqOhaPja9BI7gqUQnjw8_5FSoDXX',
940 'album
': 'Wave of Mutilation
: Best of Pixies
',
941 'thumbnail
': r're
:https
://.+\
.webp
.*',
943 'timestamp
': 1669516858,
946 'track
': 'Where Is My Mind?
',
947 'description
': 'md5
:c64f6152330c2efe98093ccc8597871c
',
948 'uploader
': 'sigmachaddeus
',
949 'creator
': 'SigmaChad
',
952 'url
': 'https
://vm
.tiktok
.com
/ZTR45GpSF
/',
954 'id': '7106798200794926362',
956 'title
': 'md5
:edc3e7ea587847f8537468f2fe51d074
',
957 'uploader_id
': '6997695878846268418',
958 'upload_date
': '20220608',
961 'comment_count
': int,
962 'thumbnail
': r're
:https
://.+\
.webp
.*',
963 'uploader_url
': 'https
://www
.tiktok
.com
/@MS4wLjABAAAAdZ_NcPPgMneaGrW0hN8O_J_bwLshwNNERRF5DxOw2HKIzk0kdlLrR8RkVl1ksrMO',
965 'timestamp
': 1654680400,
967 'artist
': 'Akihitoko
',
968 'track
': 'original sound
',
969 'description
': 'md5
:edc3e7ea587847f8537468f2fe51d074
',
970 'uploader
': 'akihitoko1
',
971 'creator
': 'Akihitoko
',
974 'url
': 'https
://vt
.tiktok
.com
/ZSe4FqkKd
',
975 'only_matching
': True,
978 def _real_extract(self, url):
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)
986 class TikTokLiveIE(InfoExtractor):
987 _VALID_URL = r'https?
://(?
:www\
.)?tiktok\
.com
/@(?P
<id>[\w\
.-]+)/live
'
988 IE_NAME = 'tiktok
:live
'
991 'url
': 'https
://www
.tiktok
.com
/@iris04201/live
',
992 'only_matching
': True,
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)
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={
1005 }), 'LiveRoomInfo
', expected_type=dict, default={})
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)
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),