summaryrefslogtreecommitdiffstats
path: root/yt_dlp/extractor/gofile.py
blob: fac08846213986d9beb71ab3a80be32d90c56eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import hashlib

from .common import InfoExtractor
from ..utils import ExtractorError, try_get


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',
                'release_timestamp': 1638338704,
                'release_date': '20211201',
            }
        }]
    }, {
        'url': 'https://gofile.io/d/is8lKr',
        'info_dict': {
            'id': 'TMjXd9',
            'ext': 'mp4',
        },
        'playlist_count': 0,
        'skip': 'No video/audio found at provided URL.',
    }, {
        'url': 'https://gofile.io/d/TMjXd9',
        'info_dict': {
            'id': 'TMjXd9',
        },
        'playlist_count': 1,
    }, {
        'url': 'https://gofile.io/d/gqOtRf',
        'info_dict': {
            'id': 'gqOtRf',
        },
        'playlist_mincount': 1,
        'params': {
            'videopassword': 'password',
        },
    }]
    _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/accounts', None, 'Getting a new guest account', data=b'{}')
        self._TOKEN = account_data['data']['token']
        self._set_cookie('.gofile.io', 'accountToken', self._TOKEN)

    def _entries(self, file_id):
        query_params = {'wt': '4fd6sg89d7s6'}  # From https://gofile.io/dist/js/alljs.js
        password = self.get_param('videopassword')
        if password:
            query_params['password'] = hashlib.sha256(password.encode('utf-8')).hexdigest()
        files = self._download_json(
            f'https://api.gofile.io/contents/{file_id}', file_id, 'Getting filelist',
            query=query_params, headers={'Authorization': f'Bearer {self._TOKEN}'})

        status = files['status']
        if status == 'error-passwordRequired':
            raise ExtractorError(
                'This video is protected by a password, use the --video-password option', expected=True)
        elif status != 'ok':
            raise ExtractorError(f'{self.IE_NAME} said: status {status}', expected=True)

        found_files = False
        for file in (try_get(files, lambda x: x['data']['children'], dict) or {}).values():
            file_type, file_format = file.get('mimetype').split('/', 1)
            if file_type not in ('video', 'audio') and file_format != 'vnd.mts':
                continue

            found_files = True
            file_url = file.get('link')
            if file_url:
                yield {
                    'id': file['id'],
                    'title': file['name'].rsplit('.', 1)[0],
                    'url': file_url,
                    'filesize': file.get('size'),
                    'release_timestamp': file.get('createTime')
                }

        if not found_files:
            raise ExtractorError('No video/audio found at provided URL.', expected=True)

    def _real_extract(self, url):
        file_id = self._match_id(url)
        return self.playlist_result(self._entries(file_id), playlist_id=file_id)