]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/youtube.py
[cleanup] misc
[yt-dlp.git] / yt_dlp / extractor / youtube.py
index 1ef80445eb6156f57b7872762d302ad7cc6f0f5d..54f5ef15cc16a129783727cd6a43aaf4abf2bb68 100644 (file)
@@ -258,28 +258,12 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
     # If True it will raise an error if no login info is provided
     _LOGIN_REQUIRED = False
 
-    r'''  # Unused since login is broken
-    _LOGIN_URL = 'https://accounts.google.com/ServiceLogin'
-    _TWOFACTOR_URL = 'https://accounts.google.com/signin/challenge'
-
-    _LOOKUP_URL = 'https://accounts.google.com/_/signin/sl/lookup'
-    _CHALLENGE_URL = 'https://accounts.google.com/_/signin/sl/challenge'
-    _TFA_URL = 'https://accounts.google.com/_/signin/challenge?hl=en&TL={0}'
-    '''
-
     def _login(self):
         """
         Attempt to log in to YouTube.
-        True is returned if successful or skipped.
-        False is returned if login failed.
-
         If _LOGIN_REQUIRED is set and no authentication was provided, an error is raised.
         """
 
-        def warn(message):
-            self.report_warning(message)
-
-        # username+password login is broken
         if (self._LOGIN_REQUIRED
                 and self.get_param('cookiefile') is None
                 and self.get_param('cookiesfrombrowser') is None):
@@ -287,184 +271,7 @@ def warn(message):
                 'Login details are needed to download this content', method='cookies')
         username, password = self._get_login_info()
         if username:
-            warn('Logging in using username and password is broken. %s' % self._LOGIN_HINTS['cookies'])
-        return
-
-        # Everything below this is broken!
-        r'''
-        # No authentication to be performed
-        if username is None:
-            if self._LOGIN_REQUIRED and self.get_param('cookiefile') is None:
-                raise ExtractorError('No login info available, needed for using %s.' % self.IE_NAME, expected=True)
-            # if self.get_param('cookiefile'):  # TODO remove 'and False' later - too many people using outdated cookies and open issues, remind them.
-            #     self.to_screen('[Cookies] Reminder - Make sure to always use up to date cookies!')
-            return True
-
-        login_page = self._download_webpage(
-            self._LOGIN_URL, None,
-            note='Downloading login page',
-            errnote='unable to fetch login page', fatal=False)
-        if login_page is False:
-            return
-
-        login_form = self._hidden_inputs(login_page)
-
-        def req(url, f_req, note, errnote):
-            data = login_form.copy()
-            data.update({
-                'pstMsg': 1,
-                'checkConnection': 'youtube',
-                'checkedDomains': 'youtube',
-                'hl': 'en',
-                'deviceinfo': '[null,null,null,[],null,"US",null,null,[],"GlifWebSignIn",null,[null,null,[]]]',
-                'f.req': json.dumps(f_req),
-                'flowName': 'GlifWebSignIn',
-                'flowEntry': 'ServiceLogin',
-                # TODO: reverse actual botguard identifier generation algo
-                'bgRequest': '["identifier",""]',
-            })
-            return self._download_json(
-                url, None, note=note, errnote=errnote,
-                transform_source=lambda s: re.sub(r'^[^[]*', '', s),
-                fatal=False,
-                data=urlencode_postdata(data), headers={
-                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
-                    'Google-Accounts-XSRF': 1,
-                })
-
-        lookup_req = [
-            username,
-            None, [], None, 'US', None, None, 2, False, True,
-            [
-                None, None,
-                [2, 1, None, 1,
-                 'https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26action_handle_signin%3Dtrue%26hl%3Den%26app%3Ddesktop%26feature%3Dsign_in_button&hl=en&service=youtube&uilel=3&requestPath=%2FServiceLogin&Page=PasswordSeparationSignIn',
-                 None, [], 4],
-                1, [None, None, []], None, None, None, True
-            ],
-            username,
-        ]
-
-        lookup_results = req(
-            self._LOOKUP_URL, lookup_req,
-            'Looking up account info', 'Unable to look up account info')
-
-        if lookup_results is False:
-            return False
-
-        user_hash = try_get(lookup_results, lambda x: x[0][2], compat_str)
-        if not user_hash:
-            warn('Unable to extract user hash')
-            return False
-
-        challenge_req = [
-            user_hash,
-            None, 1, None, [1, None, None, None, [password, None, True]],
-            [
-                None, None, [2, 1, None, 1, 'https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26action_handle_signin%3Dtrue%26hl%3Den%26app%3Ddesktop%26feature%3Dsign_in_button&hl=en&service=youtube&uilel=3&requestPath=%2FServiceLogin&Page=PasswordSeparationSignIn', None, [], 4],
-                1, [None, None, []], None, None, None, True
-            ]]
-
-        challenge_results = req(
-            self._CHALLENGE_URL, challenge_req,
-            'Logging in', 'Unable to log in')
-
-        if challenge_results is False:
-            return
-
-        login_res = try_get(challenge_results, lambda x: x[0][5], list)
-        if login_res:
-            login_msg = try_get(login_res, lambda x: x[5], compat_str)
-            warn(
-                'Unable to login: %s' % 'Invalid password'
-                if login_msg == 'INCORRECT_ANSWER_ENTERED' else login_msg)
-            return False
-
-        res = try_get(challenge_results, lambda x: x[0][-1], list)
-        if not res:
-            warn('Unable to extract result entry')
-            return False
-
-        login_challenge = try_get(res, lambda x: x[0][0], list)
-        if login_challenge:
-            challenge_str = try_get(login_challenge, lambda x: x[2], compat_str)
-            if challenge_str == 'TWO_STEP_VERIFICATION':
-                # SEND_SUCCESS - TFA code has been successfully sent to phone
-                # QUOTA_EXCEEDED - reached the limit of TFA codes
-                status = try_get(login_challenge, lambda x: x[5], compat_str)
-                if status == 'QUOTA_EXCEEDED':
-                    warn('Exceeded the limit of TFA codes, try later')
-                    return False
-
-                tl = try_get(challenge_results, lambda x: x[1][2], compat_str)
-                if not tl:
-                    warn('Unable to extract TL')
-                    return False
-
-                tfa_code = self._get_tfa_info('2-step verification code')
-
-                if not tfa_code:
-                    warn(
-                        'Two-factor authentication required. Provide it either interactively or with --twofactor <code>'
-                        '(Note that only TOTP (Google Authenticator App) codes work at this time.)')
-                    return False
-
-                tfa_code = remove_start(tfa_code, 'G-')
-
-                tfa_req = [
-                    user_hash, None, 2, None,
-                    [
-                        9, None, None, None, None, None, None, None,
-                        [None, tfa_code, True, 2]
-                    ]]
-
-                tfa_results = req(
-                    self._TFA_URL.format(tl), tfa_req,
-                    'Submitting TFA code', 'Unable to submit TFA code')
-
-                if tfa_results is False:
-                    return False
-
-                tfa_res = try_get(tfa_results, lambda x: x[0][5], list)
-                if tfa_res:
-                    tfa_msg = try_get(tfa_res, lambda x: x[5], compat_str)
-                    warn(
-                        'Unable to finish TFA: %s' % 'Invalid TFA code'
-                        if tfa_msg == 'INCORRECT_ANSWER_ENTERED' else tfa_msg)
-                    return False
-
-                check_cookie_url = try_get(
-                    tfa_results, lambda x: x[0][-1][2], compat_str)
-            else:
-                CHALLENGES = {
-                    'LOGIN_CHALLENGE': "This device isn't recognized. For your security, Google wants to make sure it's really you.",
-                    'USERNAME_RECOVERY': 'Please provide additional information to aid in the recovery process.',
-                    'REAUTH': "There is something unusual about your activity. For your security, Google wants to make sure it's really you.",
-                }
-                challenge = CHALLENGES.get(
-                    challenge_str,
-                    '%s returned error %s.' % (self.IE_NAME, challenge_str))
-                warn('%s\nGo to https://accounts.google.com/, login and solve a challenge.' % challenge)
-                return False
-        else:
-            check_cookie_url = try_get(res, lambda x: x[2], compat_str)
-
-        if not check_cookie_url:
-            warn('Unable to extract CheckCookie URL')
-            return False
-
-        check_cookie_results = self._download_webpage(
-            check_cookie_url, None, 'Checking cookie', fatal=False)
-
-        if check_cookie_results is False:
-            return False
-
-        if 'https://myaccount.google.com/' not in check_cookie_results:
-            warn('Unable to log in')
-            return False
-
-        return True
-        '''
+            self.report_warning(f'Cannot login to YouTube using username and password. {self._LOGIN_HINTS["cookies"]}')
 
     def _initialize_consent(self):
         cookies = self._get_cookies('https://www.youtube.com/')
@@ -483,10 +290,7 @@ def _initialize_consent(self):
 
     def _real_initialize(self):
         self._initialize_consent()
-        if self._downloader is None:
-            return
-        if not self._login():
-            return
+        self._login()
 
     _YT_INITIAL_DATA_RE = r'(?:window\s*\[\s*["\']ytInitialData["\']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;'
     _YT_INITIAL_PLAYER_RESPONSE_RE = r'ytInitialPlayerResponse\s*=\s*({.+?})\s*;'
@@ -2314,6 +2118,10 @@ def extract_thread(contents):
             continuation_token = self._generate_comment_continuation(video_id)
             continuation = self._build_api_continuation_query(continuation_token, None)
 
+        message = self._get_text(root_continuation_data, ('contents', ..., 'messageRenderer', 'text'), max_runs=1)
+        if message and not parent:
+            self.report_warning(message, video_id=video_id)
+
         visitor_data = None
         is_first_continuation = parent is None
 
@@ -2416,8 +2224,10 @@ def _generate_comment_continuation(video_id):
     def _get_comments(self, ytcfg, video_id, contents, webpage):
         """Entry for comment extraction"""
         def _real_comment_extract(contents):
-            yield from self._comment_entries(
-                traverse_obj(contents, (..., 'itemSectionRenderer'), get_all=False), ytcfg, video_id)
+            renderer = next((
+                item for item in traverse_obj(contents, (..., 'itemSectionRenderer'), default={})
+                if item.get('sectionIdentifier') == 'comment-item-section'), None)
+            yield from self._comment_entries(renderer, ytcfg, video_id)
 
         max_comments = int_or_none(self._configuration_arg('max_comments', [''])[0])
         # Force English regardless of account setting to prevent parsing issues
@@ -2692,7 +2502,9 @@ def guess_quality(f):
                     itag = self._search_regex(
                         r'/itag/(\d+)', f['url'], 'itag', default=None)
                     if itag in itags:
-                        continue
+                        itag += '-hls'
+                        if itag in itags:
+                            continue
                     if itag:
                         f['format_id'] = itag
                         itags.append(itag)
@@ -2704,8 +2516,11 @@ def guess_quality(f):
                 for f in self._extract_mpd_formats(dash_manifest_url, video_id, fatal=False):
                     itag = f['format_id']
                     if itag in itags:
-                        continue
+                        itag += '-dash'
+                        if itag in itags:
+                            continue
                     if itag:
+                        f['format_id'] = itag
                         itags.append(itag)
                     f['quality'] = guess_quality(f)
                     filesize = int_or_none(self._search_regex(
@@ -2838,7 +2653,7 @@ def feed_entry(name):
 
         # Source is given priority since formats that throttle are given lower source_preference
         # When throttling issue is fully fixed, remove this
-        self._sort_formats(formats, ('quality', 'res', 'fps', 'source', 'codec:vp9.2', 'lang'))
+        self._sort_formats(formats, ('quality', 'res', 'fps', 'hdr:12', 'source', 'codec:vp9.2', 'lang'))
 
         keywords = get_first(video_details, 'keywords', expected_type=list) or []
         if not keywords and webpage:
@@ -2884,21 +2699,18 @@ def feed_entry(name):
         # The best resolution thumbnails sometimes does not appear in the webpage
         # See: https://github.com/ytdl-org/youtube-dl/issues/29049, https://github.com/yt-dlp/yt-dlp/issues/340
         # List of possible thumbnails - Ref: <https://stackoverflow.com/a/20542029>
-        hq_thumbnail_names = ['maxresdefault', 'hq720', 'sddefault', 'sd1', 'sd2', 'sd3']
-        # TODO: Test them also? - For some videos, even these don't exist
-        guaranteed_thumbnail_names = [
+        thumbnail_names = [
+            'maxresdefault', 'hq720', 'sddefault', 'sd1', 'sd2', 'sd3',
             'hqdefault', 'hq1', 'hq2', 'hq3', '0',
             'mqdefault', 'mq1', 'mq2', 'mq3',
             'default', '1', '2', '3'
         ]
-        thumbnail_names = hq_thumbnail_names + guaranteed_thumbnail_names
         n_thumbnail_names = len(thumbnail_names)
 
         thumbnails.extend({
             'url': 'https://i.ytimg.com/vi{webp}/{video_id}/{name}{live}.{ext}'.format(
                 video_id=video_id, name=name, ext=ext,
                 webp='_webp' if ext == 'webp' else '', live='_live' if is_live else ''),
-            '_test_url': name in hq_thumbnail_names,
         } for name in thumbnail_names for ext in ('webp', 'jpg'))
         for thumb in thumbnails:
             i = next((i for i, t in enumerate(thumbnail_names) if f'/{video_id}/{t}' in thumb['url']), n_thumbnail_names)
@@ -4492,9 +4304,7 @@ class YoutubePlaylistIE(InfoExtractor):
     def suitable(cls, url):
         if YoutubeTabIE.suitable(url):
             return False
-        # Hack for lazy extractors until more generic solution is implemented
-        # (see #28780)
-        from .youtube import parse_qs
+        from ..utils import parse_qs
         qs = parse_qs(url)
         if qs.get('v', [None])[0]:
             return False