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.

155 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 = [{
  71. 'url': x[u'url'],
  72. 'format_id': x[u'content_format'],
  73. 'preference': self._known_formats.index(x[u'content_format']),
  74. } for x in result[u'files'] if x[u'content_format'] in self._known_formats]
  75. self._sort_formats(formats)
  76. if not formats:
  77. raise ExtractorError(u'No media links available for %s' % video_id)
  78. duration = result[u'duration']
  79. compilation = result[u'compilation']
  80. title = result[u'title']
  81. title = '%s - %s' % (compilation, title) if compilation is not None else title
  82. previews = result[u'preview']
  83. previews.sort(key=lambda fmt: self._known_thumbnails.index(fmt['content_format']))
  84. thumbnail = previews[-1][u'url'] if len(previews) > 0 else None
  85. video_page = self._download_webpage(url, video_id, u'Downloading video page')
  86. description = self._extract_description(video_page)
  87. comment_count = self._extract_comment_count(video_page)
  88. return {
  89. 'id': video_id,
  90. 'title': title,
  91. 'thumbnail': thumbnail,
  92. 'description': description,
  93. 'duration': duration,
  94. 'comment_count': comment_count,
  95. 'formats': formats,
  96. }
  97. class IviCompilationIE(InfoExtractor):
  98. IE_DESC = u'ivi.ru compilations'
  99. IE_NAME = u'ivi:compilation'
  100. _VALID_URL = r'^https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
  101. def _extract_entries(self, html, compilation_id):
  102. return [self.url_result('http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), 'Ivi')
  103. for serie in re.findall(r'<strong><a href="/watch/%s/(\d+)">(?:[^<]+)</a></strong>' % compilation_id, html)]
  104. def _real_extract(self, url):
  105. mobj = re.match(self._VALID_URL, url)
  106. compilation_id = mobj.group('compilationid')
  107. season_id = mobj.group('seasonid')
  108. if season_id is not None: # Season link
  109. season_page = self._download_webpage(url, compilation_id, u'Downloading season %s web page' % season_id)
  110. playlist_id = '%s/season%s' % (compilation_id, season_id)
  111. playlist_title = self._html_search_meta(u'title', season_page, u'title')
  112. entries = self._extract_entries(season_page, compilation_id)
  113. else: # Compilation link
  114. compilation_page = self._download_webpage(url, compilation_id, u'Downloading compilation web page')
  115. playlist_id = compilation_id
  116. playlist_title = self._html_search_meta(u'title', compilation_page, u'title')
  117. seasons = re.findall(r'<a href="/watch/%s/season(\d+)">[^<]+</a>' % compilation_id, compilation_page)
  118. if len(seasons) == 0: # No seasons in this compilation
  119. entries = self._extract_entries(compilation_page, compilation_id)
  120. else:
  121. entries = []
  122. for season_id in seasons:
  123. season_page = self._download_webpage('http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
  124. compilation_id, u'Downloading season %s web page' % season_id)
  125. entries.extend(self._extract_entries(season_page, compilation_id))
  126. return self.playlist_result(entries, playlist_id, playlist_title)