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.

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