5 from .common
import InfoExtractor
6 from ..networking
import HEADRequest
22 class TrillerBaseIE(InfoExtractor
):
23 _NETRC_MACHINE
= 'triller'
24 _API_BASE_URL
= 'https://social.triller.co/v1.5'
25 _API_HEADERS
= {'Origin': 'https://triller.co'}
27 def _perform_login(self
, username
, password
):
28 if self
._API
_HEADERS
.get('Authorization'):
31 headers
= {**self._API_HEADERS, 'Content-Type': 'application/json'}
32 user_check
= traverse_obj(self
._download
_json
(
33 f
'{self._API_BASE_URL}/api/user/is-valid-username', None, note
='Checking username',
34 fatal
=False, expected_status
=400, headers
=headers
,
35 data
=json
.dumps({'username': username}
, separators
=(',', ':')).encode()), 'status')
37 if user_check
: # endpoint returns `"status":false` if username exists
38 raise ExtractorError('Unable to login: Invalid username', expected
=True)
40 login
= self
._download
_json
(
41 f
'{self._API_BASE_URL}/user/auth', None, note
='Logging in', fatal
=False,
42 expected_status
=400, headers
=headers
, data
=json
.dumps({
45 }, separators
=(',', ':')).encode()) or {}
47 if not login
.get('auth_token'):
48 if login
.get('error') == 1008:
49 raise ExtractorError('Unable to login: Incorrect password', expected
=True)
50 raise ExtractorError('Unable to login')
52 self
._API
_HEADERS
['Authorization'] = f
'Bearer {login["auth_token"]}'
54 def _get_comments(self
, video_id
, limit
=15):
55 comment_info
= self
._download
_json
(
56 f
'{self._API_BASE_URL}/api/videos/{video_id}/comments_v2',
57 video_id
, fatal
=False, note
='Downloading comments API JSON',
58 headers
=self
._API
_HEADERS
, query
={'limit': limit}
) or {}
59 if not comment_info
.get('comments'):
61 yield from traverse_obj(comment_info
, ('comments', ..., {
62 'id': ('id', {str_or_none}
),
64 'author': ('author', 'username'),
65 'author_id': ('author', 'user_id'),
66 'timestamp': ('timestamp', {unified_timestamp}
),
69 def _parse_video_info(self
, video_info
, username
, user_id
, display_id
=None):
70 video_id
= str(video_info
['id'])
71 display_id
= display_id
or video_info
.get('video_uuid')
73 if traverse_obj(video_info
, (
74 None, ('transcoded_url', 'video_url', 'stream_url', 'audio_url'),
75 {lambda x: re.search(r'/copyright/', x)}
), get_all
=False):
76 self
.raise_no_formats('This video has been removed due to licensing restrictions', expected
=True)
81 'ext': determine_ext(url
),
82 'format_id': url_basename(url
).split('.')[0],
87 if determine_ext(video_info
.get('transcoded_url')) == 'm3u8':
88 formats
.extend(self
._extract
_m
3u8_formats
(
89 video_info
['transcoded_url'], video_id
, 'mp4', m3u8_id
='hls', fatal
=False))
91 for video
in traverse_obj(video_info
, ('video_set', lambda _
, v
: url_or_none(v
['url']))):
93 **format_info(video
['url']),
94 **parse_resolution(video
.get('resolution')),
95 'vcodec': video
.get('codec'),
96 'vbr': int_or_none(video
.get('bitrate'), 1000),
99 video_url
= traverse_obj(video_info
, 'video_url', 'stream_url', expected_type
=url_or_none
)
102 **format_info(video_url
),
104 **traverse_obj(video_info
, {
107 'filesize': 'filesize',
108 }, expected_type
=int_or_none
),
111 audio_url
= url_or_none(video_info
.get('audio_url'))
113 formats
.append(format_info(audio_url
))
115 comment_count
= traverse_obj(video_info
, ('comment_count', {int_or_none}
))
119 'display_id': display_id
,
120 'uploader': username
,
121 'uploader_id': user_id
or traverse_obj(video_info
, ('user', 'user_id', {str_or_none}
)),
122 'webpage_url': urljoin(f
'https://triller.co/@{username}/video/', display_id
),
123 'uploader_url': f
'https://triller.co/@{username}',
124 'extractor_key': TrillerIE
.ie_key(),
125 'extractor': TrillerIE
.IE_NAME
,
127 'comment_count': comment_count
,
128 '__post_extractor': self
.extract_comments(video_id
, comment_count
),
129 **traverse_obj(video_info
, {
130 'title': ('description', {lambda x: x.replace('\r\n', ' ')}
),
131 'description': 'description',
132 'creator': ((('user'), ('users', lambda _
, v
: str(v
['user_id']) == user_id
)), 'name'),
133 'thumbnail': ('thumbnail_url', {url_or_none}
),
134 'timestamp': ('timestamp', {unified_timestamp}
),
135 'duration': ('duration', {int_or_none}
),
136 'view_count': ('play_count', {int_or_none}
),
137 'like_count': ('likes_count', {int_or_none}
),
138 'artist': 'song_artist',
139 'track': 'song_title',
144 class TrillerIE(TrillerBaseIE
):
145 _VALID_URL
= r
'''(?x)
146 https?://(?:www\.)?triller\.co/
147 @(?P<username>[\w.]+)/video/(?P<id>[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12})
150 'url': 'https://triller.co/@theestallion/video/2358fcd7-3df2-4c77-84c8-1d091610a6cf',
151 'md5': '228662d783923b60d78395fedddc0a20',
155 'title': 'md5:9a2bf9435c5c4292678996a464669416',
156 'thumbnail': r
're:^https://uploads\.cdn\.triller\.co/.+\.jpg$',
157 'description': 'md5:9a2bf9435c5c4292678996a464669416',
158 'uploader': 'theestallion',
159 'uploader_id': '18992236',
160 'creator': 'Megan Thee Stallion',
161 'timestamp': 1660598222,
162 'upload_date': '20220815',
166 'artist': 'Megan Thee Stallion',
168 'uploader_url': 'https://triller.co/@theestallion',
169 'comment_count': int,
171 'skip': 'This video has been removed due to licensing restrictions',
173 'url': 'https://triller.co/@charlidamelio/video/46c6fcfa-aa9e-4503-a50c-68444f44cddc',
174 'md5': '874055f462af5b0699b9dbb527a505a0',
178 'title': 'md5:4c91ea82760fe0fffb71b8c3aa7295fc',
179 'display_id': '46c6fcfa-aa9e-4503-a50c-68444f44cddc',
180 'thumbnail': r
're:^https://uploads\.cdn\.triller\.co/.+\.jpg$',
181 'description': 'md5:4c91ea82760fe0fffb71b8c3aa7295fc',
182 'uploader': 'charlidamelio',
183 'uploader_id': '1875551',
184 'creator': 'charli damelio',
185 'timestamp': 1660773354,
186 'upload_date': '20220817',
191 'track': 'Someone to Blame',
192 'uploader_url': 'https://triller.co/@charlidamelio',
193 'comment_count': int,
196 'url': 'https://triller.co/@theestallion/video/07f35f38-1f51-48e2-8c5f-f7a8e829988f',
197 'md5': 'af7b3553e4b8bfca507636471ee2eb41',
201 'title': 'UNGRATEFUL VIDEO OUT NOW ๐๐พ๐๐พ๐๐พ ๐๐ link my bio #womeninhiphop',
202 'display_id': '07f35f38-1f51-48e2-8c5f-f7a8e829988f',
203 'thumbnail': r
're:^https://uploads\.cdn\.triller\.co/.+\.jpg$',
204 'description': 'UNGRATEFUL VIDEO OUT NOW ๐๐พ๐๐พ๐๐พ ๐๐ link my bio\r\n #womeninhiphop',
205 'uploader': 'theestallion',
206 'uploader_id': '18992236',
207 'creator': 'Megan Thee Stallion',
208 'timestamp': 1662486178,
209 'upload_date': '20220906',
215 'uploader_url': 'https://triller.co/@theestallion',
216 'comment_count': int,
220 def _real_extract(self
, url
):
221 username
, display_id
= self
._match
_valid
_url
(url
).group('username', 'id')
223 video_info
= self
._download
_json
(
224 f
'{self._API_BASE_URL}/api/videos/{display_id}', display_id
,
225 headers
=self
._API
_HEADERS
)['videos'][0]
227 return self
._parse
_video
_info
(video_info
, username
, None, display_id
)
230 class TrillerUserIE(TrillerBaseIE
):
231 _VALID_URL
= r
'https?://(?:www\.)?triller\.co/@(?P<id>[\w.]+)/?(?:$|[#?])'
233 'url': 'https://triller.co/@theestallion',
234 'playlist_mincount': 12,
237 'title': 'theestallion',
238 'thumbnail': r
're:^https://uploads\.cdn\.triller\.co/.+\.jpg$',
241 'url': 'https://triller.co/@charlidamelio',
242 'playlist_mincount': 150,
245 'title': 'charlidamelio',
246 'thumbnail': r
're:^https://uploads\.cdn\.triller\.co/.+\.jpg$',
250 def _real_initialize(self
):
251 if not self
._API
_HEADERS
.get('Authorization'):
252 guest
= self
._download
_json
(
253 f
'{self._API_BASE_URL}/user/create_guest', None,
254 note
='Creating guest session', data
=b
'', headers
=self
._API
_HEADERS
, query
={
258 if not guest
.get('auth_token'):
259 raise ExtractorError('Unable to fetch required auth token for user extraction')
261 self
._API
_HEADERS
['Authorization'] = f
'Bearer {guest["auth_token"]}'
263 def _entries(self
, username
, user_id
, limit
=6):
264 query
= {'limit': limit}
265 for page
in itertools
.count(1):
266 videos
= self
._download
_json
(
267 f
'{self._API_BASE_URL}/api/users/{user_id}/videos',
268 username
, note
=f
'Downloading user video list page {page}',
269 headers
=self
._API
_HEADERS
, query
=query
)
271 for video
in traverse_obj(videos
, ('videos', ...)):
272 yield self
._parse
_video
_info
(video
, username
, user_id
)
274 query
['before_time'] = traverse_obj(videos
, ('videos', -1, 'timestamp'))
275 if not query
['before_time']:
278 def _real_extract(self
, url
):
279 username
= self
._match
_id
(url
)
281 user_info
= traverse_obj(self
._download
_json
(
282 f
'{self._API_BASE_URL}/api/users/by_username/{username}',
283 username
, note
='Downloading user info', headers
=self
._API
_HEADERS
), ('user', {dict}
)) or {}
285 if user_info
.get('private') and user_info
.get('followed_by_me') not in (True, 'true'):
286 raise ExtractorError('This user profile is private', expected
=True)
287 elif traverse_obj(user_info
, (('blocked_by_user', 'blocking_user'), {bool}
), get_all
=False):
288 raise ExtractorError('The author of the video is blocked', expected
=True)
290 user_id
= str_or_none(user_info
.get('user_id'))
292 raise ExtractorError('Unable to extract user ID')
294 return self
.playlist_result(
295 self
._entries
(username
, user_id
), user_id
, username
, thumbnail
=user_info
.get('avatar_url'))
298 class TrillerShortIE(InfoExtractor
):
299 _VALID_URL
= r
'https?://v\.triller\.co/(?P<id>\w+)'
301 'url': 'https://v.triller.co/WWZNWk',
302 'md5': '5eb8dc2c971bd8cd794ec9e8d5e9d101',
306 'title': 'md5:2dfc89d154cd91a4a18cd9582ba03e16',
307 'display_id': 'f4480e1f-fb4e-45b9-a44c-9e6c679ce7eb',
308 'thumbnail': r
're:^https://uploads\.cdn\.triller\.co/.+\.jpg$',
309 'description': 'md5:2dfc89d154cd91a4a18cd9582ba03e16',
310 'uploader': 'statefairent',
311 'uploader_id': '487545193',
312 'creator': 'Officialย Summerย Fairย ofย LA',
313 'timestamp': 1629655457,
314 'upload_date': '20210822',
320 'uploader_url': 'https://triller.co/@statefairent',
321 'comment_count': int,
325 def _real_extract(self
, url
):
326 real_url
= self
._request
_webpage
(HEADRequest(url
), self
._match
_id
(url
)).url
327 if self
.suitable(real_url
): # Prevent infinite loop in case redirect fails
328 raise UnsupportedError(real_url
)
329 return self
.url_result(real_url
)