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.

122 lines
4.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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 ..utils import (
  7. compat_str,
  8. unified_strdate,
  9. ExtractorError,
  10. )
  11. class RutubeIE(InfoExtractor):
  12. IE_NAME = 'rutube'
  13. IE_DESC = 'Rutube videos'
  14. _VALID_URL = r'https?://rutube\.ru/video/(?P<id>[\da-z]{32})'
  15. _TEST = {
  16. 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
  17. 'info_dict': {
  18. 'id': '3eac3b4561676c17df9132a9a1e62e3e',
  19. 'ext': 'mp4',
  20. 'title': 'Раненный кенгуру забежал в аптеку',
  21. 'description': 'http://www.ntdtv.ru ',
  22. 'duration': 80,
  23. 'uploader': 'NTDRussian',
  24. 'uploader_id': '29790',
  25. 'upload_date': '20131016',
  26. },
  27. 'params': {
  28. # It requires ffmpeg (m3u8 download)
  29. 'skip_download': True,
  30. },
  31. }
  32. def _real_extract(self, url):
  33. mobj = re.match(self._VALID_URL, url)
  34. video_id = mobj.group('id')
  35. video = self._download_json(
  36. 'http://rutube.ru/api/video/%s/?format=json' % video_id,
  37. video_id, 'Downloading video JSON')
  38. trackinfo = self._download_json(
  39. 'http://rutube.ru/api/play/trackinfo/%s/?format=json' % video_id,
  40. video_id, 'Downloading trackinfo JSON')
  41. # Some videos don't have the author field
  42. author = trackinfo.get('author') or {}
  43. m3u8_url = trackinfo['video_balancer'].get('m3u8')
  44. if m3u8_url is None:
  45. raise ExtractorError('Couldn\'t find m3u8 manifest url')
  46. return {
  47. 'id': video['id'],
  48. 'title': video['title'],
  49. 'description': video['description'],
  50. 'duration': video['duration'],
  51. 'view_count': video['hits'],
  52. 'url': m3u8_url,
  53. 'ext': 'mp4',
  54. 'thumbnail': video['thumbnail_url'],
  55. 'uploader': author.get('name'),
  56. 'uploader_id': compat_str(author['id']) if author else None,
  57. 'upload_date': unified_strdate(video['created_ts']),
  58. 'age_limit': 18 if video['is_adult'] else 0,
  59. }
  60. class RutubeChannelIE(InfoExtractor):
  61. IE_NAME = 'rutube:channel'
  62. IE_DESC = 'Rutube channels'
  63. _VALID_URL = r'http://rutube\.ru/tags/video/(?P<id>\d+)'
  64. _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
  65. def _extract_videos(self, channel_id, channel_title=None):
  66. entries = []
  67. for pagenum in itertools.count(1):
  68. page = self._download_json(
  69. self._PAGE_TEMPLATE % (channel_id, pagenum),
  70. channel_id, 'Downloading page %s' % pagenum)
  71. results = page['results']
  72. if not results:
  73. break
  74. entries.extend(self.url_result(result['video_url'], 'Rutube') for result in results)
  75. if not page['has_next']:
  76. break
  77. return self.playlist_result(entries, channel_id, channel_title)
  78. def _real_extract(self, url):
  79. mobj = re.match(self._VALID_URL, url)
  80. channel_id = mobj.group('id')
  81. return self._extract_videos(channel_id)
  82. class RutubeMovieIE(RutubeChannelIE):
  83. IE_NAME = 'rutube:movie'
  84. IE_DESC = 'Rutube movies'
  85. _VALID_URL = r'http://rutube\.ru/metainfo/tv/(?P<id>\d+)'
  86. _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
  87. _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
  88. def _real_extract(self, url):
  89. mobj = re.match(self._VALID_URL, url)
  90. movie_id = mobj.group('id')
  91. movie = self._download_json(
  92. self._MOVIE_TEMPLATE % movie_id, movie_id,
  93. 'Downloading movie JSON')
  94. movie_name = movie['name']
  95. return self._extract_videos(movie_id, movie_name)
  96. class RutubePersonIE(RutubeChannelIE):
  97. IE_NAME = 'rutube:person'
  98. IE_DESC = 'Rutube person videos'
  99. _VALID_URL = r'http://rutube\.ru/video/person/(?P<id>\d+)'
  100. _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'