]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/gofile.py
[cleanup] Upgrade syntax
[yt-dlp.git] / yt_dlp / extractor / gofile.py
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 try_get
5 )
6
7
8 class GofileIE(InfoExtractor):
9 _VALID_URL = r'https?://(?:www\.)?gofile\.io/d/(?P<id>[^/]+)'
10 _TESTS = [{
11 'url': 'https://gofile.io/d/AMZyDw',
12 'info_dict': {
13 'id': 'AMZyDw',
14 },
15 'playlist_mincount': 2,
16 'playlist': [{
17 'info_dict': {
18 'id': 'de571ac1-5edc-42e2-8ec2-bdac83ad4a31',
19 'filesize': 928116,
20 'ext': 'mp4',
21 'title': 'nuuh',
22 'release_timestamp': 1638338704,
23 'release_date': '20211201',
24 }
25 }]
26 }, {
27 'url': 'https://gofile.io/d/is8lKr',
28 'info_dict': {
29 'id': 'TMjXd9',
30 'ext': 'mp4',
31 },
32 'playlist_count': 0,
33 'skip': 'No video/audio found at provided URL.',
34 }, {
35 'url': 'https://gofile.io/d/TMjXd9',
36 'info_dict': {
37 'id': 'TMjXd9',
38 },
39 'playlist_count': 1,
40 }]
41 _TOKEN = None
42
43 def _real_initialize(self):
44 token = self._get_cookies('https://gofile.io/').get('accountToken')
45 if token:
46 self._TOKEN = token.value
47 return
48
49 account_data = self._download_json(
50 'https://api.gofile.io/createAccount', None, note='Getting a new guest account')
51 self._TOKEN = account_data['data']['token']
52 self._set_cookie('gofile.io', 'accountToken', self._TOKEN)
53
54 def _entries(self, file_id):
55 files = self._download_json('https://api.gofile.io/getContent', 'Gofile', note='Getting filelist', query={
56 'contentId': file_id,
57 'token': self._TOKEN,
58 'websiteToken': 12345,
59 })
60
61 status = files['status']
62 if status != 'ok':
63 raise ExtractorError(f'{self.IE_NAME} said: status {status}', expected=True)
64
65 found_files = False
66 for file in (try_get(files, lambda x: x['data']['contents'], dict) or {}).values():
67 file_type, file_format = file.get('mimetype').split('/', 1)
68 if file_type not in ('video', 'audio') and file_format != 'vnd.mts':
69 continue
70
71 found_files = True
72 file_url = file.get('link')
73 if file_url:
74 yield {
75 'id': file['id'],
76 'title': file['name'].rsplit('.', 1)[0],
77 'url': file_url,
78 'filesize': file.get('size'),
79 'release_timestamp': file.get('createTime')
80 }
81
82 if not found_files:
83 raise ExtractorError('No video/audio found at provided URL.', expected=True)
84
85 def _real_extract(self, url):
86 file_id = self._match_id(url)
87 return self.playlist_result(self._entries(file_id), playlist_id=file_id)