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

Code before:
            '3600': [1440, 810],
            '5000': [1920, 1080],
            '10000': [1920, 1080],
        }

        def get_location(url, headers={}):
            _url = compat_urllib_parse_urlparse(url)
            conn = compat_http_client.HTTPConnection(_url.netloc)
            headers.update({'Connection': 'close'})
            try:
                conn.request(
                    'HEAD', '%s?%s' % (_url.path, _url.query),
                    headers=headers)
            except Exception:
                return None

            resp = conn.getresponse()

            if resp.status == 200:
                return True
            elif resp.status == 302:
                return resp.getheader('Location')
            elif resp.status == 404:
                return None
            return None

        def get_format_info(tbr):
            br = int_or_none(tbr)
            if len(fmts) == 1 and not br:
                br = fmts[0].get('tbr')

            for f in fmts:
                if f.get('tbr'):
                    if (br - br / 100 * 10) <= f.get('tbr') <= (br + br / 100 * 10):
                        return [
                            f.get('width'),
                            f.get('height'),
                            f.get('tbr'),
                            f.get('format_id').split('-')[1],
                        ]

            return [None, None, None, tbr]

        def quality_tag(q, qualities):
            return '_%s' % q if (len(qualities) > 1 or int_or_none(q)) else ''

        cdn_id, cdn_num = None, None
        loc = get_location(relinker_url)
        loc = loc if loc is not True else ''
        mobj = re.match(_MP4_REG, loc or '')
        if mobj:

Review comment (suggestion):
Use this only if you cannot do what you want with `_download_webpage_handle` or even `YoutubeDL.urlopen`

I do see some other small stuff I can review, but this is the most important part

Code after:
            '10000': [1920, 1080],
        }

        def test_url(url):
            resp = self._request_webpage(
                HEADRequest(url), None, headers={'User-Agent': 'Rai'},
                fatal=False, errnote=False, note=False)

            if resp is False:
                return False

            if resp.code == 200:
                return False if resp.url == url else resp.url
            return None

        def get_format_info(tbr):
            import math
            br = int_or_none(tbr)
            if len(fmts) == 1 and not br:
                br = fmts[0].get('tbr')
            if br > 300:
                tbr = compat_str(math.floor(br / 100) * 100)
            else:
                tbr = '250'

            # try extracting info from available m3u8 formats
            format_copy = None
            for f in fmts:
                if f.get('tbr'):
                    br_limit = math.floor(br / 100)
                    if br_limit - 1 <= math.floor(f['tbr'] / 100) <= br_limit + 1:
                        format_copy = f.copy()
            return {
                'width': format_copy.get('width'),
                'height': format_copy.get('height'),
                'tbr': format_copy.get('tbr'),
                'vcodec': format_copy.get('vcodec'),
                'acodec': format_copy.get('acodec'),
                'fps': format_copy.get('fps'),
                'format_id': 'https-%s' % tbr,
            } if format_copy else {
                'width': _QUALITY[tbr][0],
                'height': _QUALITY[tbr][1],
                'format_id': 'https-%s' % tbr,
                'tbr': int(tbr),
            }

        loc = test_url(_MP4_TMPL % (relinker_url, '*'))
        if not isinstance(loc, compat_str):
            return []