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.

165 lines
6.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError,
  7. compat_str,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. )
  13. class EaglePlatformIE(InfoExtractor):
  14. _VALID_URL = r'''(?x)
  15. (?:
  16. eagleplatform:(?P<custom_host>[^/]+):|
  17. https?://(?P<host>.+?\.media\.eagleplatform\.com)/index/player\?.*\brecord_id=
  18. )
  19. (?P<id>\d+)
  20. '''
  21. _TESTS = [{
  22. # http://lenta.ru/news/2015/03/06/navalny/
  23. 'url': 'http://lentaru.media.eagleplatform.com/index/player?player=new&record_id=227304&player_template_id=5201',
  24. # Not checking MD5 as sometimes the direct HTTP link results in 404 and HLS is used
  25. 'info_dict': {
  26. 'id': '227304',
  27. 'ext': 'mp4',
  28. 'title': 'Навальный вышел на свободу',
  29. 'description': 'md5:d97861ac9ae77377f3f20eaf9d04b4f5',
  30. 'thumbnail': r're:^https?://.*\.jpg$',
  31. 'duration': 87,
  32. 'view_count': int,
  33. 'age_limit': 0,
  34. },
  35. }, {
  36. # http://muz-tv.ru/play/7129/
  37. # http://media.clipyou.ru/index/player?record_id=12820&width=730&height=415&autoplay=true
  38. 'url': 'eagleplatform:media.clipyou.ru:12820',
  39. 'md5': '358597369cf8ba56675c1df15e7af624',
  40. 'info_dict': {
  41. 'id': '12820',
  42. 'ext': 'mp4',
  43. 'title': "'O Sole Mio",
  44. 'thumbnail': r're:^https?://.*\.jpg$',
  45. 'duration': 216,
  46. 'view_count': int,
  47. },
  48. 'skip': 'Georestricted',
  49. }]
  50. @staticmethod
  51. def _extract_url(webpage):
  52. # Regular iframe embedding
  53. mobj = re.search(
  54. r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//.+?\.media\.eagleplatform\.com/index/player\?.+?)\1',
  55. webpage)
  56. if mobj is not None:
  57. return mobj.group('url')
  58. # Basic usage embedding (see http://dultonmedia.github.io/eplayer/)
  59. mobj = re.search(
  60. r'''(?xs)
  61. <script[^>]+
  62. src=(?P<q1>["\'])(?:https?:)?//(?P<host>.+?\.media\.eagleplatform\.com)/player/player\.js(?P=q1)
  63. .+?
  64. <div[^>]+
  65. class=(?P<q2>["\'])eagleplayer(?P=q2)[^>]+
  66. data-id=["\'](?P<id>\d+)
  67. ''', webpage)
  68. if mobj is not None:
  69. return 'eagleplatform:%(host)s:%(id)s' % mobj.groupdict()
  70. @staticmethod
  71. def _handle_error(response):
  72. status = int_or_none(response.get('status', 200))
  73. if status != 200:
  74. raise ExtractorError(' '.join(response['errors']), expected=True)
  75. def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata', *args, **kwargs):
  76. try:
  77. response = super(EaglePlatformIE, self)._download_json(url_or_request, video_id, note)
  78. except ExtractorError as ee:
  79. if isinstance(ee.cause, compat_HTTPError):
  80. response = self._parse_json(ee.cause.read().decode('utf-8'), video_id)
  81. self._handle_error(response)
  82. raise
  83. return response
  84. def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
  85. return self._download_json(url_or_request, video_id, note)['data'][0]
  86. def _real_extract(self, url):
  87. mobj = re.match(self._VALID_URL, url)
  88. host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
  89. player_data = self._download_json(
  90. 'http://%s/api/player_data?id=%s' % (host, video_id), video_id)
  91. media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
  92. title = media['title']
  93. description = media.get('description')
  94. thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
  95. duration = int_or_none(media.get('duration'))
  96. view_count = int_or_none(media.get('views'))
  97. age_restriction = media.get('age_restriction')
  98. age_limit = None
  99. if age_restriction:
  100. age_limit = 0 if age_restriction == 'allow_all' else 18
  101. secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
  102. formats = []
  103. m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
  104. m3u8_formats = self._extract_m3u8_formats(
  105. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  106. m3u8_id='hls', fatal=False)
  107. formats.extend(m3u8_formats)
  108. m3u8_formats_dict = {}
  109. for f in m3u8_formats:
  110. if f.get('height') is not None:
  111. m3u8_formats_dict[f['height']] = f
  112. mp4_data = self._download_json(
  113. # Secure mp4 URL is constructed according to Player.prototype.mp4 from
  114. # http://lentaru.media.eagleplatform.com/player/player.js
  115. re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4s', secure_m3u8),
  116. video_id, 'Downloading mp4 JSON', fatal=False)
  117. if mp4_data:
  118. for format_id, format_url in mp4_data.get('data', {}).items():
  119. if not isinstance(format_url, compat_str):
  120. continue
  121. height = int_or_none(format_id)
  122. if height is not None and m3u8_formats_dict.get(height):
  123. f = m3u8_formats_dict[height].copy()
  124. f.update({
  125. 'format_id': f['format_id'].replace('hls', 'http'),
  126. 'protocol': 'http',
  127. })
  128. else:
  129. f = {
  130. 'format_id': 'http-%s' % format_id,
  131. 'height': int_or_none(format_id),
  132. }
  133. f['url'] = format_url
  134. formats.append(f)
  135. self._sort_formats(formats)
  136. return {
  137. 'id': video_id,
  138. 'title': title,
  139. 'description': description,
  140. 'thumbnail': thumbnail,
  141. 'duration': duration,
  142. 'view_count': view_count,
  143. 'age_limit': age_limit,
  144. 'formats': formats,
  145. }