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.

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