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.

154 lines
5.8 KiB

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