Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/archiveorg.py (Python)

Code before:

        def id_from_url(url, type_):
            return self._search_regex(
                rf'(?:{type_})/([^/#&?]+)', url or '', f'{type_} id', default=None)

        # XXX: would the get_elements_by_... functions be better suited here?
        # Uploader ID and URL
        _CHANNEL_URL_HREF_RE = r'href="[^"]*(?P<url>https?://www\.youtube\.com/(?:user|channel)/[^"]+)"'
        upch_url = self._search_regex(
            [fr'<(?:link\s*itemprop=\"url\"|a\s*id=\"watch-username\").*?\b{_CHANNEL_URL_HREF_RE}>',  # @fd05024
             fr'<div\s*id=\"(?:watch-channel-stats|watch-headline-user-info)\"[^>]*>\s*<a[^>]*\b{_CHANNEL_URL_HREF_RE}'],  # ~ May 2009, ~June 2012
            webpage, 'uploader or channel url', default=None)

        owner_profile_url = url_or_none(microformats.get('ownerProfileUrl'))  # @a6211d2

        # Uploader refers to the /user/ id ONLY
        uploader_id = (
            id_from_url(owner_profile_url, 'user')
            or id_from_url(upch_url, 'user')
            or ytcfg.get('VIDEO_USERNAME'))
        uploader_url = f'https://www.youtube.com/user/{uploader_id}' if uploader_id else None

        # TODO: sort out what uploader means
        uploader = (
            self._search_regex(
                [r'<a\s*id=\"watch-username\".*\">\s*<strong[^>]?>([^<]+)</strong>',
                 r'var\s*watchUsername\s*=\s*\'(.+)\';',  # ~May 2009
                 r'<div\s*\bid=\"watch-channel-stats\"[^>]*>\s*<a[^>]*>\s*(.+?)\s*</a',  # ~May 2009
                 r'<a\s*id=\"watch-userbanner\"[^>]*title=\"\s*(.+?)\s*\"'],  # ~June 2012
                webpage, 'uploader', default=None)
            or self._html_search_regex(
                [r'(?s)<div\s*class=\"yt-user-info\".*?<a[^>]*[^>]*>\s*(.*?)\s*<\/a',  # March 2016
                 r'(?s)<a[^>]*yt-user-name[^>]*>\s*(.*?)\s*<\/a'],  # july 2013
                get_element_by_id('watch7-user-header', webpage), 'uploader', default=None)
            or self._html_search_regex(
                r'<button\s*href=\"\/user\/[^>]*>\s*<span[^>]*>\s*(.+?)\s*<',  # April 2012
                get_element_by_id('watch-headline-user-info', webpage), 'uploader', default=None)
            or traverse_obj(player_config, ('args', 'creator'))
            or video_details.get('author'))

        channel_id = str_or_none(
            video_details.get('channelId')
            or microformats.get('externalChannelId')
            or search_meta('channelId')
            or self._search_regex(
                r'data-channel-external-id=(["\'])(?P<id>(?:(?!\1).)+)\1',  # @b45a9e6
                webpage, 'channel id', default=None, group='id')
            or id_from_url(owner_profile_url, 'channel')
            or id_from_url(upch_url, 'channel')
            or traverse_obj(player_config, ('args', 'ucid')))


Review comment (suggestion):
u sure it should be greedy?
```suggestion
                [r'<a\s*id="watch-username".*?">\s*<strong[^>]?>([^<]+)</strong>',
```

Code after:

        def id_from_url(url, type_):
            return self._search_regex(
                rf'(?:{type_})/([^/#&?]+)', url or '', f'{type_} id', default=None)

        # XXX: would the get_elements_by_... functions be better suited here?
        _CHANNEL_URL_HREF_RE = r'href="[^"]*(?P<url>https?://www\.youtube\.com/(?:user|channel)/[^"]+)"'
        uploader_or_channel_url = self._search_regex(
            [fr'<(?:link\s*itemprop=\"url\"|a\s*id=\"watch-username\").*?\b{_CHANNEL_URL_HREF_RE}>',  # @fd05024
             fr'<div\s*id=\"(?:watch-channel-stats|watch-headline-user-info)\"[^>]*>\s*<a[^>]*\b{_CHANNEL_URL_HREF_RE}'],  # ~ May 2009, ~June 2012
            webpage, 'uploader or channel url', default=None)

        owner_profile_url = url_or_none(microformats.get('ownerProfileUrl'))  # @a6211d2

        # Uploader refers to the /user/ id ONLY
        uploader_id = (
            id_from_url(owner_profile_url, 'user')
            or id_from_url(uploader_or_channel_url, 'user')
            or ytcfg.get('VIDEO_USERNAME'))
        uploader_url = f'https://www.youtube.com/user/{uploader_id}' if uploader_id else None

        # XXX: do we want to differentiate uploader and channel?
        uploader = (
            self._search_regex(
                [r'<a\s*id="watch-username"[^>]*>\s*<strong>([^<]+)</strong>',  # June 2010
                 r'var\s*watchUsername\s*=\s*\'(.+?)\';',  # ~May 2009
                 r'<div\s*\bid=\"watch-channel-stats"[^>]*>\s*<a[^>]*>\s*(.+?)\s*</a',  # ~May 2009
                 r'<a\s*id="watch-userbanner"[^>]*title="\s*(.+?)\s*"'],  # ~June 2012
                webpage, 'uploader', default=None)
            or self._html_search_regex(
                [r'(?s)<div\s*class="yt-user-info".*?<a[^>]*[^>]*>\s*(.*?)\s*</a',  # March 2016
                 r'(?s)<a[^>]*yt-user-name[^>]*>\s*(.*?)\s*</a'],  # july 2013
                get_element_by_id('watch7-user-header', webpage), 'uploader', default=None)
            or self._html_search_regex(
                r'<button\s*href="/user/[^>]*>\s*<span[^>]*>\s*(.+?)\s*<',  # April 2012
                get_element_by_id('watch-headline-user-info', webpage), 'uploader', default=None)
            or traverse_obj(player_config, ('args', 'creator'))
            or video_details.get('author'))

        channel_id = str_or_none(
            video_details.get('channelId')
            or microformats.get('externalChannelId')
            or search_meta('channelId')
            or self._search_regex(
                r'data-channel-external-id=(["\'])(?P<id>(?:(?!\1).)+)\1',  # @b45a9e6
                webpage, 'channel id', default=None, group='id')
            or id_from_url(owner_profile_url, 'channel')
            or id_from_url(uploader_or_channel_url, 'channel')
            or traverse_obj(player_config, ('args', 'ucid')))

        channel_url = f'https://www.youtube.com/channel/{channel_id}' if channel_id else None