yandexdisk.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import compat_HTTPError
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. float_or_none,
  10. int_or_none,
  11. mimetype2ext,
  12. parse_iso8601,
  13. urljoin,
  14. )
  15. class YandexDiskIE(InfoExtractor):
  16. _VALID_URL = r'''(?x)https?://
  17. (?:
  18. (?:www\.)?yadi\.sk|
  19. disk\.yandex\.
  20. (?:
  21. az|
  22. by|
  23. co(?:m(?:\.(?:am|ge|tr))?|\.il)|
  24. ee|
  25. fr|
  26. k[gz]|
  27. l[tv]|
  28. md|
  29. t[jm]|
  30. u[az]|
  31. ru
  32. )
  33. )/(?:[di]/|public.*?\bhash=)(?P<id>[^/?#&]+)'''
  34. _TESTS = [{
  35. 'url': 'https://yadi.sk/i/VdOeDou8eZs6Y',
  36. 'md5': '33955d7ae052f15853dc41f35f17581c',
  37. 'info_dict': {
  38. 'id': 'VdOeDou8eZs6Y',
  39. 'ext': 'mp4',
  40. 'title': '4.mp4',
  41. 'duration': 168.6,
  42. 'uploader': 'y.botova',
  43. 'uploader_id': '300043621',
  44. 'timestamp': 1421396809,
  45. 'upload_date': '20150116',
  46. 'view_count': int,
  47. },
  48. }, {
  49. 'url': 'https://yadi.sk/d/h3WAXvDS3Li3Ce',
  50. 'only_matching': True,
  51. }, {
  52. 'url': 'https://yadi.sk/public?hash=5DZ296JK9GWCLp02f6jrObjnctjRxMs8L6%2B%2FuhNqk38%3D',
  53. 'only_matching': True,
  54. }]
  55. def _real_extract(self, url):
  56. video_id = self._match_id(url)
  57. try:
  58. resource = self._download_json(
  59. 'https://cloud-api.yandex.net/v1/disk/public/resources',
  60. video_id, query={'public_key': url})
  61. except ExtractorError as e:
  62. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  63. error_description = self._parse_json(
  64. e.cause.read().decode(), video_id)['description']
  65. raise ExtractorError(error_description, expected=True)
  66. raise
  67. title = resource['name']
  68. public_url = resource.get('public_url')
  69. if public_url:
  70. video_id = self._match_id(public_url)
  71. self._set_cookie('yadi.sk', 'yandexuid', '0')
  72. def call_api(action):
  73. return (self._download_json(
  74. urljoin(url, '/public/api/') + action, video_id, data=json.dumps({
  75. 'hash': url,
  76. # obtain sk if needed from call_api('check-auth') while
  77. # the yandexuid cookie is set and sending an empty JSON object
  78. 'sk': 'ya6b52f8c6b12abe91a66d22d3a31084b'
  79. }).encode(), headers={
  80. 'Content-Type': 'text/plain',
  81. }, fatal=False) or {}).get('data') or {}
  82. formats = []
  83. source_url = resource.get('file')
  84. if not source_url:
  85. source_url = call_api('download-url').get('url')
  86. if source_url:
  87. formats.append({
  88. 'url': source_url,
  89. 'format_id': 'source',
  90. 'ext': determine_ext(title, mimetype2ext(resource.get('mime_type')) or 'mp4'),
  91. 'quality': 1,
  92. 'filesize': int_or_none(resource.get('size'))
  93. })
  94. video_streams = call_api('get-video-streams')
  95. for video in (video_streams.get('videos') or []):
  96. format_url = video.get('url')
  97. if not format_url:
  98. continue
  99. if video.get('dimension') == 'adaptive':
  100. formats.extend(self._extract_m3u8_formats(
  101. format_url, video_id, 'mp4', 'm3u8_native',
  102. m3u8_id='hls', fatal=False))
  103. else:
  104. size = video.get('size') or {}
  105. height = int_or_none(size.get('height'))
  106. format_id = 'hls'
  107. if height:
  108. format_id += '-%dp' % height
  109. formats.append({
  110. 'ext': 'mp4',
  111. 'format_id': format_id,
  112. 'height': height,
  113. 'protocol': 'm3u8_native',
  114. 'url': format_url,
  115. 'width': int_or_none(size.get('width')),
  116. })
  117. self._sort_formats(formats)
  118. owner = resource.get('owner') or {}
  119. return {
  120. 'id': video_id,
  121. 'title': title,
  122. 'duration': float_or_none(video_streams.get('duration'), 1000),
  123. 'uploader': owner.get('display_name'),
  124. 'uploader_id': owner.get('uid'),
  125. 'view_count': int_or_none(resource.get('views_count')),
  126. 'timestamp': parse_iso8601(resource.get('created')),
  127. 'formats': formats,
  128. }