Skip to content

Commit

Permalink
Improve sec_uid extraction and error message
Browse files Browse the repository at this point in the history
  • Loading branch information
bashonly committed Nov 14, 2023
1 parent 38d4483 commit 33da60a
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions yt_dlp_plugins/extractor/tt_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import string
import time

from yt_dlp.utils import ExtractorError, int_or_none, traverse_obj, try_call
from yt_dlp.utils import ExtractorError, int_or_none, traverse_obj
from yt_dlp.extractor.tiktok import TikTokIE, TikTokUserIE


Expand Down Expand Up @@ -98,10 +98,20 @@ def _get_sec_uid(self, user_url, user_name, msg):
webpage = self._download_webpage(
user_url, user_name, fatal=False, headers={'User-Agent': 'Mozilla/5.0'},
note=f'Downloading {msg} webpage', errnote=f'Unable to download {msg} webpage')
data = try_call(lambda: self._get_sigi_state(webpage, user_name))
return traverse_obj(
data, ('LiveRoom', 'liveRoomUserInfo', 'user', 'secUid'),
('UserModule', 'users', ..., 'secUid'), get_all=False, expected_type=str)
sec_uid = traverse_obj(self._search_json(
r'<script[^>]+\bid="__UNIVERSAL_DATA_FOR_REHYDRATION__"[^>]*>', webpage,
'rehydration data', user_name, end_pattern=r'</script>', default={}),
('__DEFAULT_SCOPE__', 'webapp.user-detail', 'userInfo', 'user', 'secUid', {str}))
if sec_uid:
return sec_uid
try:
return traverse_obj(
self._get_sigi_state(webpage, user_name),
('LiveRoom', 'liveRoomUserInfo', 'user', 'secUid'),
('UserModule', 'users', ..., 'secUid'),
get_all=False, expected_type=str)
except ExtractorError:
return None

def _real_extract(self, url):
user_name = self._match_id(url)
Expand All @@ -118,9 +128,11 @@ def _real_extract(self, url):

if not sec_uid:
webpage = self._download_webpage(
f'https://www.tiktok.com/embed/@{user_name}', user_name, note='Downloading user embed page')
f'https://www.tiktok.com/embed/@{user_name}', user_name,
note='Downloading user embed page', fatal=False)
data = traverse_obj(self._search_json(
r'<script[^>]+\bid=[\'"]__FRONTITY_CONNECT_STATE__[\'"][^>]*>', webpage, 'data', user_name),
r'<script[^>]+\bid=[\'"]__FRONTITY_CONNECT_STATE__[\'"][^>]*>',
webpage, 'data', user_name, default={}),
('source', 'data', f'/embed/@{user_name}', {dict}))

for aweme_id in traverse_obj(data, ('videoList', ..., 'id')):
Expand All @@ -132,7 +144,10 @@ def _real_extract(self, url):
break

if not sec_uid:
raise ExtractorError('Could not extract secondary user ID')
raise ExtractorError(
'Could not extract secondary user ID. '
'Try using --extractor-arg "tiktok:sec_uid=ID" with your command, '
'replacing "ID" with the channel_id of the requested user')

return self.playlist_result(self._entries(sec_uid, user_name), user_name)

Expand Down

0 comments on commit 33da60a

Please sign in to comment.