yandexdisk.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import json
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. determine_ext,
  5. float_or_none,
  6. int_or_none,
  7. join_nonempty,
  8. mimetype2ext,
  9. try_get,
  10. urljoin,
  11. )
  12. class YandexDiskIE(InfoExtractor):
  13. _VALID_URL = r'''(?x)https?://
  14. (?P<domain>
  15. yadi\.sk|
  16. disk\.yandex\.
  17. (?:
  18. az|
  19. by|
  20. co(?:m(?:\.(?:am|ge|tr))?|\.il)|
  21. ee|
  22. fr|
  23. k[gz]|
  24. l[tv]|
  25. md|
  26. t[jm]|
  27. u[az]|
  28. ru
  29. )
  30. )/(?:[di]/|public.*?\bhash=)(?P<id>[^/?#&]+)'''
  31. _TESTS = [{
  32. 'url': 'https://yadi.sk/i/VdOeDou8eZs6Y',
  33. 'md5': 'a4a8d52958c8fddcf9845935070402ae',
  34. 'info_dict': {
  35. 'id': 'VdOeDou8eZs6Y',
  36. 'ext': 'mp4',
  37. 'title': '4.mp4',
  38. 'duration': 168.6,
  39. 'uploader': 'y.botova',
  40. 'uploader_id': '300043621',
  41. 'view_count': int,
  42. },
  43. 'expected_warnings': ['Unable to download JSON metadata'],
  44. }, {
  45. 'url': 'https://yadi.sk/d/h3WAXvDS3Li3Ce',
  46. 'only_matching': True,
  47. }, {
  48. 'url': 'https://yadi.sk/public?hash=5DZ296JK9GWCLp02f6jrObjnctjRxMs8L6%2B%2FuhNqk38%3D',
  49. 'only_matching': True,
  50. }]
  51. def _real_extract(self, url):
  52. domain, video_id = self._match_valid_url(url).groups()
  53. webpage = self._download_webpage(url, video_id)
  54. store = self._parse_json(self._search_regex(
  55. r'<script[^>]+id="store-prefetch"[^>]*>\s*({.+?})\s*</script>',
  56. webpage, 'store'), video_id)
  57. resource = store['resources'][store['rootResourceId']]
  58. title = resource['name']
  59. meta = resource.get('meta') or {}
  60. public_url = meta.get('short_url')
  61. if public_url:
  62. video_id = self._match_id(public_url)
  63. source_url = (self._download_json(
  64. 'https://cloud-api.yandex.net/v1/disk/public/resources/download',
  65. video_id, query={'public_key': url}, fatal=False) or {}).get('href')
  66. video_streams = resource.get('videoStreams') or {}
  67. video_hash = resource.get('hash') or url
  68. environment = store.get('environment') or {}
  69. sk = environment.get('sk')
  70. yandexuid = environment.get('yandexuid')
  71. if sk and yandexuid and not (source_url and video_streams):
  72. self._set_cookie(domain, 'yandexuid', yandexuid)
  73. def call_api(action):
  74. return (self._download_json(
  75. urljoin(url, '/public/api/') + action, video_id, data=json.dumps({
  76. 'hash': video_hash,
  77. 'sk': sk,
  78. }).encode(), headers={
  79. 'Content-Type': 'text/plain',
  80. }, fatal=False) or {}).get('data') or {}
  81. if not source_url:
  82. # TODO: figure out how to detect if download limit has
  83. # been reached and then avoid unnecessary source format
  84. # extraction requests
  85. source_url = call_api('download-url').get('url')
  86. if not video_streams:
  87. video_streams = call_api('get-video-streams')
  88. formats = []
  89. if source_url:
  90. formats.append({
  91. 'url': source_url,
  92. 'format_id': 'source',
  93. 'ext': determine_ext(title, meta.get('ext') or mimetype2ext(meta.get('mime_type')) or 'mp4'),
  94. 'quality': 1,
  95. 'filesize': int_or_none(meta.get('size')),
  96. })
  97. for video in (video_streams.get('videos') or []):
  98. format_url = video.get('url')
  99. if not format_url:
  100. continue
  101. if video.get('dimension') == 'adaptive':
  102. formats.extend(self._extract_m3u8_formats(
  103. format_url, video_id, 'mp4', 'm3u8_native',
  104. m3u8_id='hls', fatal=False))
  105. else:
  106. size = video.get('size') or {}
  107. height = int_or_none(size.get('height'))
  108. formats.append({
  109. 'ext': 'mp4',
  110. 'format_id': join_nonempty('hls', height and f'{height}p'),
  111. 'height': height,
  112. 'protocol': 'm3u8_native',
  113. 'url': format_url,
  114. 'width': int_or_none(size.get('width')),
  115. })
  116. uid = resource.get('uid')
  117. display_name = try_get(store, lambda x: x['users'][uid]['displayName'])
  118. return {
  119. 'id': video_id,
  120. 'title': title,
  121. 'duration': float_or_none(video_streams.get('duration'), 1000),
  122. 'uploader': display_name,
  123. 'uploader_id': uid,
  124. 'view_count': int_or_none(meta.get('views_counter')),
  125. 'formats': formats,
  126. }