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.

133 lines
5.0 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 _handle_error(response):
  50. status = int_or_none(response.get('status', 200))
  51. if status != 200:
  52. raise ExtractorError(' '.join(response['errors']), expected=True)
  53. def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
  54. try:
  55. response = super(EaglePlatformIE, self)._download_json(url_or_request, video_id, note)
  56. except ExtractorError as ee:
  57. if isinstance(ee.cause, compat_HTTPError):
  58. response = self._parse_json(ee.cause.read().decode('utf-8'), video_id)
  59. self._handle_error(response)
  60. raise
  61. return response
  62. def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
  63. return self._download_json(url_or_request, video_id, note)['data'][0]
  64. def _real_extract(self, url):
  65. mobj = re.match(self._VALID_URL, url)
  66. host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
  67. player_data = self._download_json(
  68. 'http://%s/api/player_data?id=%s' % (host, video_id), video_id)
  69. media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
  70. title = media['title']
  71. description = media.get('description')
  72. thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
  73. duration = int_or_none(media.get('duration'))
  74. view_count = int_or_none(media.get('views'))
  75. age_restriction = media.get('age_restriction')
  76. age_limit = None
  77. if age_restriction:
  78. age_limit = 0 if age_restriction == 'allow_all' else 18
  79. secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
  80. formats = []
  81. m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
  82. m3u8_formats = self._extract_m3u8_formats(
  83. m3u8_url, video_id,
  84. 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
  85. formats.extend(m3u8_formats)
  86. mp4_url = self._get_video_url(
  87. # Secure mp4 URL is constructed according to Player.prototype.mp4 from
  88. # http://lentaru.media.eagleplatform.com/player/player.js
  89. re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4', secure_m3u8),
  90. video_id, 'Downloading mp4 JSON')
  91. mp4_url_basename = url_basename(mp4_url)
  92. for m3u8_format in m3u8_formats:
  93. mobj = re.search('/([^/]+)/index\.m3u8', m3u8_format['url'])
  94. if mobj:
  95. http_format = m3u8_format.copy()
  96. video_url = mp4_url.replace(mp4_url_basename, mobj.group(1))
  97. if not self._is_valid_url(video_url, video_id):
  98. continue
  99. http_format.update({
  100. 'url': video_url,
  101. 'format_id': m3u8_format['format_id'].replace('hls', 'http'),
  102. 'protocol': 'http',
  103. })
  104. formats.append(http_format)
  105. self._sort_formats(formats)
  106. return {
  107. 'id': video_id,
  108. 'title': title,
  109. 'description': description,
  110. 'thumbnail': thumbnail,
  111. 'duration': duration,
  112. 'view_count': view_count,
  113. 'age_limit': age_limit,
  114. 'formats': formats,
  115. }