ivi.py 7.1 KB

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