logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

yandexdisk.py (5166B)


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