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.

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