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

Code before:
    def _real_extract(self, url):
        video_id = self._match_id(url)

        webpage = self._download_webpage(url, video_id)

        if re.search(r'alert\(["\']This video has been deleted', webpage):
            raise ExtractorError(
                'Video %s has been deleted' % video_id, expected=True)

        video_id = self._search_regex(
            r'vod.afreecatv.com/player/(\d+)/', webpage, 'title', default=video_id)

        partial_view = False
        adult_view = False
        for _ in range(2):
            video_json = self._download_json(
                "https://api.m.afreecatv.com/station/video/a/view",
                video_id, headers={'Referer': url}, data=urlencode_postdata({
                    'nTitleNo': video_id,
                    'nApiLevel': 10,
                }))
            station_id = video_json['data']['station_no']
            bbs_id = video_json['data']['bbs_no']
            query = {
                'nTitleNo': video_id,
                'nStationNo': station_id,
                'nBbsNo': bbs_id,
            }
            if partial_view:
                query['partialView'] = 'SKIP_ADULT'
            if adult_view:
                query['adultView'] = 'ADULT_VIEW'
            video_xml = self._download_xml(
                'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
                video_id, 'Downloading video info XML%s'
                % (' (skipping adult)' if partial_view else ''),
                video_id, headers={
                    'Referer': url,
                }, query=query)

            flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
            if flag and flag == 'SUCCEED':
                break
            if flag == 'PARTIAL_ADULT':
                self.report_warning(
                    'In accordance with local laws and regulations, underage users are restricted from watching adult content. '
                    'Only content suitable for all ages will be downloaded. '
                    'Provide account credentials if you wish to download restricted content.')
                partial_view = True
                continue
            elif flag == 'ADULT':

Review comment (refactor):
single-quote URL, and you could simplify it like this:
```suggestion
            data = self._download_json(
                'https://api.m.afreecatv.com/station/video/a/view',
                video_id, headers={'Referer': url}, data=urlencode_postdata({
                    'nTitleNo': video_id,
                    'nApiLevel': 10,
                }))['data']
            query = {
                'nTitleNo': video_id,
                'nStationNo': data['station_no'],
```
and then on the following line:
```py
                'nBbsNo': data['bbs_no'],
```
(github wouldn't let me include it in the suggestion)

Code after:
        video_id = self._match_id(url)

        partial_view = False
        adult_view = False
        for _ in range(2):
            data = self._download_json(
                'https://api.m.afreecatv.com/station/video/a/view',
                video_id, headers={'Referer': url}, data=urlencode_postdata({
                    'nTitleNo': video_id,
                    'nApiLevel': 10,
                }))['data']
            if traverse_obj(data, ('code', {int})) == -6221:
                raise ExtractorError('The VOD does not exist', expected=True)
            query = {
                'nTitleNo': video_id,
                'nStationNo': data['station_no'],
                'nBbsNo': data['bbs_no'],
            }
            if partial_view:
                query['partialView'] = 'SKIP_ADULT'
            if adult_view:
                query['adultView'] = 'ADULT_VIEW'
            video_xml = self._download_xml(
                'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
                video_id, 'Downloading video info XML%s'
                % (' (skipping adult)' if partial_view else ''),
                video_id, headers={
                    'Referer': url,
                }, query=query)

            flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
            if flag and flag == 'SUCCEED':
                break
            if flag == 'PARTIAL_ADULT':
                self.report_warning(
                    'In accordance with local laws and regulations, underage users are restricted from watching adult content. '
                    'Only content suitable for all ages will be downloaded. '
                    'Provide account credentials if you wish to download restricted content.')
                partial_view = True
                continue
            elif flag == 'ADULT':
                if not adult_view:
                    adult_view = True
                    continue
                error = 'Only users older than 19 are able to watch this video. Provide account credentials to download this content.'
            else:
                error = flag
            raise ExtractorError(
                '%s said: %s' % (self.IE_NAME, error), expected=True)
        else:
            raise ExtractorError('Unable to download video info')