You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import itertools
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_str,
  8. )
  9. from ..utils import (
  10. determine_ext,
  11. unified_strdate,
  12. )
  13. class RutubeIE(InfoExtractor):
  14. IE_NAME = 'rutube'
  15. IE_DESC = 'Rutube videos'
  16. _VALID_URL = r'https?://rutube\.ru/(?:video|play/embed)/(?P<id>[\da-z]{32})'
  17. _TESTS = [{
  18. 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
  19. 'info_dict': {
  20. 'id': '3eac3b4561676c17df9132a9a1e62e3e',
  21. 'ext': 'mp4',
  22. 'title': 'Раненный кенгуру забежал в аптеку',
  23. 'description': 'http://www.ntdtv.ru ',
  24. 'duration': 80,
  25. 'uploader': 'NTDRussian',
  26. 'uploader_id': '29790',
  27. 'upload_date': '20131016',
  28. 'age_limit': 0,
  29. },
  30. 'params': {
  31. # It requires ffmpeg (m3u8 download)
  32. 'skip_download': True,
  33. },
  34. }, {
  35. 'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661',
  36. 'only_matching': True,
  37. }]
  38. def _real_extract(self, url):
  39. video_id = self._match_id(url)
  40. video = self._download_json(
  41. 'http://rutube.ru/api/video/%s/?format=json' % video_id,
  42. video_id, 'Downloading video JSON')
  43. # Some videos don't have the author field
  44. author = video.get('author') or {}
  45. options = self._download_json(
  46. 'http://rutube.ru/api/play/options/%s/?format=json' % video_id,
  47. video_id, 'Downloading options JSON')
  48. formats = []
  49. for format_id, format_url in options['video_balancer'].items():
  50. ext = determine_ext(format_url)
  51. if ext == 'm3u8':
  52. formats.extend(self._extract_m3u8_formats(
  53. format_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  54. elif ext == 'f4m':
  55. formats.extend(self._extract_f4m_formats(
  56. format_url, video_id, f4m_id=format_id, fatal=False))
  57. else:
  58. formats.append({
  59. 'url': format_url,
  60. 'format_id': format_id,
  61. })
  62. self._sort_formats(formats)
  63. return {
  64. 'id': video['id'],
  65. 'title': video['title'],
  66. 'description': video['description'],
  67. 'duration': video['duration'],
  68. 'view_count': video['hits'],
  69. 'formats': formats,
  70. 'thumbnail': video['thumbnail_url'],
  71. 'uploader': author.get('name'),
  72. 'uploader_id': compat_str(author['id']) if author else None,
  73. 'upload_date': unified_strdate(video['created_ts']),
  74. 'age_limit': 18 if video['is_adult'] else 0,
  75. }
  76. class RutubeEmbedIE(InfoExtractor):
  77. IE_NAME = 'rutube:embed'
  78. IE_DESC = 'Rutube embedded videos'
  79. _VALID_URL = 'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)'
  80. _TESTS = [{
  81. 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=',
  82. 'info_dict': {
  83. 'id': 'a10e53b86e8f349080f718582ce4c661',
  84. 'ext': 'mp4',
  85. 'upload_date': '20131223',
  86. 'uploader_id': '297833',
  87. 'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89',
  88. 'uploader': 'subziro89 ILya',
  89. 'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89',
  90. },
  91. 'params': {
  92. 'skip_download': 'Requires ffmpeg',
  93. },
  94. }, {
  95. 'url': 'http://rutube.ru/play/embed/8083783',
  96. 'only_matching': True,
  97. }]
  98. def _real_extract(self, url):
  99. embed_id = self._match_id(url)
  100. webpage = self._download_webpage(url, embed_id)
  101. canonical_url = self._html_search_regex(
  102. r'<link\s+rel="canonical"\s+href="([^"]+?)"', webpage,
  103. 'Canonical URL')
  104. return self.url_result(canonical_url, 'Rutube')
  105. class RutubeChannelIE(InfoExtractor):
  106. IE_NAME = 'rutube:channel'
  107. IE_DESC = 'Rutube channels'
  108. _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)'
  109. _TESTS = [{
  110. 'url': 'http://rutube.ru/tags/video/1800/',
  111. 'info_dict': {
  112. 'id': '1800',
  113. },
  114. 'playlist_mincount': 68,
  115. }]
  116. _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
  117. def _extract_videos(self, channel_id, channel_title=None):
  118. entries = []
  119. for pagenum in itertools.count(1):
  120. page = self._download_json(
  121. self._PAGE_TEMPLATE % (channel_id, pagenum),
  122. channel_id, 'Downloading page %s' % pagenum)
  123. results = page['results']
  124. if not results:
  125. break
  126. entries.extend(self.url_result(result['video_url'], 'Rutube') for result in results)
  127. if not page['has_next']:
  128. break
  129. return self.playlist_result(entries, channel_id, channel_title)
  130. def _real_extract(self, url):
  131. mobj = re.match(self._VALID_URL, url)
  132. channel_id = mobj.group('id')
  133. return self._extract_videos(channel_id)
  134. class RutubeMovieIE(RutubeChannelIE):
  135. IE_NAME = 'rutube:movie'
  136. IE_DESC = 'Rutube movies'
  137. _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)'
  138. _TESTS = []
  139. _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
  140. _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
  141. def _real_extract(self, url):
  142. movie_id = self._match_id(url)
  143. movie = self._download_json(
  144. self._MOVIE_TEMPLATE % movie_id, movie_id,
  145. 'Downloading movie JSON')
  146. movie_name = movie['name']
  147. return self._extract_videos(movie_id, movie_name)
  148. class RutubePersonIE(RutubeChannelIE):
  149. IE_NAME = 'rutube:person'
  150. IE_DESC = 'Rutube person videos'
  151. _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)'
  152. _TESTS = [{
  153. 'url': 'http://rutube.ru/video/person/313878/',
  154. 'info_dict': {
  155. 'id': '313878',
  156. },
  157. 'playlist_mincount': 37,
  158. }]
  159. _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'