2 from __future__
import unicode_literals
10 from .common
import InfoExtractor
11 from ..compat
import (
12 compat_urllib_parse_unquote
,
13 compat_urllib_parse_urlparse
21 srt_subtitles_timecode
,
30 class TikTokBaseIE(InfoExtractor
):
31 _APP_VERSIONS
= [('20.9.3', '293'), ('20.4.3', '243'), ('20.2.1', '221'), ('20.1.2', '212'), ('20.0.4', '204')]
32 _WORKING_APP_VERSION
= None
35 _API_HOSTNAME
= 'api-h2.tiktokv.com'
36 _UPLOADER_URL_FORMAT
= 'https://www.tiktok.com/@%s'
37 _WEBPAGE_HOST
= 'https://www.tiktok.com/'
38 QUALITIES
= ('360p', '540p', '720p', '1080p')
40 def _call_api_impl(self
, ep
, query
, manifest_app_version
, video_id
, fatal
=True,
41 note
='Downloading API JSON', errnote
='Unable to download API page'):
42 self
._set
_cookie
(self
._API
_HOSTNAME
, 'odin_tt', ''.join(random
.choice('0123456789abcdef') for _
in range(160)))
43 webpage_cookies
= self
._get
_cookies
(self
._WEBPAGE
_HOST
)
44 if webpage_cookies
.get('sid_tt'):
45 self
._set
_cookie
(self
._API
_HOSTNAME
, 'sid_tt', webpage_cookies
['sid_tt'].value
)
46 return self
._download
_json
(
47 'https://%s/aweme/v1/%s/' % (self
._API
_HOSTNAME
, ep
), video_id
=video_id
,
48 fatal
=fatal
, note
=note
, errnote
=errnote
, headers
={
49 'User-Agent': f
'com.ss.android.ugc.trill/{manifest_app_version} (Linux; U; Android 10; en_US; Pixel 4; Build/QQ3A.200805.001; Cronet/58.0.2991.0)',
50 'Accept': 'application/json',
53 def _build_api_query(self
, query
, app_version
, manifest_app_version
):
56 'version_name': app_version
,
57 'version_code': manifest_app_version
,
58 'build_number': app_version
,
59 'manifest_version_code': manifest_app_version
,
60 'update_version_code': manifest_app_version
,
61 'openudid': ''.join(random
.choice('0123456789abcdef') for _
in range(16)),
62 'uuid': ''.join([random
.choice(string
.digits
) for _
in range(16)]),
63 '_rticket': int(time
.time() * 1000),
64 'ts': int(time
.time()),
65 'device_brand': 'Google',
66 'device_type': 'Pixel 4',
67 'device_platform': 'android',
68 'resolution': '1080*1920',
72 'carrier_region': 'US',
75 'app_name': self
._APP
_NAME
,
78 'timezone_name': 'America/New_York',
79 'timezone_offset': '-14400',
80 'channel': 'googleplay',
87 'cp': 'cbfhckdckkde1',
90 def _call_api(self
, ep
, query
, video_id
, fatal
=True,
91 note
='Downloading API JSON', errnote
='Unable to download API page'):
92 if not self
._WORKING
_APP
_VERSION
:
93 app_version
= self
._configuration
_arg
('app_version', [''], ie_key
=TikTokIE
.ie_key())[0]
94 manifest_app_version
= self
._configuration
_arg
('manifest_app_version', [''], ie_key
=TikTokIE
.ie_key())[0]
95 if app_version
and manifest_app_version
:
96 self
._WORKING
_APP
_VERSION
= (app_version
, manifest_app_version
)
97 self
.write_debug('Imported app version combo from extractor arguments')
98 elif app_version
or manifest_app_version
:
99 self
.report_warning('Only one of the two required version params are passed as extractor arguments', only_once
=True)
101 if self
._WORKING
_APP
_VERSION
:
102 app_version
, manifest_app_version
= self
._WORKING
_APP
_VERSION
103 real_query
= self
._build
_api
_query
(query
, app_version
, manifest_app_version
)
104 return self
._call
_api
_impl
(ep
, real_query
, manifest_app_version
, video_id
, fatal
, note
, errnote
)
106 for count
, (app_version
, manifest_app_version
) in enumerate(self
._APP
_VERSIONS
, start
=1):
107 real_query
= self
._build
_api
_query
(query
, app_version
, manifest_app_version
)
109 res
= self
._call
_api
_impl
(ep
, real_query
, manifest_app_version
, video_id
, fatal
, note
, errnote
)
110 self
._WORKING
_APP
_VERSION
= (app_version
, manifest_app_version
)
112 except ExtractorError
as e
:
113 if isinstance(e
.cause
, json
.JSONDecodeError
) and e
.cause
.pos
== 0:
114 if count
== len(self
._APP
_VERSIONS
):
118 self
.report_warning(str(e
.cause
or e
.msg
))
120 self
.report_warning('%s. Retrying... (attempt %s of %s)' % (str(e
.cause
or e
.msg
), count
, len(self
._APP
_VERSIONS
)))
124 def _get_subtitles(self
, aweme_detail
, aweme_id
):
125 # TODO: Extract text positioning info
127 captions_info
= traverse_obj(
128 aweme_detail
, ('interaction_stickers', ..., 'auto_video_caption_info', 'auto_captions', ...), expected_type
=dict, default
=[])
129 for caption
in captions_info
:
130 caption_url
= traverse_obj(caption
, ('url', 'url_list', ...), expected_type
=url_or_none
, get_all
=False)
133 caption_json
= self
._download
_json
(
134 caption_url
, aweme_id
, note
='Downloading captions', errnote
='Unable to download captions', fatal
=False)
137 subtitles
.setdefault(caption
.get('language', 'en'), []).append({
140 f
'{i + 1}\n{srt_subtitles_timecode(line["start_time"] / 1000)} --> {srt_subtitles_timecode(line["end_time"] / 1000)}\n{line["text"]}'
141 for i
, line
in enumerate(caption_json
['utterances']) if line
.get('text'))
145 def _parse_aweme_video_app(self
, aweme_detail
):
146 aweme_id
= aweme_detail
['aweme_id']
147 video_info
= aweme_detail
['video']
149 def parse_url_key(url_key
):
150 format_id
, codec
, res
, bitrate
= self
._search
_regex
(
151 r
'v[^_]+_(?P<id>(?P<codec>[^_]+)_(?P<res>\d+p)_(?P<bitrate>\d+))', url_key
,
152 'url key', default
=(None, None, None, None), group
=('id', 'codec', 'res', 'bitrate'))
156 'format_id': format_id
,
157 'vcodec': 'h265' if codec
== 'bytevc1' else codec
,
158 'tbr': int_or_none(bitrate
, scale
=1000) or None,
159 'quality': qualities(self
.QUALITIES
)(res
),
162 known_resolutions
= {}
164 def extract_addr(addr
, add_meta
={}):
165 parsed_meta
, res
= parse_url_key(addr
.get('url_key', ''))
167 known_resolutions
.setdefault(res
, {}).setdefault('height', add_meta
.get('height'))
168 known_resolutions
[res
].setdefault('width', add_meta
.get('width'))
169 parsed_meta
.update(known_resolutions
.get(res
, {}))
170 add_meta
.setdefault('height', int_or_none(res
[:-1]))
173 'filesize': int_or_none(addr
.get('data_size')),
176 'source_preference': -2 if 'aweme/v1' in url
else -1, # Downloads from API might get blocked
177 **add_meta
, **parsed_meta
,
178 'format_note': join_nonempty(
179 add_meta
.get('format_note'), '(API)' if 'aweme/v1' in url
else None, delim
=' ')
180 } for url
in addr
.get('url_list') or []]
182 # Hack: Add direct video links first to prioritize them when removing duplicate formats
184 if video_info
.get('play_addr'):
185 formats
.extend(extract_addr(video_info
['play_addr'], {
186 'format_id': 'play_addr',
187 'format_note': 'Direct video',
188 'vcodec': 'h265' if traverse_obj(
189 video_info
, 'is_bytevc1', 'is_h265') else 'h264', # TODO: Check for "direct iOS" videos, like https://www.tiktok.com/@cookierun_dev/video/7039716639834656002
190 'width': video_info
.get('width'),
191 'height': video_info
.get('height'),
193 if video_info
.get('download_addr'):
194 formats
.extend(extract_addr(video_info
['download_addr'], {
195 'format_id': 'download_addr',
196 'format_note': 'Download video%s' % (', watermarked' if video_info
.get('has_watermark') else ''),
198 'width': video_info
.get('width'),
199 'height': video_info
.get('height'),
200 'preference': -2 if video_info
.get('has_watermark') else -1,
202 if video_info
.get('play_addr_h264'):
203 formats
.extend(extract_addr(video_info
['play_addr_h264'], {
204 'format_id': 'play_addr_h264',
205 'format_note': 'Direct video',
208 if video_info
.get('play_addr_bytevc1'):
209 formats
.extend(extract_addr(video_info
['play_addr_bytevc1'], {
210 'format_id': 'play_addr_bytevc1',
211 'format_note': 'Direct video',
215 for bitrate
in video_info
.get('bit_rate', []):
216 if bitrate
.get('play_addr'):
217 formats
.extend(extract_addr(bitrate
['play_addr'], {
218 'format_id': bitrate
.get('gear_name'),
219 'format_note': 'Playback video',
220 'tbr': try_get(bitrate
, lambda x
: x
['bit_rate'] / 1000),
221 'vcodec': 'h265' if traverse_obj(
222 bitrate
, 'is_bytevc1', 'is_h265') else 'h264',
223 'fps': bitrate
.get('FPS'),
226 self
._remove
_duplicate
_formats
(formats
)
227 auth_cookie
= self
._get
_cookies
(self
._WEBPAGE
_HOST
).get('sid_tt')
230 self
._set
_cookie
(compat_urllib_parse_urlparse(f
['url']).hostname
, 'sid_tt', auth_cookie
.value
)
231 self
._sort
_formats
(formats
, ('quality', 'codec', 'size', 'br'))
234 for cover_id
in ('cover', 'ai_dynamic_cover', 'animated_cover', 'ai_dynamic_cover_bak',
235 'origin_cover', 'dynamic_cover'):
236 cover
= video_info
.get(cover_id
)
238 for cover_url
in cover
['url_list']:
244 stats_info
= aweme_detail
.get('statistics', {})
245 author_info
= aweme_detail
.get('author', {})
246 music_info
= aweme_detail
.get('music', {})
247 user_url
= self
._UPLOADER
_URL
_FORMAT
% (traverse_obj(author_info
,
248 'sec_uid', 'id', 'uid', 'unique_id',
249 expected_type
=str_or_none
, get_all
=False))
250 labels
= traverse_obj(aweme_detail
, ('hybrid_label', ..., 'text'), expected_type
=str, default
=[])
252 contained_music_track
= traverse_obj(
253 music_info
, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type
=str)
254 contained_music_author
= traverse_obj(
255 music_info
, ('matched_song', 'author'), ('matched_pgc_sound', 'author'), 'author', expected_type
=str)
257 is_generic_og_trackname
= music_info
.get('is_original_sound') and music_info
.get('title') == 'original sound - %s' % music_info
.get('owner_handle')
258 if is_generic_og_trackname
:
259 music_track
, music_author
= contained_music_track
or 'original sound', contained_music_author
261 music_track
, music_author
= music_info
.get('title'), music_info
.get('author')
265 'title': aweme_detail
['desc'],
266 'description': aweme_detail
['desc'],
267 'view_count': int_or_none(stats_info
.get('play_count')),
268 'like_count': int_or_none(stats_info
.get('digg_count')),
269 'repost_count': int_or_none(stats_info
.get('share_count')),
270 'comment_count': int_or_none(stats_info
.get('comment_count')),
271 'uploader': str_or_none(author_info
.get('unique_id')),
272 'creator': str_or_none(author_info
.get('nickname')),
273 'uploader_id': str_or_none(author_info
.get('uid')),
274 'uploader_url': user_url
,
275 'track': music_track
,
276 'album': str_or_none(music_info
.get('album')) or None,
277 'artist': music_author
,
278 'timestamp': int_or_none(aweme_detail
.get('create_time')),
280 'subtitles': self
.extract_subtitles(aweme_detail
, aweme_id
),
281 'thumbnails': thumbnails
,
282 'duration': int_or_none(traverse_obj(video_info
, 'duration', ('download_addr', 'duration')), scale
=1000),
283 'availability': self
._availability
(
284 is_private
='Private' in labels
,
285 needs_subscription
='Friends only' in labels
,
286 is_unlisted
='Followers only' in labels
)
289 def _parse_aweme_video_web(self
, aweme_detail
, webpage_url
):
290 video_info
= aweme_detail
['video']
291 author_info
= traverse_obj(aweme_detail
, 'authorInfo', 'author', expected_type
=dict, default
={})
292 music_info
= aweme_detail
.get('music') or {}
293 stats_info
= aweme_detail
.get('stats') or {}
294 user_url
= self
._UPLOADER
_URL
_FORMAT
% (traverse_obj(author_info
,
295 'secUid', 'id', 'uid', 'uniqueId',
296 expected_type
=str_or_none
, get_all
=False)
297 or aweme_detail
.get('authorSecId'))
300 play_url
= video_info
.get('playAddr')
301 width
= video_info
.get('width')
302 height
= video_info
.get('height')
303 if isinstance(play_url
, str):
305 'url': self
._proto
_relative
_url
(play_url
),
310 elif isinstance(play_url
, list):
312 'url': self
._proto
_relative
_url
(url
),
316 } for url
in traverse_obj(play_url
, (..., 'src'), expected_type
=url_or_none
, default
=[]) if url
]
318 download_url
= url_or_none(video_info
.get('downloadAddr')) or traverse_obj(video_info
, ('download', 'url'), expected_type
=url_or_none
)
321 'format_id': 'download',
322 'url': self
._proto
_relative
_url
(download_url
),
327 self
._remove
_duplicate
_formats
(formats
)
328 self
._sort
_formats
(formats
)
331 for thumbnail_name
in ('thumbnail', 'cover', 'dynamicCover', 'originCover'):
332 if aweme_detail
.get(thumbnail_name
):
334 'url': self
._proto
_relative
_url
(aweme_detail
[thumbnail_name
]),
340 'id': traverse_obj(aweme_detail
, 'id', 'awemeId', expected_type
=str_or_none
),
341 'title': aweme_detail
.get('desc'),
342 'duration': try_get(aweme_detail
, lambda x
: x
['video']['duration'], int),
343 'view_count': int_or_none(stats_info
.get('playCount')),
344 'like_count': int_or_none(stats_info
.get('diggCount')),
345 'repost_count': int_or_none(stats_info
.get('shareCount')),
346 'comment_count': int_or_none(stats_info
.get('commentCount')),
347 'timestamp': int_or_none(aweme_detail
.get('createTime')),
348 'creator': str_or_none(author_info
.get('nickname')),
349 'uploader': str_or_none(author_info
.get('uniqueId') or aweme_detail
.get('author')),
350 'uploader_id': str_or_none(author_info
.get('id') or aweme_detail
.get('authorId')),
351 'uploader_url': user_url
,
352 'track': str_or_none(music_info
.get('title')),
353 'album': str_or_none(music_info
.get('album')) or None,
354 'artist': str_or_none(music_info
.get('authorName')),
356 'thumbnails': thumbnails
,
357 'description': str_or_none(aweme_detail
.get('desc')),
359 'Referer': webpage_url
364 class TikTokIE(TikTokBaseIE
):
365 _VALID_URL
= r
'https?://www\.tiktok\.com/@[\w\.-]+/video/(?P<id>\d+)'
368 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610',
369 'md5': '736bb7a466c6f0a6afeb597da1e6f5b7',
371 'id': '6748451240264420610',
373 'title': '#jassmanak #lehanga #leenabhushan',
374 'description': '#jassmanak #lehanga #leenabhushan',
378 'uploader': 'leenabhushan',
379 'uploader_id': '6691488002098119685',
380 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA_Eb4t1vodM1IuTy_cvp9CY22RAb59xqrO0Xtz9CYQJvgXaDvZxYnZYRzDWhhgJmy',
381 'creator': 'facestoriesbyleenabh',
382 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
383 'upload_date': '20191016',
384 'timestamp': 1571246252,
388 'comment_count': int,
391 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en',
392 'md5': '6f3cf8cdd9b28cb8363fe0a9a160695b',
394 'id': '6742501081818877190',
396 'title': 'md5:5e2a23877420bb85ce6521dbee39ba94',
397 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94',
401 'uploader': 'patrox',
402 'uploader_id': '18702747',
403 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws',
405 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
406 'upload_date': '20190930',
407 'timestamp': 1569860870,
411 'comment_count': int,
414 # Banned audio, only available on the app
415 'url': 'https://www.tiktok.com/@barudakhb_/video/6984138651336838402',
417 'id': '6984138651336838402',
419 'title': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥',
420 'description': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥',
421 'uploader': 'barudakhb_',
422 'creator': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6',
423 'uploader_id': '6974687867511718913',
424 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAbhBwQC-R1iKoix6jDFsF-vBdfx2ABoDjaZrM9fX6arU3w71q3cOWgWuTXn1soZ7d',
425 'track': 'Boka Dance',
426 'artist': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6',
427 'timestamp': 1626121503,
429 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
430 'upload_date': '20210712',
434 'comment_count': int,
437 # Sponsored video, only available with feed workaround
438 'url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_/video/7042692929109986561',
440 'id': '7042692929109986561',
442 'title': 'Slap and Run!',
443 'description': 'Slap and Run!',
444 'uploader': 'user440922249',
445 'creator': 'Slap And Run',
446 'uploader_id': '7036055384943690754',
447 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_',
448 'track': 'Promoted Music',
449 'timestamp': 1639754738,
451 'thumbnail': r
're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
452 'upload_date': '20211217',
456 'comment_count': int,
458 'expected_warnings': ['Video not available']
460 # Auto-captions available
461 'url': 'https://www.tiktok.com/@hankgreen1/video/7047596209028074758',
462 'only_matching': True
465 def _extract_aweme_app(self
, aweme_id
):
467 aweme_detail
= self
._call
_api
('aweme/detail', {'aweme_id': aweme_id}
, aweme_id
,
468 note
='Downloading video details', errnote
='Unable to download video details').get('aweme_detail')
470 raise ExtractorError('Video not available', video_id
=aweme_id
)
471 except ExtractorError
as e
:
472 self
.report_warning(f
'{e}; Retrying with feed workaround')
473 feed_list
= self
._call
_api
('feed', {'aweme_id': aweme_id}
, aweme_id
,
474 note
='Downloading video feed', errnote
='Unable to download video feed').get('aweme_list') or []
475 aweme_detail
= next((aweme
for aweme
in feed_list
if str(aweme
.get('aweme_id')) == aweme_id
), None)
477 raise ExtractorError('Unable to find video in feed', video_id
=aweme_id
)
478 return self
._parse
_aweme
_video
_app
(aweme_detail
)
480 def _real_extract(self
, url
):
481 video_id
= self
._match
_id
(url
)
484 return self
._extract
_aweme
_app
(video_id
)
485 except ExtractorError
as e
:
486 self
.report_warning(f
'{e}; Retrying with webpage')
488 # If we only call once, we get a 403 when downlaoding the video.
489 self
._download
_webpage
(url
, video_id
)
490 webpage
= self
._download
_webpage
(url
, video_id
, note
='Downloading video webpage')
491 next_data
= self
._search
_nextjs
_data
(webpage
, video_id
, default
='{}')
494 status
= traverse_obj(next_data
, ('props', 'pageProps', 'statusCode'), expected_type
=int) or 0
495 video_data
= traverse_obj(next_data
, ('props', 'pageProps', 'itemInfo', 'itemStruct'), expected_type
=dict)
497 sigi_json
= self
._search
_regex
(
498 r
'>\s*window\[[\'"]SIGI_STATE[\'"]\
]\s
*=\s
*(?P
<sigi_state
>{.+}
);',
499 webpage, 'sigi data
', group='sigi_state
')
500 sigi_data = self._parse_json(sigi_json, video_id)
501 status = traverse_obj(sigi_data, ('VideoPage
', 'statusCode
'), expected_type=int) or 0
502 video_data = traverse_obj(sigi_data, ('ItemModule
', video_id), expected_type=dict)
505 return self._parse_aweme_video_web(video_data, url)
506 elif status == 10216:
507 raise ExtractorError('This video
is private
', expected=True)
508 raise ExtractorError('Video
not available
', video_id=video_id)
511 class TikTokUserIE(TikTokBaseIE):
512 IE_NAME = 'tiktok
:user
'
513 _VALID_URL = r'https?
://(?
:www\
.)?tiktok\
.com
/@(?P
<id>[\w\
.-]+)/?
(?
:$|
[#?])'
515 'url': 'https://tiktok.com/@corgibobaa?lang=en',
516 'playlist_mincount': 45,
518 'id': '6935371178089399301',
519 'title': 'corgibobaa',
520 'thumbnail': r
're:https://.+_1080x1080\.webp'
522 'expected_warnings': ['Retrying']
524 'url': 'https://www.tiktok.com/@meme',
525 'playlist_mincount': 593,
527 'id': '79005827461758976',
529 'thumbnail': r
're:https://.+_1080x1080\.webp'
531 'expected_warnings': ['Retrying']
534 r
''' # TODO: Fix by adding _signature to api_url
535 def _entries(self, webpage, user_id, username):
536 secuid = self._search_regex(r'\"secUid\":\"(?P<secUid>[^\"]+)', webpage, username)
537 verifyfp_cookie = self._get_cookies('https://www.tiktok.com').get('s_v_web_id')
538 if not verifyfp_cookie:
539 raise ExtractorError('Improper cookies (missing s_v_web_id).', expected=True)
540 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='
542 for page in itertools.count():
543 data_json = self._download_json(api_url + cursor, username, note='Downloading Page %d' % page)
544 for video in data_json.get('itemList', []):
545 video_id = video['id']
546 video_url = f'https://www.tiktok.com/@{user_id}/video/{video_id}'
547 yield self._url_result(video_url, 'TikTok', video_id, str_or_none(video.get('desc')))
548 if not data_json.get('hasMore'):
550 cursor = data_json['cursor']
553 def _video_entries_api(self
, webpage
, user_id
, username
):
559 'retry_type': 'no_retry',
560 'device_id': ''.join(random
.choice(string
.digits
) for _
in range(19)), # Some endpoints don't like randomized device_id, so it isn't directly set in _call_api.
563 max_retries
= self
.get_param('extractor_retries', 3)
564 for page
in itertools
.count(1):
565 for retries
in itertools
.count():
567 post_list
= self
._call
_api
('aweme/post', query
, username
,
568 note
='Downloading user video list page %d%s' % (page
, f
' (attempt {retries})' if retries
!= 0 else ''),
569 errnote
='Unable to download user video list')
570 except ExtractorError
as e
:
571 if isinstance(e
.cause
, json
.JSONDecodeError
) and e
.cause
.pos
== 0 and retries
!= max_retries
:
572 self
.report_warning('%s. Retrying...' % str(e
.cause
or e
.msg
))
576 yield from post_list
.get('aweme_list', [])
577 if not post_list
.get('has_more'):
579 query
['max_cursor'] = post_list
['max_cursor']
581 def _entries_api(self
, user_id
, videos
):
584 **self
._parse
_aweme
_video
_app
(video
),
585 'extractor_key': TikTokIE
.ie_key(),
586 'extractor': 'TikTok',
587 'webpage_url': f
'https://tiktok.com/@{user_id}/video/{video["aweme_id"]}',
590 def _real_extract(self
, url
):
591 user_name
= self
._match
_id
(url
)
592 webpage
= self
._download
_webpage
(url
, user_name
, headers
={
593 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
595 user_id
= self
._html
_search
_regex
(r
'snssdk\d*://user/profile/(\d+)', webpage
, 'user ID')
597 videos
= LazyList(self
._video
_entries
_api
(webpage
, user_id
, user_name
))
598 thumbnail
= traverse_obj(videos
, (0, 'author', 'avatar_larger', 'url_list', 0))
600 return self
.playlist_result(self
._entries
_api
(user_id
, videos
), user_id
, user_name
, thumbnail
=thumbnail
)
603 class TikTokBaseListIE(TikTokBaseIE
):
604 def _entries(self
, list_id
, display_id
):
606 self
._QUERY
_NAME
: list_id
,
610 'device_id': ''.join(random
.choice(string
.digits
) for i
in range(19))
613 max_retries
= self
.get_param('extractor_retries', 3)
614 for page
in itertools
.count(1):
615 for retries
in itertools
.count():
617 post_list
= self
._call
_api
(self
._API
_ENDPOINT
, query
, display_id
,
618 note
='Downloading video list page %d%s' % (page
, f
' (attempt {retries})' if retries
!= 0 else ''),
619 errnote
='Unable to download video list')
620 except ExtractorError
as e
:
621 if isinstance(e
.cause
, json
.JSONDecodeError
) and e
.cause
.pos
== 0 and retries
!= max_retries
:
622 self
.report_warning('%s. Retrying...' % str(e
.cause
or e
.msg
))
626 for video
in post_list
.get('aweme_list', []):
628 **self
._parse
_aweme
_video
_app
(video
),
629 'extractor_key': TikTokIE
.ie_key(),
630 'extractor': 'TikTok',
631 'webpage_url': f
'https://tiktok.com/@_/video/{video["aweme_id"]}',
633 if not post_list
.get('has_more'):
635 query
['cursor'] = post_list
['cursor']
637 def _real_extract(self
, url
):
638 list_id
= self
._match
_id
(url
)
639 return self
.playlist_result(self
._entries
(list_id
, list_id
), list_id
)
642 class TikTokSoundIE(TikTokBaseListIE
):
643 IE_NAME
= 'tiktok:sound'
644 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/music/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
645 _QUERY_NAME
= 'music_id'
646 _API_ENDPOINT
= 'music/aweme'
648 'url': 'https://www.tiktok.com/music/Build-a-Btch-6956990112127585029?lang=en',
649 'playlist_mincount': 100,
651 'id': '6956990112127585029'
653 'expected_warnings': ['Retrying']
655 # Actual entries are less than listed video count
656 'url': 'https://www.tiktok.com/music/jiefei-soap-remix-7036843036118469381',
657 'playlist_mincount': 2182,
659 'id': '7036843036118469381'
661 'expected_warnings': ['Retrying']
665 class TikTokEffectIE(TikTokBaseListIE
):
666 IE_NAME
= 'tiktok:effect'
667 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/sticker/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
668 _QUERY_NAME
= 'sticker_id'
669 _API_ENDPOINT
= 'sticker/aweme'
671 'url': 'https://www.tiktok.com/sticker/MATERIAL-GWOOORL-1258156',
672 'playlist_mincount': 100,
676 'expected_warnings': ['Retrying']
678 # Different entries between mobile and web, depending on region
679 'url': 'https://www.tiktok.com/sticker/Elf-Friend-479565',
680 'only_matching': True
684 class TikTokTagIE(TikTokBaseListIE
):
685 IE_NAME
= 'tiktok:tag'
686 _VALID_URL
= r
'https?://(?:www\.)?tiktok\.com/tag/(?P<id>[^/?#&]+)'
687 _QUERY_NAME
= 'ch_id'
688 _API_ENDPOINT
= 'challenge/aweme'
690 'url': 'https://tiktok.com/tag/hello2018',
691 'playlist_mincount': 39,
694 'title': 'hello2018',
696 'expected_warnings': ['Retrying']
698 'url': 'https://tiktok.com/tag/fypシ?is_copy_url=0&is_from_webapp=v1',
699 'only_matching': True
702 def _real_extract(self
, url
):
703 display_id
= self
._match
_id
(url
)
704 webpage
= self
._download
_webpage
(url
, display_id
, headers
={
705 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
707 tag_id
= self
._html
_search
_regex
(r
'snssdk\d*://challenge/detail/(\d+)', webpage
, 'tag ID')
708 return self
.playlist_result(self
._entries
(tag_id
, display_id
), tag_id
, display_id
)
711 class DouyinIE(TikTokIE
):
712 _VALID_URL
= r
'https?://(?:www\.)?douyin\.com/video/(?P<id>[0-9]+)'
714 'url': 'https://www.douyin.com/video/6961737553342991651',
715 'md5': '10523312c8b8100f353620ac9dc8f067',
717 'id': '6961737553342991651',
719 'title': '#杨超越 小小水手带你去远航❤️',
721 'upload_date': '20210513',
722 'timestamp': 1620905839,
723 'uploader_id': '110403406559',
727 'comment_count': int,
730 'url': 'https://www.douyin.com/video/6982497745948921092',
731 'md5': 'd78408c984b9b5102904cf6b6bc2d712',
733 'id': '6982497745948921092',
735 'title': '这个夏日和小羊@杨超越 一起遇见白色幻想',
736 'uploader': '杨超越工作室',
737 'upload_date': '20210708',
738 'timestamp': 1625739481,
739 'uploader_id': '408654318141572',
743 'comment_count': int,
746 'url': 'https://www.douyin.com/video/6953975910773099811',
747 'md5': '72e882e24f75064c218b76c8b713c185',
749 'id': '6953975910773099811',
751 'title': '#一起看海 出现在你的夏日里',
753 'upload_date': '20210422',
754 'timestamp': 1619098692,
755 'uploader_id': '110403406559',
759 'comment_count': int,
762 'url': 'https://www.douyin.com/video/6950251282489675042',
763 'md5': 'b4db86aec367ef810ddd38b1737d2fed',
765 'id': '6950251282489675042',
767 'title': '哈哈哈,成功了哈哈哈哈哈哈',
769 'upload_date': '20210412',
770 'timestamp': 1618231483,
771 'uploader_id': '110403406559',
775 'comment_count': int,
778 'url': 'https://www.douyin.com/video/6963263655114722595',
779 'md5': '1abe1c477d05ee62efb40bf2329957cf',
781 'id': '6963263655114722595',
783 'title': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
785 'upload_date': '20210517',
786 'timestamp': 1621261163,
787 'uploader_id': '110403406559',
791 'comment_count': int,
794 _APP_VERSIONS
= [('9.6.0', '960')]
797 _API_HOSTNAME
= 'aweme.snssdk.com'
798 _UPLOADER_URL_FORMAT
= 'https://www.douyin.com/user/%s'
799 _WEBPAGE_HOST
= 'https://www.douyin.com/'
801 def _real_extract(self
, url
):
802 video_id
= self
._match
_id
(url
)
805 return self
._extract
_aweme
_app
(video_id
)
806 except ExtractorError
as e
:
807 self
.report_warning(f
'{e}; Retrying with webpage')
809 webpage
= self
._download
_webpage
(url
, video_id
)
810 render_data_json
= self
._search
_regex
(
811 r
'<script [^>]*\bid=[\'"]RENDER_DATA[\'"][^
>]*>(%7B
.+%7D
)</script
>',
812 webpage, 'render data
', default=None)
813 if not render_data_json:
814 # TODO: Run verification challenge code to generate signature cookies
815 raise ExtractorError('Fresh
cookies (not necessarily logged
in) are needed
')
817 render_data = self._parse_json(
818 render_data_json, video_id, transform_source=compat_urllib_parse_unquote)
819 return self._parse_aweme_video_web(
820 traverse_obj(render_data, (..., 'aweme
', 'detail
'), get_all=False), url)
823 class TikTokVMIE(InfoExtractor):
824 _VALID_URL = r'https?
://(?
:vm|vt
)\
.tiktok\
.com
/(?P
<id>\w
+)'
825 IE_NAME = 'vm
.tiktok
'
828 'url
': 'https
://vm
.tiktok
.com
/ZSe4FqkKd
',
830 'id': '7023491746608712966',
832 'title
': 'md5
:5607564db90271abbbf8294cca77eddd
',
833 'description
': 'md5
:5607564db90271abbbf8294cca77eddd
',
835 'upload_date
': '20211026',
836 'uploader_id
': '7007385080558846981',
839 'track
': 'original sound
',
840 'uploader
': 'susmandem
',
841 'timestamp
': 1635284105,
842 'thumbnail
': r're
:https
://.+\
.webp
.*',
845 'comment_count
': int,
847 'uploader_url
': 'https
://www
.tiktok
.com
/@MS4wLjABAAAAXcNoOEOxVyBzuII_E--T0MeCrLP0ay1Sm6x_n3dluiWEoWZD0VlQOytwad4W0i0n
',
850 'url
': 'https
://vt
.tiktok
.com
/ZSe4FqkKd
',
851 'only_matching
': True,
854 def _real_extract(self, url):
855 return self.url_result(self._request_webpage(
856 HEADRequest(url), self._match_id(url), headers={'User-Agent': 'facebookexternalhit/1.1'}).geturl(), TikTokIE)