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.

283 lines
9.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # coding: 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. compat_parse_qs,
  9. compat_urllib_parse_urlparse,
  10. )
  11. from ..utils import (
  12. determine_ext,
  13. bool_or_none,
  14. int_or_none,
  15. try_get,
  16. unified_timestamp,
  17. url_or_none,
  18. )
  19. class RutubeBaseIE(InfoExtractor):
  20. def _extract_video(self, video, video_id=None, require_title=True):
  21. title = video['title'] if require_title else video.get('title')
  22. age_limit = video.get('is_adult')
  23. if age_limit is not None:
  24. age_limit = 18 if age_limit is True else 0
  25. uploader_id = try_get(video, lambda x: x['author']['id'])
  26. category = try_get(video, lambda x: x['category']['name'])
  27. return {
  28. 'id': video.get('id') or video_id,
  29. 'title': title,
  30. 'description': video.get('description'),
  31. 'thumbnail': video.get('thumbnail_url'),
  32. 'duration': int_or_none(video.get('duration')),
  33. 'uploader': try_get(video, lambda x: x['author']['name']),
  34. 'uploader_id': compat_str(uploader_id) if uploader_id else None,
  35. 'timestamp': unified_timestamp(video.get('created_ts')),
  36. 'category': [category] if category else None,
  37. 'age_limit': age_limit,
  38. 'view_count': int_or_none(video.get('hits')),
  39. 'comment_count': int_or_none(video.get('comments_count')),
  40. 'is_live': bool_or_none(video.get('is_livestream')),
  41. }
  42. class RutubeIE(RutubeBaseIE):
  43. IE_NAME = 'rutube'
  44. IE_DESC = 'Rutube videos'
  45. _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})'
  46. _TESTS = [{
  47. 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
  48. 'md5': '79938ade01294ef7e27574890d0d3769',
  49. 'info_dict': {
  50. 'id': '3eac3b4561676c17df9132a9a1e62e3e',
  51. 'ext': 'flv',
  52. 'title': 'Раненный кенгуру забежал в аптеку',
  53. 'description': 'http://www.ntdtv.ru ',
  54. 'duration': 80,
  55. 'uploader': 'NTDRussian',
  56. 'uploader_id': '29790',
  57. 'timestamp': 1381943602,
  58. 'upload_date': '20131016',
  59. 'age_limit': 0,
  60. },
  61. }, {
  62. 'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661',
  63. 'only_matching': True,
  64. }, {
  65. 'url': 'http://rutube.ru/embed/a10e53b86e8f349080f718582ce4c661',
  66. 'only_matching': True,
  67. }, {
  68. 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/?pl_id=4252',
  69. 'only_matching': True,
  70. }, {
  71. 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_type=source',
  72. 'only_matching': True,
  73. }]
  74. @classmethod
  75. def suitable(cls, url):
  76. return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url)
  77. @staticmethod
  78. def _extract_urls(webpage):
  79. return [mobj.group('url') for mobj in re.finditer(
  80. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/embed/[\da-z]{32}.*?)\1',
  81. webpage)]
  82. def _real_extract(self, url):
  83. video_id = self._match_id(url)
  84. video = self._download_json(
  85. 'http://rutube.ru/api/video/%s/?format=json' % video_id,
  86. video_id, 'Downloading video JSON')
  87. info = self._extract_video(video, video_id)
  88. options = self._download_json(
  89. 'http://rutube.ru/api/play/options/%s/?format=json' % video_id,
  90. video_id, 'Downloading options JSON')
  91. formats = []
  92. for format_id, format_url in options['video_balancer'].items():
  93. ext = determine_ext(format_url)
  94. if ext == 'm3u8':
  95. formats.extend(self._extract_m3u8_formats(
  96. format_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  97. elif ext == 'f4m':
  98. formats.extend(self._extract_f4m_formats(
  99. format_url, video_id, f4m_id=format_id, fatal=False))
  100. else:
  101. formats.append({
  102. 'url': format_url,
  103. 'format_id': format_id,
  104. })
  105. self._sort_formats(formats)
  106. info['formats'] = formats
  107. return info
  108. class RutubeEmbedIE(InfoExtractor):
  109. IE_NAME = 'rutube:embed'
  110. IE_DESC = 'Rutube embedded videos'
  111. _VALID_URL = r'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)'
  112. _TESTS = [{
  113. 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=',
  114. 'info_dict': {
  115. 'id': 'a10e53b86e8f349080f718582ce4c661',
  116. 'ext': 'flv',
  117. 'timestamp': 1387830582,
  118. 'upload_date': '20131223',
  119. 'uploader_id': '297833',
  120. 'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89',
  121. 'uploader': 'subziro89 ILya',
  122. 'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89',
  123. },
  124. 'params': {
  125. 'skip_download': True,
  126. },
  127. }, {
  128. 'url': 'http://rutube.ru/play/embed/8083783',
  129. 'only_matching': True,
  130. }]
  131. def _real_extract(self, url):
  132. embed_id = self._match_id(url)
  133. webpage = self._download_webpage(url, embed_id)
  134. canonical_url = self._html_search_regex(
  135. r'<link\s+rel="canonical"\s+href="([^"]+?)"', webpage,
  136. 'Canonical URL')
  137. return self.url_result(canonical_url, RutubeIE.ie_key())
  138. class RutubePlaylistBaseIE(RutubeBaseIE):
  139. def _next_page_url(self, page_num, playlist_id, *args, **kwargs):
  140. return self._PAGE_TEMPLATE % (playlist_id, page_num)
  141. def _entries(self, playlist_id, *args, **kwargs):
  142. next_page_url = None
  143. for pagenum in itertools.count(1):
  144. page = self._download_json(
  145. next_page_url or self._next_page_url(
  146. pagenum, playlist_id, *args, **kwargs),
  147. playlist_id, 'Downloading page %s' % pagenum)
  148. results = page.get('results')
  149. if not results or not isinstance(results, list):
  150. break
  151. for result in results:
  152. video_url = url_or_none(result.get('video_url'))
  153. if not video_url:
  154. continue
  155. entry = self._extract_video(result, require_title=False)
  156. entry.update({
  157. '_type': 'url',
  158. 'url': video_url,
  159. 'ie_key': RutubeIE.ie_key(),
  160. })
  161. yield entry
  162. next_page_url = page.get('next')
  163. if not next_page_url or not page.get('has_next'):
  164. break
  165. def _extract_playlist(self, playlist_id, *args, **kwargs):
  166. return self.playlist_result(
  167. self._entries(playlist_id, *args, **kwargs),
  168. playlist_id, kwargs.get('playlist_name'))
  169. def _real_extract(self, url):
  170. return self._extract_playlist(self._match_id(url))
  171. class RutubeChannelIE(RutubePlaylistBaseIE):
  172. IE_NAME = 'rutube:channel'
  173. IE_DESC = 'Rutube channels'
  174. _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)'
  175. _TESTS = [{
  176. 'url': 'http://rutube.ru/tags/video/1800/',
  177. 'info_dict': {
  178. 'id': '1800',
  179. },
  180. 'playlist_mincount': 68,
  181. }]
  182. _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
  183. class RutubeMovieIE(RutubePlaylistBaseIE):
  184. IE_NAME = 'rutube:movie'
  185. IE_DESC = 'Rutube movies'
  186. _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)'
  187. _TESTS = []
  188. _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
  189. _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
  190. def _real_extract(self, url):
  191. movie_id = self._match_id(url)
  192. movie = self._download_json(
  193. self._MOVIE_TEMPLATE % movie_id, movie_id,
  194. 'Downloading movie JSON')
  195. return self._extract_playlist(
  196. movie_id, playlist_name=movie.get('name'))
  197. class RutubePersonIE(RutubePlaylistBaseIE):
  198. IE_NAME = 'rutube:person'
  199. IE_DESC = 'Rutube person videos'
  200. _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)'
  201. _TESTS = [{
  202. 'url': 'http://rutube.ru/video/person/313878/',
  203. 'info_dict': {
  204. 'id': '313878',
  205. },
  206. 'playlist_mincount': 37,
  207. }]
  208. _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'
  209. class RutubePlaylistIE(RutubePlaylistBaseIE):
  210. IE_NAME = 'rutube:playlist'
  211. IE_DESC = 'Rutube playlists'
  212. _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/[\da-z]{32}/\?.*?\bpl_id=(?P<id>\d+)'
  213. _TESTS = [{
  214. 'url': 'https://rutube.ru/video/cecd58ed7d531fc0f3d795d51cee9026/?pl_id=3097&pl_type=tag',
  215. 'info_dict': {
  216. 'id': '3097',
  217. },
  218. 'playlist_count': 27,
  219. }, {
  220. 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_id=4252&pl_type=source',
  221. 'only_matching': True,
  222. }]
  223. _PAGE_TEMPLATE = 'http://rutube.ru/api/playlist/%s/%s/?page=%s&format=json'
  224. @classmethod
  225. def suitable(cls, url):
  226. if not super(RutubePlaylistIE, cls).suitable(url):
  227. return False
  228. params = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  229. return params.get('pl_type', [None])[0] and int_or_none(params.get('pl_id', [None])[0])
  230. def _next_page_url(self, page_num, playlist_id, item_kind):
  231. return self._PAGE_TEMPLATE % (item_kind, playlist_id, page_num)
  232. def _real_extract(self, url):
  233. qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  234. playlist_kind = qs['pl_type'][0]
  235. playlist_id = qs['pl_id'][0]
  236. return self._extract_playlist(playlist_id, item_kind=playlist_kind)