]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/eagleplatform.py
debian/control: Mark compliance with policy 4.0.1.
[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 unsmuggle_url,
15 )
16
17
18 class EaglePlatformIE(InfoExtractor):
19 _VALID_URL = r'''(?x)
20 (?:
21 eagleplatform:(?P<custom_host>[^/]+):|
22 https?://(?P<host>.+?\.media\.eagleplatform\.com)/index/player\?.*\brecord_id=
23 )
24 (?P<id>\d+)
25 '''
26 _TESTS = [{
27 # http://lenta.ru/news/2015/03/06/navalny/
28 'url': 'http://lentaru.media.eagleplatform.com/index/player?player=new&record_id=227304&player_template_id=5201',
29 # Not checking MD5 as sometimes the direct HTTP link results in 404 and HLS is used
30 'info_dict': {
31 'id': '227304',
32 'ext': 'mp4',
33 'title': 'Навальный вышел на свободу',
34 'description': 'md5:d97861ac9ae77377f3f20eaf9d04b4f5',
35 'thumbnail': r're:^https?://.*\.jpg$',
36 'duration': 87,
37 'view_count': int,
38 'age_limit': 0,
39 },
40 }, {
41 # http://muz-tv.ru/play/7129/
42 # http://media.clipyou.ru/index/player?record_id=12820&width=730&height=415&autoplay=true
43 'url': 'eagleplatform:media.clipyou.ru:12820',
44 'md5': '358597369cf8ba56675c1df15e7af624',
45 'info_dict': {
46 'id': '12820',
47 'ext': 'mp4',
48 'title': "'O Sole Mio",
49 'thumbnail': r're:^https?://.*\.jpg$',
50 'duration': 216,
51 'view_count': int,
52 },
53 'skip': 'Georestricted',
54 }, {
55 # referrer protected video (https://tvrain.ru/lite/teleshow/kak_vse_nachinalos/namin-418921/)
56 'url': 'eagleplatform:tvrainru.media.eagleplatform.com:582306',
57 'only_matching': True,
58 }]
59
60 @staticmethod
61 def _extract_url(webpage):
62 # Regular iframe embedding
63 mobj = re.search(
64 r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//.+?\.media\.eagleplatform\.com/index/player\?.+?)\1',
65 webpage)
66 if mobj is not None:
67 return mobj.group('url')
68 PLAYER_JS_RE = r'''
69 <script[^>]+
70 src=(?P<qjs>["\'])(?:https?:)?//(?P<host>(?:(?!(?P=qjs)).)+\.media\.eagleplatform\.com)/player/player\.js(?P=qjs)
71 .+?
72 '''
73 # "Basic usage" embedding (see http://dultonmedia.github.io/eplayer/)
74 mobj = re.search(
75 r'''(?xs)
76 %s
77 <div[^>]+
78 class=(?P<qclass>["\'])eagleplayer(?P=qclass)[^>]+
79 data-id=["\'](?P<id>\d+)
80 ''' % PLAYER_JS_RE, webpage)
81 if mobj is not None:
82 return 'eagleplatform:%(host)s:%(id)s' % mobj.groupdict()
83 # Generalization of "Javascript code usage", "Combined usage" and
84 # "Usage without attaching to DOM" embeddings (see
85 # http://dultonmedia.github.io/eplayer/)
86 mobj = re.search(
87 r'''(?xs)
88 %s
89 <script>
90 .+?
91 new\s+EaglePlayer\(
92 (?:[^,]+\s*,\s*)?
93 {
94 .+?
95 \bid\s*:\s*["\']?(?P<id>\d+)
96 .+?
97 }
98 \s*\)
99 .+?
100 </script>
101 ''' % PLAYER_JS_RE, webpage)
102 if mobj is not None:
103 return 'eagleplatform:%(host)s:%(id)s' % mobj.groupdict()
104
105 @staticmethod
106 def _handle_error(response):
107 status = int_or_none(response.get('status', 200))
108 if status != 200:
109 raise ExtractorError(' '.join(response['errors']), expected=True)
110
111 def _download_json(self, url_or_request, video_id, *args, **kwargs):
112 try:
113 response = super(EaglePlatformIE, self)._download_json(
114 url_or_request, video_id, *args, **kwargs)
115 except ExtractorError as ee:
116 if isinstance(ee.cause, compat_HTTPError):
117 response = self._parse_json(ee.cause.read().decode('utf-8'), video_id)
118 self._handle_error(response)
119 raise
120 return response
121
122 def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
123 return self._download_json(url_or_request, video_id, note)['data'][0]
124
125 def _real_extract(self, url):
126 url, smuggled_data = unsmuggle_url(url, {})
127
128 mobj = re.match(self._VALID_URL, url)
129 host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
130
131 headers = {}
132 query = {
133 'id': video_id,
134 }
135
136 referrer = smuggled_data.get('referrer')
137 if referrer:
138 headers['Referer'] = referrer
139 query['referrer'] = referrer
140
141 player_data = self._download_json(
142 'http://%s/api/player_data' % host, video_id,
143 headers=headers, query=query)
144
145 media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
146
147 title = media['title']
148 description = media.get('description')
149 thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
150 duration = int_or_none(media.get('duration'))
151 view_count = int_or_none(media.get('views'))
152
153 age_restriction = media.get('age_restriction')
154 age_limit = None
155 if age_restriction:
156 age_limit = 0 if age_restriction == 'allow_all' else 18
157
158 secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
159
160 formats = []
161
162 m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
163 m3u8_formats = self._extract_m3u8_formats(
164 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
165 m3u8_id='hls', fatal=False)
166 formats.extend(m3u8_formats)
167
168 m3u8_formats_dict = {}
169 for f in m3u8_formats:
170 if f.get('height') is not None:
171 m3u8_formats_dict[f['height']] = f
172
173 mp4_data = self._download_json(
174 # Secure mp4 URL is constructed according to Player.prototype.mp4 from
175 # http://lentaru.media.eagleplatform.com/player/player.js
176 re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4s', secure_m3u8),
177 video_id, 'Downloading mp4 JSON', fatal=False)
178 if mp4_data:
179 for format_id, format_url in mp4_data.get('data', {}).items():
180 if not isinstance(format_url, compat_str):
181 continue
182 height = int_or_none(format_id)
183 if height is not None and m3u8_formats_dict.get(height):
184 f = m3u8_formats_dict[height].copy()
185 f.update({
186 'format_id': f['format_id'].replace('hls', 'http'),
187 'protocol': 'http',
188 })
189 else:
190 f = {
191 'format_id': 'http-%s' % format_id,
192 'height': int_or_none(format_id),
193 }
194 f['url'] = format_url
195 formats.append(f)
196
197 self._sort_formats(formats)
198
199 return {
200 'id': video_id,
201 'title': title,
202 'description': description,
203 'thumbnail': thumbnail,
204 'duration': duration,
205 'view_count': view_count,
206 'age_limit': age_limit,
207 'formats': formats,
208 }