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

Code before:
            }
        },
        {
            'url': 'https://zenporn.com/video/15872038/glad-you-came/',
            'md5': '296ccab437f5bac6099433768449d8e1',
            'info_dict': {
                'id': '15872038',
                'extr_id': '111585',
                'ext': 'mp4',
                'title': 'Glad You Came',
                'description': '',
                'thumbnail': 'https://vpim.m3pd.com/contents/videos_screenshots/111000/111585/480x270/1.jpg',
                'post_date': '20231024',
                'uploader': 'Martin Rudenko',
                'age_limit': 18
            }
        }
    ]

    def _gen_info_url(self, ext_domain, extr_id, lifetime=86400):
        """ This function is a reverse engineering from the website javascript """
        dyn_a = int_or_none(1e6 * math.floor(int_or_none(extr_id) / 1e6))
        dyn_b = int_or_none(1e3 * math.floor(int_or_none(extr_id) / 1e3))
        if dyn_a is None or dyn_b is None:
            raise ExtractorError('Unable to generate the ``gen_info_url``.')

        return f'https://{ext_domain}/api/json/video/{lifetime}/{dyn_a}/{dyn_b}/{extr_id}.json'

    def _decode_video_url(self, ext_domain, encoded_url):
        """ This function is a reverse engineering from the website javascript """
        cust_char_set = 'АВСDЕFGHIJKLМNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,~'
        decoded_url = ''
        cur_pos = 0

        # Check for characters not in the custom character set and handle errors
        if any(char not in cust_char_set for char in encoded_url):
            return None

        # Filter out characters not in the custom character set
        encoded_url = ''.join(char for char in encoded_url if char in cust_char_set)

        while cur_pos < len(encoded_url):
            o = cust_char_set.index(encoded_url[cur_pos])
            i = cust_char_set.index(encoded_url[cur_pos + 1])
            s = cust_char_set.index(encoded_url[cur_pos + 2])
            a = cust_char_set.index(encoded_url[cur_pos + 3])

            o = (o << 2) | (i >> 4)
            i = ((i & 15) << 4) | (s >> 2)
            l = ((s & 3) << 6) | a


Review comment (refactor):
This can fatal since `int_or_none` can return `None`, which we cant divide. Let's secure against that:
```suggestion
        extr_id = int_or_none(extr_id)
        if extr_id is None:
            raise ExtractorError('Unable to generate the `gen_info_url`')
        dyn_a = extr_id // 1_000_000 * 1_000_000
        dyn_b = extr_id // 1_000 * 1_000
```
We can also use a comprehention to generate the path
```py
        result = '/'.join(extr_id // i * i for i in (1_000_000, 1_000, 1))
        return f'https://{ext_domain}/api/json/video/{lifetime}/{result}.json'
```


Code after:
            'title': 'Glad You Came',
            'description': '',
            'thumbnail': 'https://vpim.m3pd.com/contents/videos_screenshots/111000/111585/480x270/1.jpg',
            'upload_date': '20231024',
            'uploader': 'Martin Rudenko',
            'age_limit': 18,
        }
    }]

    def _gen_info_url(self, ext_domain, extr_id, lifetime=86400):
        """ This function is a reverse engineering from the website javascript """
        result = '/'.join(str(int(extr_id) // i * i) for i in (1_000_000, 1_000, 1))
        return f'https://{ext_domain}/api/json/video/{lifetime}/{result}.json'

    @staticmethod
    def _decode_video_url(encoded_url):
        """ This function is a reverse engineering from the website javascript """
        # Replace lookalike characters and standardize map
        translation = str.maketrans('АВСЕМ.,~', 'ABCEM+/=')
        try:
            return base64.b64decode(encoded_url.translate(translation), validate=True).decode()
        except (binascii.Error, ValueError):
            return None

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)

        ext_domain, video_id = self._search_regex(
            r'https://(?P<ext_domain>[\w.-]+\.\w{3})/embed/(?P<extr_id>\d+)/',
            webpage, 'embed info', group=('ext_domain', 'extr_id'))

        info_json = self._download_json(
            self._gen_info_url(ext_domain, video_id), video_id, fatal=False)

        video_json = self._download_json(
            f'https://{ext_domain}/api/videofile.php', video_id, query={
                'video_id': video_id,
                'lifetime': 8640000,
            }, note='Downloading video file JSON', errnote='Failed to download video file JSON')

        decoded_url = self._decode_video_url(video_json[0]['video_url'])
        if not decoded_url:
            raise ExtractorError('Unable to decode the video url')

        return {
            'id': video_id,
            'display_id': display_id,
            'ext': traverse_obj(video_json, (0, 'format', {determine_ext})),
            'url': f'https://{ext_domain}{decoded_url}',
            'age_limit': 18,