eagleplatform.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import functools
  2. import re
  3. from .common import InfoExtractor
  4. from ..networking.exceptions import HTTPError
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. smuggle_url,
  9. unsmuggle_url,
  10. url_or_none,
  11. )
  12. class EaglePlatformIE(InfoExtractor):
  13. _VALID_URL = r'''(?x)
  14. (?:
  15. eagleplatform:(?P<custom_host>[^/]+):|
  16. https?://(?P<host>.+?\.media\.eagleplatform\.com)/index/player\?.*\brecord_id=
  17. )
  18. (?P<id>\d+)
  19. '''
  20. _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//.+?\.media\.eagleplatform\.com/index/player\?.+?)\1']
  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. # referrer protected video (https://tvrain.ru/lite/teleshow/kak_vse_nachinalos/namin-418921/)
  51. 'url': 'eagleplatform:tvrainru.media.eagleplatform.com:582306',
  52. 'only_matching': True,
  53. }]
  54. @classmethod
  55. def _extract_embed_urls(cls, url, webpage):
  56. add_referer = functools.partial(smuggle_url, data={'referrer': url})
  57. res = tuple(super()._extract_embed_urls(url, webpage))
  58. if res:
  59. return map(add_referer, res)
  60. PLAYER_JS_RE = r'''
  61. <script[^>]+
  62. src=(?P<qjs>["\'])(?:https?:)?//(?P<host>(?:(?!(?P=qjs)).)+\.media\.eagleplatform\.com)/player/player\.js(?P=qjs)
  63. .+?
  64. '''
  65. # "Basic usage" embedding (see http://dultonmedia.github.io/eplayer/)
  66. mobj = re.search(
  67. rf'''(?xs)
  68. {PLAYER_JS_RE}
  69. <div[^>]+
  70. class=(?P<qclass>["\'])eagleplayer(?P=qclass)[^>]+
  71. data-id=["\'](?P<id>\d+)
  72. ''', webpage)
  73. if mobj is not None:
  74. return [add_referer('eagleplatform:{host}:{id}'.format(**mobj.groupdict()))]
  75. # Generalization of "Javascript code usage", "Combined usage" and
  76. # "Usage without attaching to DOM" embeddings (see
  77. # http://dultonmedia.github.io/eplayer/)
  78. mobj = re.search(
  79. r'''(?xs)
  80. %s
  81. <script>
  82. .+?
  83. new\s+EaglePlayer\(
  84. (?:[^,]+\s*,\s*)?
  85. {
  86. .+?
  87. \bid\s*:\s*["\']?(?P<id>\d+)
  88. .+?
  89. }
  90. \s*\)
  91. .+?
  92. </script>
  93. ''' % PLAYER_JS_RE, webpage) # noqa: UP031
  94. if mobj is not None:
  95. return [add_referer('eagleplatform:{host}:{id}'.format(**mobj.groupdict()))]
  96. @staticmethod
  97. def _handle_error(response):
  98. status = int_or_none(response.get('status', 200))
  99. if status != 200:
  100. raise ExtractorError(' '.join(response['errors']), expected=True)
  101. def _download_json(self, url_or_request, video_id, *args, **kwargs):
  102. try:
  103. response = super()._download_json(
  104. url_or_request, video_id, *args, **kwargs)
  105. except ExtractorError as ee:
  106. if isinstance(ee.cause, HTTPError):
  107. response = self._parse_json(ee.cause.response.read().decode('utf-8'), video_id)
  108. self._handle_error(response)
  109. raise
  110. return response
  111. def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
  112. return self._download_json(url_or_request, video_id, note)['data'][0]
  113. def _real_extract(self, url):
  114. url, smuggled_data = unsmuggle_url(url, {})
  115. mobj = self._match_valid_url(url)
  116. host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
  117. headers = {}
  118. query = {
  119. 'id': video_id,
  120. }
  121. referrer = smuggled_data.get('referrer')
  122. if referrer:
  123. headers['Referer'] = referrer
  124. query['referrer'] = referrer
  125. player_data = self._download_json(
  126. f'http://{host}/api/player_data', video_id,
  127. headers=headers, query=query)
  128. media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
  129. title = media['title']
  130. description = media.get('description')
  131. thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
  132. duration = int_or_none(media.get('duration'))
  133. view_count = int_or_none(media.get('views'))
  134. age_restriction = media.get('age_restriction')
  135. age_limit = None
  136. if age_restriction:
  137. age_limit = 0 if age_restriction == 'allow_all' else 18
  138. secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
  139. formats = []
  140. m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
  141. m3u8_formats = self._extract_m3u8_formats(
  142. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  143. m3u8_id='hls', fatal=False)
  144. formats.extend(m3u8_formats)
  145. m3u8_formats_dict = {}
  146. for f in m3u8_formats:
  147. if f.get('height') is not None:
  148. m3u8_formats_dict[f['height']] = f
  149. mp4_data = self._download_json(
  150. # Secure mp4 URL is constructed according to Player.prototype.mp4 from
  151. # http://lentaru.media.eagleplatform.com/player/player.js
  152. re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4s', secure_m3u8),
  153. video_id, 'Downloading mp4 JSON', fatal=False)
  154. if mp4_data:
  155. for format_id, format_url in mp4_data.get('data', {}).items():
  156. if not url_or_none(format_url):
  157. continue
  158. height = int_or_none(format_id)
  159. if height is not None and m3u8_formats_dict.get(height):
  160. f = m3u8_formats_dict[height].copy()
  161. f.update({
  162. 'format_id': f['format_id'].replace('hls', 'http'),
  163. 'protocol': 'http',
  164. })
  165. else:
  166. f = {
  167. 'format_id': f'http-{format_id}',
  168. 'height': int_or_none(format_id),
  169. }
  170. f['url'] = format_url
  171. formats.append(f)
  172. return {
  173. 'id': video_id,
  174. 'title': title,
  175. 'description': description,
  176. 'thumbnail': thumbnail,
  177. 'duration': duration,
  178. 'view_count': view_count,
  179. 'age_limit': age_limit,
  180. 'formats': formats,
  181. }
  182. class ClipYouEmbedIE(InfoExtractor):
  183. _VALID_URL = False
  184. @classmethod
  185. def _extract_embed_urls(cls, url, webpage):
  186. mobj = re.search(
  187. r'<iframe[^>]+src="https?://(?P<host>media\.clipyou\.ru)/index/player\?.*\brecord_id=(?P<id>\d+).*"', webpage)
  188. if mobj is not None:
  189. yield smuggle_url('eagleplatform:{host}:{id}'.format(**mobj.groupdict()), {'referrer': url})