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

Code before:
)


class GofileIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?gofile\.io/d/(?P<id>[^/]+)'
    _TESTS = [{
        'url': 'https://gofile.io/d/AMZyDw',
        'info_dict': {
            'id': 'AMZyDw',
        },
        'playlist_mincount': 2,
        'playlist': [{
            'info_dict': {
                'id': 'de571ac1-5edc-42e2-8ec2-bdac83ad4a31',
                'filesize': 928116,
                'ext': 'mp4',
                'title': 'nuuh'
            }
        }]
    }]

    def _entries(self, file_id):

        # Create guest account
        accountdata = self._download_json('https://api.gofile.io/createAccount', 'Gofile', note='Getting a new guest account')
        accountToken = accountdata['data']['token']

        # Get file list
        requesturl = f'https://api.gofile.io/getContent?contentId={file_id}&token={accountToken}&websiteToken=websiteToken&cache=true'
        filelist = self._download_json(requesturl, 'Gofile', note='Getting filelist')

        status = filelist['status']
        if status != "ok":
            raise ExtractorError('Received error from service, status: %s\n' % status, expected=True)

        contents = try_get(filelist, lambda x: x['data']['contents'], dict)

        for _, file in contents.items():

            filetype = file['mimetype'].split("/", 1)[0]
            if filetype != "video" and filetype != "audio":
                continue

            filedata = {
                'id': file['id'],
                'title': file['name'].rsplit('.', 1)[0],
                'url': file['directLink'],
                'filesize': file['size'],
                'release_timestamp': file['createTime']
            }
            yield filedata

Review comment (performance):
move this to `_real_initialize`. That way if user gives `yt-dlp URL1 URL2`, the token is only generated once. Also, if user gives cookies, use that token instead of generating a new one

PS: This is what Ashish meant by "cache the token"

Code after:
)


class GofileIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?gofile\.io/d/(?P<id>[^/]+)'
    _TESTS = [{
        'url': 'https://gofile.io/d/AMZyDw',
        'info_dict': {
            'id': 'AMZyDw',
        },
        'playlist_mincount': 2,
        'playlist': [{
            'info_dict': {
                'id': 'de571ac1-5edc-42e2-8ec2-bdac83ad4a31',
                'filesize': 928116,
                'ext': 'mp4',
                'title': 'nuuh'
            }
        }]
    }, {  # URL to test mixed file types
        'url': 'https://gofile.io/d/avt34h',
        'info_dict': {
            'id': 'avt34h',
        },
        'playlist_mincount': 1,
    }, {  # URL to test no video/audio error
        'url': 'https://gofile.io/d/aB03lZ',
        'info_dict': {
            'id': 'aB03lZ',
        },
        'playlist_count': 0,
        'skip': 'No video/audio found at provided URL.',
    }]
    _TOKEN = None

    def _real_initialize(self):
        token = self._get_cookies('https://gofile.io/').get('accountToken')
        if token:
            self._TOKEN = token.value
            return

        account_data = self._download_json(
            'https://api.gofile.io/createAccount', None, note='Getting a new guest account')
        self._TOKEN = account_data['data']['token']
        self._set_cookie('gofile.io', 'accountToken', self._TOKEN)

    def _entries(self, file_id):
        files = self._download_json(
            f'https://api.gofile.io/getContent?contentId={file_id}&token={self._TOKEN}&websiteToken=websiteToken&cache=true',
            'Gofile', note='Getting filelist')