gofile.py 3.4 KB

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