logo

youtube-dl

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

yandexvideo.py (5258B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. int_or_none,
  7. try_get,
  8. url_or_none,
  9. )
  10. class YandexVideoIE(InfoExtractor):
  11. _VALID_URL = r'''(?x)
  12. https?://
  13. (?:
  14. yandex\.ru(?:/(?:portal/(?:video|efir)|efir))?/?\?.*?stream_id=|
  15. frontend\.vh\.yandex\.ru/player/
  16. )
  17. (?P<id>(?:[\da-f]{32}|[\w-]{12}))
  18. '''
  19. _TESTS = [{
  20. 'url': 'https://yandex.ru/portal/video?stream_id=4dbb36ec4e0526d58f9f2dc8f0ecf374',
  21. 'md5': 'e02a05bfaf0d9615ef07ae3a10f4faf4',
  22. 'info_dict': {
  23. 'id': '4dbb36ec4e0526d58f9f2dc8f0ecf374',
  24. 'ext': 'mp4',
  25. 'title': 'Русский Вудсток - главный рок-фест в истории СССР / вДудь',
  26. 'description': 'md5:7d6b8d4bc4a3b9a56499916c1ea5b5fa',
  27. 'thumbnail': r're:^https?://',
  28. 'timestamp': 1549972939,
  29. 'duration': 5575,
  30. 'age_limit': 18,
  31. 'upload_date': '20190212',
  32. 'view_count': int,
  33. 'like_count': int,
  34. 'dislike_count': int,
  35. },
  36. }, {
  37. 'url': 'https://yandex.ru/portal/efir?stream_id=4dbb262b4fe5cf15a215de4f34eee34d&from=morda',
  38. 'only_matching': True,
  39. }, {
  40. 'url': 'https://yandex.ru/?stream_id=4dbb262b4fe5cf15a215de4f34eee34d',
  41. 'only_matching': True,
  42. }, {
  43. 'url': 'https://frontend.vh.yandex.ru/player/4dbb262b4fe5cf15a215de4f34eee34d?from=morda',
  44. 'only_matching': True,
  45. }, {
  46. # vod-episode, series episode
  47. 'url': 'https://yandex.ru/portal/video?stream_id=45b11db6e4b68797919c93751a938cee',
  48. 'only_matching': True,
  49. }, {
  50. # episode, sports
  51. 'url': 'https://yandex.ru/?stream_channel=1538487871&stream_id=4132a07f71fb0396be93d74b3477131d',
  52. 'only_matching': True,
  53. }, {
  54. # DASH with DRM
  55. 'url': 'https://yandex.ru/portal/video?from=morda&stream_id=485a92d94518d73a9d0ff778e13505f8',
  56. 'only_matching': True,
  57. }, {
  58. 'url': 'https://yandex.ru/efir?stream_active=watching&stream_id=v7a2dZ-v5mSI&from_block=efir_newtab',
  59. 'only_matching': True,
  60. }]
  61. def _real_extract(self, url):
  62. video_id = self._match_id(url)
  63. player = try_get((self._download_json(
  64. 'https://frontend.vh.yandex.ru/graphql', video_id, data=('''{
  65. player(content_id: "%s") {
  66. computed_title
  67. content_url
  68. description
  69. dislikes
  70. duration
  71. likes
  72. program_title
  73. release_date
  74. release_date_ut
  75. release_year
  76. restriction_age
  77. season
  78. start_time
  79. streams
  80. thumbnail
  81. title
  82. views_count
  83. }
  84. }''' % video_id).encode(), fatal=False)), lambda x: x['player']['content'])
  85. if not player or player.get('error'):
  86. player = self._download_json(
  87. 'https://frontend.vh.yandex.ru/v23/player/%s.json' % video_id,
  88. video_id, query={
  89. 'stream_options': 'hires',
  90. 'disable_trackings': 1,
  91. })
  92. content = player['content']
  93. title = content.get('title') or content['computed_title']
  94. formats = []
  95. streams = content.get('streams') or []
  96. streams.append({'url': content.get('content_url')})
  97. for stream in streams:
  98. content_url = url_or_none(stream.get('url'))
  99. if not content_url:
  100. continue
  101. ext = determine_ext(content_url)
  102. if ext == 'ismc':
  103. continue
  104. elif ext == 'm3u8':
  105. formats.extend(self._extract_m3u8_formats(
  106. content_url, video_id, 'mp4',
  107. 'm3u8_native', m3u8_id='hls', fatal=False))
  108. elif ext == 'mpd':
  109. formats.extend(self._extract_mpd_formats(
  110. content_url, video_id, mpd_id='dash', fatal=False))
  111. else:
  112. formats.append({'url': content_url})
  113. self._sort_formats(formats)
  114. timestamp = (int_or_none(content.get('release_date'))
  115. or int_or_none(content.get('release_date_ut'))
  116. or int_or_none(content.get('start_time')))
  117. season = content.get('season') or {}
  118. return {
  119. 'id': video_id,
  120. 'title': title,
  121. 'description': content.get('description'),
  122. 'thumbnail': content.get('thumbnail'),
  123. 'timestamp': timestamp,
  124. 'duration': int_or_none(content.get('duration')),
  125. 'series': content.get('program_title'),
  126. 'age_limit': int_or_none(content.get('restriction_age')),
  127. 'view_count': int_or_none(content.get('views_count')),
  128. 'like_count': int_or_none(content.get('likes')),
  129. 'dislike_count': int_or_none(content.get('dislikes')),
  130. 'season_number': int_or_none(season.get('season_number')),
  131. 'season_id': season.get('id'),
  132. 'release_year': int_or_none(content.get('release_year')),
  133. 'formats': formats,
  134. }