ivi.py 6.8 KB

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