eagleplatform.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. PLAYER_JS_RE = r'''
  59. <script[^>]+
  60. src=(?P<qjs>["\'])(?:https?:)?//(?P<host>(?:(?!(?P=qjs)).)+\.media\.eagleplatform\.com)/player/player\.js(?P=qjs)
  61. .+?
  62. '''
  63. # "Basic usage" embedding (see http://dultonmedia.github.io/eplayer/)
  64. mobj = re.search(
  65. r'''(?xs)
  66. %s
  67. <div[^>]+
  68. class=(?P<qclass>["\'])eagleplayer(?P=qclass)[^>]+
  69. data-id=["\'](?P<id>\d+)
  70. ''' % PLAYER_JS_RE, webpage)
  71. if mobj is not None:
  72. return 'eagleplatform:%(host)s:%(id)s' % mobj.groupdict()
  73. # Generalization of "Javascript code usage", "Combined usage" and
  74. # "Usage without attaching to DOM" embeddings (see
  75. # http://dultonmedia.github.io/eplayer/)
  76. mobj = re.search(
  77. r'''(?xs)
  78. %s
  79. <script>
  80. .+?
  81. new\s+EaglePlayer\(
  82. (?:[^,]+\s*,\s*)?
  83. {
  84. .+?
  85. \bid\s*:\s*["\']?(?P<id>\d+)
  86. .+?
  87. }
  88. \s*\)
  89. .+?
  90. </script>
  91. ''' % PLAYER_JS_RE, webpage)
  92. if mobj is not None:
  93. return 'eagleplatform:%(host)s:%(id)s' % mobj.groupdict()
  94. @staticmethod
  95. def _handle_error(response):
  96. status = int_or_none(response.get('status', 200))
  97. if status != 200:
  98. raise ExtractorError(' '.join(response['errors']), expected=True)
  99. def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata', *args, **kwargs):
  100. try:
  101. response = super(EaglePlatformIE, self)._download_json(url_or_request, video_id, note)
  102. except ExtractorError as ee:
  103. if isinstance(ee.cause, compat_HTTPError):
  104. response = self._parse_json(ee.cause.read().decode('utf-8'), video_id)
  105. self._handle_error(response)
  106. raise
  107. return response
  108. def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
  109. return self._download_json(url_or_request, video_id, note)['data'][0]
  110. def _real_extract(self, url):
  111. mobj = re.match(self._VALID_URL, url)
  112. host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
  113. player_data = self._download_json(
  114. 'http://%s/api/player_data?id=%s' % (host, video_id), video_id)
  115. media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
  116. title = media['title']
  117. description = media.get('description')
  118. thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
  119. duration = int_or_none(media.get('duration'))
  120. view_count = int_or_none(media.get('views'))
  121. age_restriction = media.get('age_restriction')
  122. age_limit = None
  123. if age_restriction:
  124. age_limit = 0 if age_restriction == 'allow_all' else 18
  125. secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
  126. formats = []
  127. m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
  128. m3u8_formats = self._extract_m3u8_formats(
  129. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  130. m3u8_id='hls', fatal=False)
  131. formats.extend(m3u8_formats)
  132. m3u8_formats_dict = {}
  133. for f in m3u8_formats:
  134. if f.get('height') is not None:
  135. m3u8_formats_dict[f['height']] = f
  136. mp4_data = self._download_json(
  137. # Secure mp4 URL is constructed according to Player.prototype.mp4 from
  138. # http://lentaru.media.eagleplatform.com/player/player.js
  139. re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4s', secure_m3u8),
  140. video_id, 'Downloading mp4 JSON', fatal=False)
  141. if mp4_data:
  142. for format_id, format_url in mp4_data.get('data', {}).items():
  143. if not isinstance(format_url, compat_str):
  144. continue
  145. height = int_or_none(format_id)
  146. if height is not None and m3u8_formats_dict.get(height):
  147. f = m3u8_formats_dict[height].copy()
  148. f.update({
  149. 'format_id': f['format_id'].replace('hls', 'http'),
  150. 'protocol': 'http',
  151. })
  152. else:
  153. f = {
  154. 'format_id': 'http-%s' % format_id,
  155. 'height': int_or_none(format_id),
  156. }
  157. f['url'] = format_url
  158. formats.append(f)
  159. self._sort_formats(formats)
  160. return {
  161. 'id': video_id,
  162. 'title': title,
  163. 'description': description,
  164. 'thumbnail': thumbnail,
  165. 'duration': duration,
  166. 'view_count': view_count,
  167. 'age_limit': age_limit,
  168. 'formats': formats,
  169. }