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