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

Code before:
        }
    }, {
        'url': 'https://andrewzimmern.substack.com/p/mussels-with-black-bean-sauce-recipe',
        'md5': 'fd3c07077b02444ff0130715b5f632bb',
        'info_dict': {
            'id': '47368578',
            'ext': 'mp4',
            'title': 'Mussels with Black Bean Sauce: Recipe of the Week #7',
            'description': 'md5:b96234a2906c7d854d5229818d889515',
            'thumbnail': 'md5:e30bfaa9da40e82aa62354263a9dd232',
            'uploader': 'andrewzimmern',
        }
    }]

    @staticmethod
    def is_custom_domain(webpage):
        return re.search(r'<script[^>]+src=["\']https://substackcdn.com/.*\.js', webpage) is not None

    @staticmethod
    def url_from_custom_domain(url):
        custom_domain = r'https?://(?:www\.)?(?P<username>[\w-]+)\.(?:[a-z]+)/(?P<params>.+)'

        username = re.search(custom_domain, url).group('username')
        params = re.search(custom_domain, url).group('params')

        return f'https://{username}.substack.com/{params}'

    def _extract_video_formats(self, video_id, username):
        formats, subtitles = [], {}
        for video_format in ('hls', 'mp4'):
            video_url = f'https://{username}.substack.com/api/v1/video/upload/{video_id}/src?type={video_format}'

            if video_format == 'hls':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(video_url, video_id, 'mp4', fatal=False)
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append({
                    'url': video_url,
                    'ext': video_format,
                })

        return formats, subtitles

    def _real_extract(self, url):
        display_id, username = self._match_valid_url(url).group('id', 'username')
        webpage = self._download_webpage(url, display_id)

        post_info = self._search_json(
            r'<script[^>]*>\s*window\._preloads\s*=\s*', webpage, 'preloads', display_id)['post']


Review comment (refactor):
like this

```suggestion
    @classmethod
    def _extract_url(cls, webpage, url):
        if not re.search(r'<script[^>]+src=["\']https://substackcdn.com/[^"\']+\.js', webpage):
            return
        parsed = urlparse(url)
        return parsed._replace(netloc=f'{parsed.netloc.split(".")[-2]}.substack.com').geturl()
```

Code after:
            'description': 'md5:a57f2439319e56e0af92dd0c95d75797',
            'thumbnail': 'md5:daa40b6b79249417c14ff8103db29639',
            'uploader': 'Maybe Baby',
            'uploader_id': '33628',
        }
    }, {
        'url': 'https://andrewzimmern.substack.com/p/mussels-with-black-bean-sauce-recipe',
        'md5': 'fd3c07077b02444ff0130715b5f632bb',
        'info_dict': {
            'id': '47368578',
            'ext': 'mp4',
            'title': 'Mussels with Black Bean Sauce: Recipe of the Week #7',
            'description': 'md5:b96234a2906c7d854d5229818d889515',
            'thumbnail': 'md5:e30bfaa9da40e82aa62354263a9dd232',
            'uploader': "Andrew Zimmern's Spilled Milk ",
            'uploader_id': '577659',
        }
    }]

    @classmethod
    def _extract_url(cls, webpage, url):
        if not re.search(r'<script[^>]+src=["\']https://substackcdn.com/[^"\']+\.js', webpage):
            return

        mobj = re.search(r'{[^}]*["\']subdomain["\']\s*:\s*["\'](?P<subdomain>[^"]+)', webpage)
        if mobj:
            parsed = urllib.parse.urlparse(url)
            return parsed._replace(netloc=f'{mobj.group("subdomain")}.substack.com').geturl()

    def _extract_video_formats(self, video_id, username):
        formats, subtitles = [], {}
        for video_format in ('hls', 'mp4'):
            video_url = f'https://{username}.substack.com/api/v1/video/upload/{video_id}/src?type={video_format}'

            if video_format == 'hls':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(video_url, video_id, 'mp4', fatal=False)
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append({
                    'url': video_url,
                    'ext': video_format,
                })

        return formats, subtitles

    def _real_extract(self, url):
        display_id, username = self._match_valid_url(url).group('id', 'username')
        webpage = self._download_webpage(url, display_id)

        webpage_info = self._search_json(r'<script[^>]*>\s*window\._preloads\s*=', webpage, 'preloads', display_id)