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

Code before:
        info = self._download_json(self.get_api_with_st_token(video_id, episode or 0), video_id)
        formats, subtitles = self._extract_m3u8_formats_and_subtitles(info['data']['url'], video_id, 'mp4')
        self._sort_formats(formats)
        return {
            'id': video_id,
            'title': join_nonempty(
                self._html_search_meta(('og:title', 'twitter:title'), webpage), episode, delim=' - '),
            'description': self._html_search_meta(['og:description', 'twitter:description'], webpage),
            'formats': formats,
            'subtitles': subtitles,
        }

    def get_api_with_st_token(self, video_id, episode):
        path = f'/api/v6.2_w/stream/vod/{video_id}/{episode}/auto_vip'
        timestamp = int(time.time()) + 10800
        m = hashlib.md5()
        m.update(f'WEBv6Dkdsad90dasdjlALDDDS{timestamp}{path}'.encode())
        st_token = self.encrypt(m.hexdigest())
        return f'https://api.fptplay.net{path}?{urllib.parse.urlencode({"st": st_token, "e": timestamp})}'

    def encrypt(self, string):
        n = []
        t = string.upper()
        r = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
        for o in range(len(t) // 2):
            n.append(int(f'0x{t[2 * o: 2 * o + 2]}', 16))

        def convert(e):
            t = ''
            n = 0
            i = [0, 0, 0]
            a = [0, 0, 0, 0]
            s = len(e)
            c = 0
            for z in range(s, 0, -1):
                if n <= 3:
                    i[n] = e[c]
                n += 1
                c += 1
                if 3 == n:
                    a[0] = (252 & i[0]) >> 2
                    a[1] = ((3 & i[0]) << 4) + ((240 & i[1]) >> 4)
                    a[2] = ((15 & i[1]) << 2) + ((192 & i[2]) >> 6)
                    a[3] = (63 & i[2])
                    for v in range(4):
                        t += r[a[v]]
                    n = 0
            if n:
                for o in range(n, 3, 1):
                    i[o] = 0


Review comment (suggestion):
```suggestion
        t = string.upper()
        r = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
        n = [int(f'0x{t[2 * o: 2 * o + 2]}', 16) for o in range(len(t) // 2)]
```

Code after:
        'url': 'https://fptplay.vn/xem-video/nha-co-chuyen-hi-alls-well-ends-well-1997-6218995f6af792ee370459f0',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        type_url, video_id, episode = self._match_valid_url(url).group('type', 'id', 'episode')
        webpage = self._download_webpage(url, video_id=video_id, fatal=False)
        info = self._download_json(self.get_api_with_st_token(video_id, episode or 0), video_id)
        formats, subtitles = self._extract_m3u8_formats_and_subtitles(info['data']['url'], video_id, 'mp4')
        self._sort_formats(formats)
        return {
            'id': video_id,
            'title': join_nonempty(
                self._html_search_meta(('og:title', 'twitter:title'), webpage), episode, delim=' - '),
            'description': self._html_search_meta(['og:description', 'twitter:description'], webpage),
            'formats': formats,
            'subtitles': subtitles,
        }

    def get_api_with_st_token(self, video_id, episode):
        path = f'/api/v6.2_w/stream/vod/{video_id}/{episode}/auto_vip'
        timestamp = int(time.time()) + 10800

        t = hashlib.md5(f'WEBv6Dkdsad90dasdjlALDDDS{timestamp}{path}'.encode()).hexdigest().upper()
        r = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
        n = [int(f'0x{t[2 * o: 2 * o + 2]}', 16) for o in range(len(t) // 2)]

        def convert(e):
            t = ''
            n = 0
            i = [0, 0, 0]
            a = [0, 0, 0, 0]
            s = len(e)
            c = 0
            for z in range(s, 0, -1):
                if n <= 3:
                    i[n] = e[c]
                n += 1
                c += 1
                if 3 == n:
                    a[0] = (252 & i[0]) >> 2
                    a[1] = ((3 & i[0]) << 4) + ((240 & i[1]) >> 4)
                    a[2] = ((15 & i[1]) << 2) + ((192 & i[2]) >> 6)
                    a[3] = (63 & i[2])
                    for v in range(4):
                        t += r[a[v]]
                    n = 0
            if n:
                for o in range(n, 3):
                    i[o] = 0