]>
Commit | Line | Data |
---|---|---|
1 | # coding: utf-8 | |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import itertools | |
5 | import random | |
6 | import string | |
7 | import time | |
8 | import json | |
9 | ||
10 | from .common import InfoExtractor | |
11 | from ..compat import compat_urllib_parse_unquote | |
12 | from ..utils import ( | |
13 | ExtractorError, | |
14 | int_or_none, | |
15 | join_nonempty, | |
16 | str_or_none, | |
17 | traverse_obj, | |
18 | try_get, | |
19 | url_or_none, | |
20 | qualities, | |
21 | ) | |
22 | ||
23 | ||
24 | class TikTokBaseIE(InfoExtractor): | |
25 | _APP_VERSION = '20.9.3' | |
26 | _MANIFEST_APP_VERSION = '291' | |
27 | _APP_NAME = 'trill' | |
28 | _AID = 1180 | |
29 | _API_HOSTNAME = 'api-h2.tiktokv.com' | |
30 | _UPLOADER_URL_FORMAT = 'https://www.tiktok.com/@%s' | |
31 | _WEBPAGE_HOST = 'https://www.tiktok.com/' | |
32 | QUALITIES = ('360p', '540p', '720p') | |
33 | ||
34 | def _call_api(self, ep, query, video_id, fatal=True, | |
35 | note='Downloading API JSON', errnote='Unable to download API page'): | |
36 | real_query = { | |
37 | **query, | |
38 | 'version_name': self._APP_VERSION, | |
39 | 'version_code': self._MANIFEST_APP_VERSION, | |
40 | 'build_number': self._APP_VERSION, | |
41 | 'manifest_version_code': self._MANIFEST_APP_VERSION, | |
42 | 'update_version_code': self._MANIFEST_APP_VERSION, | |
43 | 'openudid': ''.join(random.choice('0123456789abcdef') for _ in range(16)), | |
44 | 'uuid': ''.join([random.choice(string.digits) for _ in range(16)]), | |
45 | '_rticket': int(time.time() * 1000), | |
46 | 'ts': int(time.time()), | |
47 | 'device_brand': 'Google', | |
48 | 'device_type': 'Pixel 4', | |
49 | 'device_platform': 'android', | |
50 | 'resolution': '1080*1920', | |
51 | 'dpi': 420, | |
52 | 'os_version': '10', | |
53 | 'os_api': '29', | |
54 | 'carrier_region': 'US', | |
55 | 'sys_region': 'US', | |
56 | 'region': 'US', | |
57 | 'app_name': self._APP_NAME, | |
58 | 'app_language': 'en', | |
59 | 'language': 'en', | |
60 | 'timezone_name': 'America/New_York', | |
61 | 'timezone_offset': '-14400', | |
62 | 'channel': 'googleplay', | |
63 | 'ac': 'wifi', | |
64 | 'mcc_mnc': '310260', | |
65 | 'is_my_cn': 0, | |
66 | 'aid': self._AID, | |
67 | 'ssmix': 'a', | |
68 | 'as': 'a1qwert123', | |
69 | 'cp': 'cbfhckdckkde1', | |
70 | } | |
71 | self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choice('0123456789abcdef') for _ in range(160))) | |
72 | webpage_cookies = self._get_cookies(self._WEBPAGE_HOST) | |
73 | if webpage_cookies.get('sid_tt'): | |
74 | self._set_cookie(self._API_HOSTNAME, 'sid_tt', webpage_cookies['sid_tt'].value) | |
75 | return self._download_json( | |
76 | 'https://%s/aweme/v1/%s/' % (self._API_HOSTNAME, ep), video_id=video_id, | |
77 | fatal=fatal, note=note, errnote=errnote, headers={ | |
78 | 'User-Agent': f'com.ss.android.ugc.trill/{self._MANIFEST_APP_VERSION} (Linux; U; Android 10; en_US; Pixel 4; Build/QQ3A.200805.001; Cronet/58.0.2991.0)', | |
79 | 'Accept': 'application/json', | |
80 | }, query=real_query) | |
81 | ||
82 | def _parse_aweme_video_app(self, aweme_detail): | |
83 | aweme_id = aweme_detail['aweme_id'] | |
84 | video_info = aweme_detail['video'] | |
85 | ||
86 | def parse_url_key(url_key): | |
87 | format_id, codec, res, bitrate = self._search_regex( | |
88 | r'v[^_]+_(?P<id>(?P<codec>[^_]+)_(?P<res>\d+p)_(?P<bitrate>\d+))', url_key, | |
89 | 'url key', default=(None, None, None, None), group=('id', 'codec', 'res', 'bitrate')) | |
90 | if not format_id: | |
91 | return {}, None | |
92 | return { | |
93 | 'format_id': format_id, | |
94 | 'vcodec': 'h265' if codec == 'bytevc1' else codec, | |
95 | 'tbr': int_or_none(bitrate, scale=1000) or None, | |
96 | 'quality': qualities(self.QUALITIES)(res), | |
97 | }, res | |
98 | ||
99 | known_resolutions = {} | |
100 | ||
101 | def extract_addr(addr, add_meta={}): | |
102 | parsed_meta, res = parse_url_key(addr.get('url_key', '')) | |
103 | if res: | |
104 | known_resolutions.setdefault(res, {}).setdefault('height', add_meta.get('height')) | |
105 | known_resolutions[res].setdefault('width', add_meta.get('width')) | |
106 | parsed_meta.update(known_resolutions.get(res, {})) | |
107 | add_meta.setdefault('height', int_or_none(res[:-1])) | |
108 | return [{ | |
109 | 'url': url, | |
110 | 'filesize': int_or_none(addr.get('data_size')), | |
111 | 'ext': 'mp4', | |
112 | 'acodec': 'aac', | |
113 | 'source_preference': -2 if 'aweme/v1' in url else -1, # Downloads from API might get blocked | |
114 | **add_meta, **parsed_meta, | |
115 | 'format_note': join_nonempty( | |
116 | add_meta.get('format_note'), '(API)' if 'aweme/v1' in url else None, delim=' ') | |
117 | } for url in addr.get('url_list') or []] | |
118 | ||
119 | # Hack: Add direct video links first to prioritize them when removing duplicate formats | |
120 | formats = [] | |
121 | if video_info.get('play_addr'): | |
122 | formats.extend(extract_addr(video_info['play_addr'], { | |
123 | 'format_id': 'play_addr', | |
124 | 'format_note': 'Direct video', | |
125 | 'vcodec': 'h265' if traverse_obj( | |
126 | video_info, 'is_bytevc1', 'is_h265') else 'h264', # Always h264? | |
127 | 'width': video_info.get('width'), | |
128 | 'height': video_info.get('height'), | |
129 | })) | |
130 | if video_info.get('download_addr'): | |
131 | formats.extend(extract_addr(video_info['download_addr'], { | |
132 | 'format_id': 'download_addr', | |
133 | 'format_note': 'Download video%s' % (', watermarked' if video_info.get('has_watermark') else ''), | |
134 | 'vcodec': 'h264', | |
135 | 'width': video_info.get('width'), | |
136 | 'height': video_info.get('height'), | |
137 | 'preference': -2 if video_info.get('has_watermark') else -1, | |
138 | })) | |
139 | if video_info.get('play_addr_h264'): | |
140 | formats.extend(extract_addr(video_info['play_addr_h264'], { | |
141 | 'format_id': 'play_addr_h264', | |
142 | 'format_note': 'Direct video', | |
143 | 'vcodec': 'h264', | |
144 | })) | |
145 | if video_info.get('play_addr_bytevc1'): | |
146 | formats.extend(extract_addr(video_info['play_addr_bytevc1'], { | |
147 | 'format_id': 'play_addr_bytevc1', | |
148 | 'format_note': 'Direct video', | |
149 | 'vcodec': 'h265', | |
150 | })) | |
151 | ||
152 | for bitrate in video_info.get('bit_rate', []): | |
153 | if bitrate.get('play_addr'): | |
154 | formats.extend(extract_addr(bitrate['play_addr'], { | |
155 | 'format_id': bitrate.get('gear_name'), | |
156 | 'format_note': 'Playback video', | |
157 | 'tbr': try_get(bitrate, lambda x: x['bit_rate'] / 1000), | |
158 | 'vcodec': 'h265' if traverse_obj( | |
159 | bitrate, 'is_bytevc1', 'is_h265') else 'h264', | |
160 | 'fps': bitrate.get('FPS'), | |
161 | })) | |
162 | ||
163 | self._remove_duplicate_formats(formats) | |
164 | self._sort_formats(formats, ('quality', 'codec', 'size', 'br')) | |
165 | ||
166 | thumbnails = [] | |
167 | for cover_id in ('cover', 'ai_dynamic_cover', 'animated_cover', 'ai_dynamic_cover_bak', | |
168 | 'origin_cover', 'dynamic_cover'): | |
169 | cover = video_info.get(cover_id) | |
170 | if cover: | |
171 | for cover_url in cover['url_list']: | |
172 | thumbnails.append({ | |
173 | 'id': cover_id, | |
174 | 'url': cover_url, | |
175 | }) | |
176 | ||
177 | stats_info = aweme_detail.get('statistics', {}) | |
178 | author_info = aweme_detail.get('author', {}) | |
179 | music_info = aweme_detail.get('music', {}) | |
180 | user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info, | |
181 | 'sec_uid', 'id', 'uid', 'unique_id', | |
182 | expected_type=str_or_none, get_all=False)) | |
183 | labels = traverse_obj(aweme_detail, ('hybrid_label', ..., 'text'), expected_type=str) | |
184 | ||
185 | contained_music_track = traverse_obj( | |
186 | music_info, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type=str) | |
187 | contained_music_author = traverse_obj( | |
188 | music_info, ('matched_song', 'author'), ('matched_pgc_sound', 'author'), 'author', expected_type=str) | |
189 | ||
190 | is_generic_og_trackname = music_info.get('is_original_sound') and music_info.get('title') == 'original sound - %s' % music_info.get('owner_handle') | |
191 | if is_generic_og_trackname: | |
192 | music_track, music_author = contained_music_track or 'original sound', contained_music_author | |
193 | else: | |
194 | music_track, music_author = music_info.get('title'), music_info.get('author') | |
195 | ||
196 | return { | |
197 | 'id': aweme_id, | |
198 | 'title': aweme_detail['desc'], | |
199 | 'description': aweme_detail['desc'], | |
200 | 'view_count': int_or_none(stats_info.get('play_count')), | |
201 | 'like_count': int_or_none(stats_info.get('digg_count')), | |
202 | 'repost_count': int_or_none(stats_info.get('share_count')), | |
203 | 'comment_count': int_or_none(stats_info.get('comment_count')), | |
204 | 'uploader': str_or_none(author_info.get('unique_id')), | |
205 | 'creator': str_or_none(author_info.get('nickname')), | |
206 | 'uploader_id': str_or_none(author_info.get('uid')), | |
207 | 'uploader_url': user_url, | |
208 | 'track': music_track, | |
209 | 'album': str_or_none(music_info.get('album')) or None, | |
210 | 'artist': music_author, | |
211 | 'timestamp': int_or_none(aweme_detail.get('create_time')), | |
212 | 'formats': formats, | |
213 | 'thumbnails': thumbnails, | |
214 | 'duration': int_or_none(traverse_obj(video_info, 'duration', ('download_addr', 'duration')), scale=1000), | |
215 | 'availability': self._availability( | |
216 | is_private='Private' in labels, | |
217 | needs_subscription='Friends only' in labels, | |
218 | is_unlisted='Followers only' in labels) | |
219 | } | |
220 | ||
221 | def _parse_aweme_video_web(self, aweme_detail, webpage_url): | |
222 | video_info = aweme_detail['video'] | |
223 | author_info = traverse_obj(aweme_detail, 'author', 'authorInfo', default={}) | |
224 | music_info = aweme_detail.get('music') or {} | |
225 | stats_info = aweme_detail.get('stats') or {} | |
226 | user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info, | |
227 | 'secUid', 'id', 'uid', 'uniqueId', | |
228 | expected_type=str_or_none, get_all=False)) | |
229 | ||
230 | formats = [] | |
231 | play_url = video_info.get('playAddr') | |
232 | width = video_info.get('width') | |
233 | height = video_info.get('height') | |
234 | if isinstance(play_url, str): | |
235 | formats = [{ | |
236 | 'url': self._proto_relative_url(play_url), | |
237 | 'ext': 'mp4', | |
238 | 'width': width, | |
239 | 'height': height, | |
240 | }] | |
241 | elif isinstance(play_url, list): | |
242 | formats = [{ | |
243 | 'url': self._proto_relative_url(url), | |
244 | 'ext': 'mp4', | |
245 | 'width': width, | |
246 | 'height': height, | |
247 | } for url in traverse_obj(play_url, (..., 'src'), expected_type=url_or_none, default=[]) if url] | |
248 | ||
249 | download_url = url_or_none(video_info.get('downloadAddr')) or traverse_obj(video_info, ('download', 'url'), expected_type=url_or_none) | |
250 | if download_url: | |
251 | formats.append({ | |
252 | 'format_id': 'download', | |
253 | 'url': self._proto_relative_url(download_url), | |
254 | 'ext': 'mp4', | |
255 | 'width': width, | |
256 | 'height': height, | |
257 | }) | |
258 | self._remove_duplicate_formats(formats) | |
259 | self._sort_formats(formats) | |
260 | ||
261 | thumbnails = [] | |
262 | for thumbnail_name in ('thumbnail', 'cover', 'dynamicCover', 'originCover'): | |
263 | if aweme_detail.get(thumbnail_name): | |
264 | thumbnails = [{ | |
265 | 'url': self._proto_relative_url(aweme_detail[thumbnail_name]), | |
266 | 'width': width, | |
267 | 'height': height | |
268 | }] | |
269 | ||
270 | return { | |
271 | 'id': traverse_obj(aweme_detail, 'id', 'awemeId', expected_type=str_or_none), | |
272 | 'title': aweme_detail.get('desc'), | |
273 | 'duration': try_get(aweme_detail, lambda x: x['video']['duration'], int), | |
274 | 'view_count': int_or_none(stats_info.get('playCount')), | |
275 | 'like_count': int_or_none(stats_info.get('diggCount')), | |
276 | 'repost_count': int_or_none(stats_info.get('shareCount')), | |
277 | 'comment_count': int_or_none(stats_info.get('commentCount')), | |
278 | 'timestamp': int_or_none(aweme_detail.get('createTime')), | |
279 | 'creator': str_or_none(author_info.get('nickname')), | |
280 | 'uploader': str_or_none(author_info.get('uniqueId')), | |
281 | 'uploader_id': str_or_none(author_info.get('id')), | |
282 | 'uploader_url': user_url, | |
283 | 'track': str_or_none(music_info.get('title')), | |
284 | 'album': str_or_none(music_info.get('album')) or None, | |
285 | 'artist': str_or_none(music_info.get('authorName')), | |
286 | 'formats': formats, | |
287 | 'thumbnails': thumbnails, | |
288 | 'description': str_or_none(aweme_detail.get('desc')), | |
289 | 'http_headers': { | |
290 | 'Referer': webpage_url | |
291 | } | |
292 | } | |
293 | ||
294 | ||
295 | class TikTokIE(TikTokBaseIE): | |
296 | _VALID_URL = r'https?://www\.tiktok\.com/@[\w\.-]+/video/(?P<id>\d+)' | |
297 | ||
298 | _TESTS = [{ | |
299 | 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610', | |
300 | 'md5': '736bb7a466c6f0a6afeb597da1e6f5b7', | |
301 | 'info_dict': { | |
302 | 'id': '6748451240264420610', | |
303 | 'ext': 'mp4', | |
304 | 'title': '#jassmanak #lehanga #leenabhushan', | |
305 | 'description': '#jassmanak #lehanga #leenabhushan', | |
306 | 'duration': 13, | |
307 | 'height': 1024, | |
308 | 'width': 576, | |
309 | 'uploader': 'leenabhushan', | |
310 | 'uploader_id': '6691488002098119685', | |
311 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA_Eb4t1vodM1IuTy_cvp9CY22RAb59xqrO0Xtz9CYQJvgXaDvZxYnZYRzDWhhgJmy', | |
312 | 'creator': 'facestoriesbyleenabh', | |
313 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
314 | 'upload_date': '20191016', | |
315 | 'timestamp': 1571246252, | |
316 | 'view_count': int, | |
317 | 'like_count': int, | |
318 | 'repost_count': int, | |
319 | 'comment_count': int, | |
320 | } | |
321 | }, { | |
322 | 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en', | |
323 | 'md5': '6f3cf8cdd9b28cb8363fe0a9a160695b', | |
324 | 'info_dict': { | |
325 | 'id': '6742501081818877190', | |
326 | 'ext': 'mp4', | |
327 | 'title': 'md5:5e2a23877420bb85ce6521dbee39ba94', | |
328 | 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94', | |
329 | 'duration': 27, | |
330 | 'height': 960, | |
331 | 'width': 540, | |
332 | 'uploader': 'patrox', | |
333 | 'uploader_id': '18702747', | |
334 | 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws', | |
335 | 'creator': 'patroX', | |
336 | 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?', | |
337 | 'upload_date': '20190930', | |
338 | 'timestamp': 1569860870, | |
339 | 'view_count': int, | |
340 | 'like_count': int, | |
341 | 'repost_count': int, | |
342 | 'comment_count': int, | |
343 | } | |
344 | }, { | |
345 | # Promoted content/ad | |
346 | 'url': 'https://www.tiktok.com/@MS4wLjABAAAAAR29F6J2Ktu0Daw03BJyXPNoRQ-W7U5a0Mn3lVCq2rQhjOd_WNLclHUoFgwX8Eno/video/6932675057474981122', | |
347 | 'only_matching': True, | |
348 | }] | |
349 | ||
350 | def _extract_aweme_app(self, aweme_id): | |
351 | aweme_detail = self._call_api('aweme/detail', {'aweme_id': aweme_id}, aweme_id, | |
352 | note='Downloading video details', errnote='Unable to download video details').get('aweme_detail') | |
353 | if not aweme_detail: | |
354 | raise ExtractorError('Video not available', video_id=aweme_id) | |
355 | return self._parse_aweme_video_app(aweme_detail) | |
356 | ||
357 | def _real_extract(self, url): | |
358 | video_id = self._match_id(url) | |
359 | ||
360 | try: | |
361 | return self._extract_aweme_app(video_id) | |
362 | except ExtractorError as e: | |
363 | self.report_warning(f'{e}; Retrying with webpage') | |
364 | ||
365 | # If we only call once, we get a 403 when downlaoding the video. | |
366 | self._download_webpage(url, video_id) | |
367 | webpage = self._download_webpage(url, video_id, note='Downloading video webpage') | |
368 | json_string = self._search_regex( | |
369 | r'id=\"__NEXT_DATA__\"\s+type=\"application\/json\"\s*[^>]+>\s*(?P<json_string_ld>[^<]+)', | |
370 | webpage, 'json_string', group='json_string_ld') | |
371 | json_data = self._parse_json(json_string, video_id) | |
372 | props_data = try_get(json_data, lambda x: x['props'], expected_type=dict) | |
373 | ||
374 | # Chech statusCode for success | |
375 | status = props_data.get('pageProps').get('statusCode') | |
376 | if status == 0: | |
377 | return self._parse_aweme_video_web(props_data['pageProps']['itemInfo']['itemStruct'], url) | |
378 | elif status == 10216: | |
379 | raise ExtractorError('This video is private', expected=True) | |
380 | ||
381 | raise ExtractorError('Video not available', video_id=video_id) | |
382 | ||
383 | ||
384 | class TikTokUserIE(TikTokBaseIE): | |
385 | IE_NAME = 'tiktok:user' | |
386 | _VALID_URL = r'https?://(?:www\.)?tiktok\.com/@(?P<id>[\w\.-]+)/?(?:$|[#?])' | |
387 | _TESTS = [{ | |
388 | 'url': 'https://tiktok.com/@corgibobaa?lang=en', | |
389 | 'playlist_mincount': 45, | |
390 | 'info_dict': { | |
391 | 'id': '6935371178089399301', | |
392 | 'title': 'corgibobaa', | |
393 | }, | |
394 | 'expected_warnings': ['Retrying'] | |
395 | }, { | |
396 | 'url': 'https://www.tiktok.com/@meme', | |
397 | 'playlist_mincount': 593, | |
398 | 'info_dict': { | |
399 | 'id': '79005827461758976', | |
400 | 'title': 'meme', | |
401 | }, | |
402 | 'expected_warnings': ['Retrying'] | |
403 | }] | |
404 | ||
405 | r''' # TODO: Fix by adding _signature to api_url | |
406 | def _entries(self, webpage, user_id, username): | |
407 | secuid = self._search_regex(r'\"secUid\":\"(?P<secUid>[^\"]+)', webpage, username) | |
408 | verifyfp_cookie = self._get_cookies('https://www.tiktok.com').get('s_v_web_id') | |
409 | if not verifyfp_cookie: | |
410 | raise ExtractorError('Improper cookies (missing s_v_web_id).', expected=True) | |
411 | 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=' | |
412 | cursor = '0' | |
413 | for page in itertools.count(): | |
414 | data_json = self._download_json(api_url + cursor, username, note='Downloading Page %d' % page) | |
415 | for video in data_json.get('itemList', []): | |
416 | video_id = video['id'] | |
417 | video_url = f'https://www.tiktok.com/@{user_id}/video/{video_id}' | |
418 | yield self._url_result(video_url, 'TikTok', video_id, str_or_none(video.get('desc'))) | |
419 | if not data_json.get('hasMore'): | |
420 | break | |
421 | cursor = data_json['cursor'] | |
422 | ''' | |
423 | ||
424 | def _entries_api(self, webpage, user_id, username): | |
425 | query = { | |
426 | 'user_id': user_id, | |
427 | 'count': 21, | |
428 | 'max_cursor': 0, | |
429 | 'min_cursor': 0, | |
430 | 'retry_type': 'no_retry', | |
431 | '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. | |
432 | } | |
433 | ||
434 | max_retries = self.get_param('extractor_retries', 3) | |
435 | for page in itertools.count(1): | |
436 | for retries in itertools.count(): | |
437 | try: | |
438 | post_list = self._call_api('aweme/post', query, username, | |
439 | note='Downloading user video list page %d%s' % (page, f' (attempt {retries})' if retries != 0 else ''), | |
440 | errnote='Unable to download user video list') | |
441 | except ExtractorError as e: | |
442 | if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0 and retries != max_retries: | |
443 | self.report_warning('%s. Retrying...' % str(e.cause or e.msg)) | |
444 | continue | |
445 | raise | |
446 | break | |
447 | for video in post_list.get('aweme_list', []): | |
448 | yield { | |
449 | **self._parse_aweme_video_app(video), | |
450 | 'ie_key': TikTokIE.ie_key(), | |
451 | 'extractor': 'TikTok', | |
452 | 'webpage_url': f'https://tiktok.com/@{user_id}/video/{video["aweme_id"]}', | |
453 | } | |
454 | if not post_list.get('has_more'): | |
455 | break | |
456 | query['max_cursor'] = post_list['max_cursor'] | |
457 | ||
458 | def _real_extract(self, url): | |
459 | user_name = self._match_id(url) | |
460 | webpage = self._download_webpage(url, user_name, headers={ | |
461 | 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' | |
462 | }) | |
463 | user_id = self._html_search_regex(r'snssdk\d*://user/profile/(\d+)', webpage, 'user ID') | |
464 | return self.playlist_result(self._entries_api(webpage, user_id, user_name), user_id, user_name) | |
465 | ||
466 | ||
467 | class DouyinIE(TikTokIE): | |
468 | _VALID_URL = r'https?://(?:www\.)?douyin\.com/video/(?P<id>[0-9]+)' | |
469 | _TESTS = [{ | |
470 | 'url': 'https://www.douyin.com/video/6961737553342991651', | |
471 | 'md5': '10523312c8b8100f353620ac9dc8f067', | |
472 | 'info_dict': { | |
473 | 'id': '6961737553342991651', | |
474 | 'ext': 'mp4', | |
475 | 'title': '#杨超越 小小水手带你去远航❤️', | |
476 | 'uploader': '杨超越', | |
477 | 'upload_date': '20210513', | |
478 | 'timestamp': 1620905839, | |
479 | 'uploader_id': '110403406559', | |
480 | 'view_count': int, | |
481 | 'like_count': int, | |
482 | 'repost_count': int, | |
483 | 'comment_count': int, | |
484 | } | |
485 | }, { | |
486 | 'url': 'https://www.douyin.com/video/6982497745948921092', | |
487 | 'md5': 'd78408c984b9b5102904cf6b6bc2d712', | |
488 | 'info_dict': { | |
489 | 'id': '6982497745948921092', | |
490 | 'ext': 'mp4', | |
491 | 'title': '这个夏日和小羊@杨超越 一起遇见白色幻想', | |
492 | 'uploader': '杨超越工作室', | |
493 | 'upload_date': '20210708', | |
494 | 'timestamp': 1625739481, | |
495 | 'uploader_id': '408654318141572', | |
496 | 'view_count': int, | |
497 | 'like_count': int, | |
498 | 'repost_count': int, | |
499 | 'comment_count': int, | |
500 | } | |
501 | }, { | |
502 | 'url': 'https://www.douyin.com/video/6953975910773099811', | |
503 | 'md5': '72e882e24f75064c218b76c8b713c185', | |
504 | 'info_dict': { | |
505 | 'id': '6953975910773099811', | |
506 | 'ext': 'mp4', | |
507 | 'title': '#一起看海 出现在你的夏日里', | |
508 | 'uploader': '杨超越', | |
509 | 'upload_date': '20210422', | |
510 | 'timestamp': 1619098692, | |
511 | 'uploader_id': '110403406559', | |
512 | 'view_count': int, | |
513 | 'like_count': int, | |
514 | 'repost_count': int, | |
515 | 'comment_count': int, | |
516 | } | |
517 | }, { | |
518 | 'url': 'https://www.douyin.com/video/6950251282489675042', | |
519 | 'md5': 'b4db86aec367ef810ddd38b1737d2fed', | |
520 | 'info_dict': { | |
521 | 'id': '6950251282489675042', | |
522 | 'ext': 'mp4', | |
523 | 'title': '哈哈哈,成功了哈哈哈哈哈哈', | |
524 | 'uploader': '杨超越', | |
525 | 'upload_date': '20210412', | |
526 | 'timestamp': 1618231483, | |
527 | 'uploader_id': '110403406559', | |
528 | 'view_count': int, | |
529 | 'like_count': int, | |
530 | 'repost_count': int, | |
531 | 'comment_count': int, | |
532 | } | |
533 | }, { | |
534 | 'url': 'https://www.douyin.com/video/6963263655114722595', | |
535 | 'md5': '1abe1c477d05ee62efb40bf2329957cf', | |
536 | 'info_dict': { | |
537 | 'id': '6963263655114722595', | |
538 | 'ext': 'mp4', | |
539 | 'title': '#哪个爱豆的105度最甜 换个角度看看我哈哈', | |
540 | 'uploader': '杨超越', | |
541 | 'upload_date': '20210517', | |
542 | 'timestamp': 1621261163, | |
543 | 'uploader_id': '110403406559', | |
544 | 'view_count': int, | |
545 | 'like_count': int, | |
546 | 'repost_count': int, | |
547 | 'comment_count': int, | |
548 | } | |
549 | }] | |
550 | _APP_VERSION = '9.6.0' | |
551 | _MANIFEST_APP_VERSION = '960' | |
552 | _APP_NAME = 'aweme' | |
553 | _AID = 1128 | |
554 | _API_HOSTNAME = 'aweme.snssdk.com' | |
555 | _UPLOADER_URL_FORMAT = 'https://www.douyin.com/user/%s' | |
556 | _WEBPAGE_HOST = 'https://www.douyin.com/' | |
557 | ||
558 | def _real_extract(self, url): | |
559 | video_id = self._match_id(url) | |
560 | ||
561 | try: | |
562 | return self._extract_aweme_app(video_id) | |
563 | except ExtractorError as e: | |
564 | self.report_warning(f'{e}; Retrying with webpage') | |
565 | ||
566 | webpage = self._download_webpage(url, video_id) | |
567 | render_data_json = self._search_regex( | |
568 | r'<script [^>]*\bid=[\'"]RENDER_DATA[\'"][^>]*>(%7B.+%7D)</script>', | |
569 | webpage, 'render data', default=None) | |
570 | if not render_data_json: | |
571 | # TODO: Run verification challenge code to generate signature cookies | |
572 | raise ExtractorError('Fresh cookies (not necessarily logged in) are needed') | |
573 | ||
574 | render_data = self._parse_json( | |
575 | render_data_json, video_id, transform_source=compat_urllib_parse_unquote) | |
576 | return self._parse_aweme_video_web( | |
577 | traverse_obj(render_data, (..., 'aweme', 'detail'), get_all=False), url) |