logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

eagleplatform.py (7736B)


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