eagleplatform.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. )
  10. class EaglePlatformIE(InfoExtractor):
  11. _VALID_URL = r'''(?x)
  12. (?:
  13. eagleplatform:(?P<custom_host>[^/]+):|
  14. https?://(?P<host>.+?\.media\.eagleplatform\.com)/index/player\?.*\brecord_id=
  15. )
  16. (?P<id>\d+)
  17. '''
  18. _TESTS = [{
  19. # http://lenta.ru/news/2015/03/06/navalny/
  20. 'url': 'http://lentaru.media.eagleplatform.com/index/player?player=new&record_id=227304&player_template_id=5201',
  21. 'md5': '70f5187fb620f2c1d503b3b22fd4efe3',
  22. 'info_dict': {
  23. 'id': '227304',
  24. 'ext': 'mp4',
  25. 'title': 'Навальный вышел на свободу',
  26. 'description': 'md5:d97861ac9ae77377f3f20eaf9d04b4f5',
  27. 'thumbnail': 're:^https?://.*\.jpg$',
  28. 'duration': 87,
  29. 'view_count': int,
  30. 'age_limit': 0,
  31. },
  32. }, {
  33. # http://muz-tv.ru/play/7129/
  34. # http://media.clipyou.ru/index/player?record_id=12820&width=730&height=415&autoplay=true
  35. 'url': 'eagleplatform:media.clipyou.ru:12820',
  36. 'md5': '90b26344ba442c8e44aa4cf8f301164a',
  37. 'info_dict': {
  38. 'id': '12820',
  39. 'ext': 'mp4',
  40. 'title': "'O Sole Mio",
  41. 'thumbnail': 're:^https?://.*\.jpg$',
  42. 'duration': 216,
  43. 'view_count': int,
  44. },
  45. 'skip': 'Georestricted',
  46. }]
  47. @staticmethod
  48. def _handle_error(response):
  49. status = int_or_none(response.get('status', 200))
  50. if status != 200:
  51. raise ExtractorError(' '.join(response['errors']), expected=True)
  52. def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
  53. try:
  54. response = super(EaglePlatformIE, self)._download_json(url_or_request, video_id, note)
  55. except ExtractorError as ee:
  56. if isinstance(ee.cause, compat_HTTPError):
  57. response = self._parse_json(ee.cause.read().decode('utf-8'), video_id)
  58. self._handle_error(response)
  59. raise
  60. return response
  61. def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
  62. return self._download_json(url_or_request, video_id, note)['data'][0]
  63. def _real_extract(self, url):
  64. mobj = re.match(self._VALID_URL, url)
  65. host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
  66. player_data = self._download_json(
  67. 'http://%s/api/player_data?id=%s' % (host, video_id), video_id)
  68. media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
  69. title = media['title']
  70. description = media.get('description')
  71. thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
  72. duration = int_or_none(media.get('duration'))
  73. view_count = int_or_none(media.get('views'))
  74. age_restriction = media.get('age_restriction')
  75. age_limit = None
  76. if age_restriction:
  77. age_limit = 0 if age_restriction == 'allow_all' else 18
  78. secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
  79. m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
  80. formats = self._extract_m3u8_formats(
  81. m3u8_url, video_id,
  82. 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
  83. mp4_url = self._get_video_url(
  84. # Secure mp4 URL is constructed according to Player.prototype.mp4 from
  85. # http://lentaru.media.eagleplatform.com/player/player.js
  86. re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4', secure_m3u8),
  87. video_id, 'Downloading mp4 JSON')
  88. formats.append({'url': mp4_url, 'format_id': 'mp4'})
  89. self._sort_formats(formats)
  90. return {
  91. 'id': video_id,
  92. 'title': title,
  93. 'description': description,
  94. 'thumbnail': thumbnail,
  95. 'duration': duration,
  96. 'view_count': view_count,
  97. 'age_limit': age_limit,
  98. 'formats': formats,
  99. }