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.

153 lines
6.6 KiB

11 years ago
11 years ago
  1. # encoding: utf-8
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urllib_request,
  7. ExtractorError,
  8. )
  9. class IviIE(InfoExtractor):
  10. IE_DESC = u'ivi.ru'
  11. IE_NAME = u'ivi'
  12. _VALID_URL = r'^https?://(?:www\.)?ivi\.ru/watch(?:/(?P<compilationid>[^/]+))?/(?P<videoid>\d+)'
  13. _TESTS = [
  14. # Single movie
  15. {
  16. u'url': u'http://www.ivi.ru/watch/53141',
  17. u'file': u'53141.mp4',
  18. u'md5': u'6ff5be2254e796ed346251d117196cf4',
  19. u'info_dict': {
  20. u'title': u'Иван Васильевич меняет профессию',
  21. u'description': u'md5:14d8eda24e9d93d29b5857012c6d6346',
  22. u'duration': 5498,
  23. u'thumbnail': u'http://thumbs.ivi.ru/f20.vcp.digitalaccess.ru/contents/d/1/c3c885163a082c29bceeb7b5a267a6.jpg',
  24. },
  25. u'skip': u'Only works from Russia',
  26. },
  27. # Serial's serie
  28. {
  29. u'url': u'http://www.ivi.ru/watch/dezhurnyi_angel/74791',
  30. u'file': u'74791.mp4',
  31. u'md5': u'3e6cc9a848c1d2ebcc6476444967baa9',
  32. u'info_dict': {
  33. u'title': u'Дежурный ангел - 1 серия',
  34. u'duration': 2490,
  35. u'thumbnail': u'http://thumbs.ivi.ru/f7.vcp.digitalaccess.ru/contents/8/e/bc2f6c2b6e5d291152fdd32c059141.jpg',
  36. },
  37. u'skip': u'Only works from Russia',
  38. }
  39. ]
  40. # Sorted by quality
  41. _known_formats = ['MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi', 'MP4-SHQ']
  42. # Sorted by size
  43. _known_thumbnails = ['Thumb-120x90', 'Thumb-160', 'Thumb-640x480']
  44. def _extract_description(self, html):
  45. m = re.search(r'<meta name="description" content="(?P<description>[^"]+)"/>', html)
  46. return m.group('description') if m is not None else None
  47. def _extract_comment_count(self, html):
  48. m = re.search(u'(?s)<a href="#" id="view-comments" class="action-button dim gradient">\s*Комментарии:\s*(?P<commentcount>\d+)\s*</a>', html)
  49. return int(m.group('commentcount')) if m is not None else 0
  50. def _real_extract(self, url):
  51. mobj = re.match(self._VALID_URL, url)
  52. video_id = mobj.group('videoid')
  53. api_url = 'http://api.digitalaccess.ru/api/json/'
  54. data = {u'method': u'da.content.get',
  55. u'params': [video_id, {u'site': u's183',
  56. u'referrer': u'http://www.ivi.ru/watch/%s' % video_id,
  57. u'contentid': video_id
  58. }
  59. ]
  60. }
  61. request = compat_urllib_request.Request(api_url, json.dumps(data))
  62. video_json_page = self._download_webpage(request, video_id, u'Downloading video JSON')
  63. video_json = json.loads(video_json_page)
  64. if u'error' in video_json:
  65. error = video_json[u'error']
  66. if error[u'origin'] == u'NoRedisValidData':
  67. raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
  68. raise ExtractorError(u'Unable to download video %s: %s' % (video_id, error[u'message']), expected=True)
  69. result = video_json[u'result']
  70. formats = [{'url': x[u'url'],
  71. 'format_id': x[u'content_format']
  72. } for x in result[u'files'] if x[u'content_format'] in self._known_formats]
  73. formats.sort(key=lambda fmt: self._known_formats.index(fmt['format_id']))
  74. if len(formats) == 0:
  75. self._downloader.report_warning(u'No media links available for %s' % video_id)
  76. return
  77. duration = result[u'duration']
  78. compilation = result[u'compilation']
  79. title = result[u'title']
  80. title = '%s - %s' % (compilation, title) if compilation is not None else title
  81. previews = result[u'preview']
  82. previews.sort(key=lambda fmt: self._known_thumbnails.index(fmt['content_format']))
  83. thumbnail = previews[-1][u'url'] if len(previews) > 0 else None
  84. video_page = self._download_webpage(url, video_id, u'Downloading video page')
  85. description = self._extract_description(video_page)
  86. comment_count = self._extract_comment_count(video_page)
  87. return {
  88. 'id': video_id,
  89. 'title': title,
  90. 'thumbnail': thumbnail,
  91. 'description': description,
  92. 'duration': duration,
  93. 'comment_count': comment_count,
  94. 'formats': formats,
  95. }
  96. class IviCompilationIE(InfoExtractor):
  97. IE_DESC = u'ivi.ru compilations'
  98. IE_NAME = u'ivi:compilation'
  99. _VALID_URL = r'^https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
  100. def _extract_entries(self, html, compilation_id):
  101. return [self.url_result('http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), 'Ivi')
  102. for serie in re.findall(r'<strong><a href="/watch/%s/(\d+)">(?:[^<]+)</a></strong>' % compilation_id, html)]
  103. def _real_extract(self, url):
  104. mobj = re.match(self._VALID_URL, url)
  105. compilation_id = mobj.group('compilationid')
  106. season_id = mobj.group('seasonid')
  107. if season_id is not None: # Season link
  108. season_page = self._download_webpage(url, compilation_id, u'Downloading season %s web page' % season_id)
  109. playlist_id = '%s/season%s' % (compilation_id, season_id)
  110. playlist_title = self._html_search_meta(u'title', season_page, u'title')
  111. entries = self._extract_entries(season_page, compilation_id)
  112. else: # Compilation link
  113. compilation_page = self._download_webpage(url, compilation_id, u'Downloading compilation web page')
  114. playlist_id = compilation_id
  115. playlist_title = self._html_search_meta(u'title', compilation_page, u'title')
  116. seasons = re.findall(r'<a href="/watch/%s/season(\d+)">[^<]+</a>' % compilation_id, compilation_page)
  117. if len(seasons) == 0: # No seasons in this compilation
  118. entries = self._extract_entries(compilation_page, compilation_id)
  119. else:
  120. entries = []
  121. for season_id in seasons:
  122. season_page = self._download_webpage('http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
  123. compilation_id, u'Downloading season %s web page' % season_id)
  124. entries.extend(self._extract_entries(season_page, compilation_id))
  125. return self.playlist_result(entries, playlist_id, playlist_title)