]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dplay.py
debian/README.source: Add my preference with respect to patches.
[youtubedl] / youtube_dl / extractor / dplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6 import time
7
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_HTTPError,
11 compat_str,
12 compat_urlparse,
13 )
14 from ..utils import (
15 ExtractorError,
16 int_or_none,
17 remove_end,
18 try_get,
19 unified_strdate,
20 update_url_query,
21 USER_AGENTS,
22 )
23
24
25 class DPlayIE(InfoExtractor):
26 _VALID_URL = r'https?://(?P<domain>www\.dplay\.(?:dk|se|no))/[^/]+/(?P<id>[^/?#]+)'
27
28 _TESTS = [{
29 # non geo restricted, via secure api, unsigned download hls URL
30 'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
31 'info_dict': {
32 'id': '3172',
33 'display_id': 'season-1-svensken-lar-sig-njuta-av-livet',
34 'ext': 'mp4',
35 'title': 'Svensken lär sig njuta av livet',
36 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
37 'duration': 2650,
38 'timestamp': 1365454320,
39 'upload_date': '20130408',
40 'creator': 'Kanal 5 (Home)',
41 'series': 'Nugammalt - 77 händelser som format Sverige',
42 'season_number': 1,
43 'episode_number': 1,
44 'age_limit': 0,
45 },
46 }, {
47 # geo restricted, via secure api, unsigned download hls URL
48 'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/',
49 'info_dict': {
50 'id': '70816',
51 'display_id': 'season-6-episode-12',
52 'ext': 'mp4',
53 'title': 'Episode 12',
54 'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90',
55 'duration': 2563,
56 'timestamp': 1429696800,
57 'upload_date': '20150422',
58 'creator': 'Kanal 4 (Home)',
59 'series': 'Mig og min mor',
60 'season_number': 6,
61 'episode_number': 12,
62 'age_limit': 0,
63 },
64 }, {
65 # geo restricted, via direct unsigned hls URL
66 'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/',
67 'only_matching': True,
68 }]
69
70 def _real_extract(self, url):
71 mobj = re.match(self._VALID_URL, url)
72 display_id = mobj.group('id')
73 domain = mobj.group('domain')
74
75 webpage = self._download_webpage(url, display_id)
76
77 video_id = self._search_regex(
78 r'data-video-id=["\'](\d+)', webpage, 'video id')
79
80 info = self._download_json(
81 'http://%s/api/v2/ajax/videos?video_id=%s' % (domain, video_id),
82 video_id)['data'][0]
83
84 title = info['title']
85
86 PROTOCOLS = ('hls', 'hds')
87 formats = []
88
89 def extract_formats(protocol, manifest_url):
90 if protocol == 'hls':
91 m3u8_formats = self._extract_m3u8_formats(
92 manifest_url, video_id, ext='mp4',
93 entry_protocol='m3u8_native', m3u8_id=protocol, fatal=False)
94 # Sometimes final URLs inside m3u8 are unsigned, let's fix this
95 # ourselves. Also fragments' URLs are only served signed for
96 # Safari user agent.
97 query = compat_urlparse.parse_qs(compat_urlparse.urlparse(manifest_url).query)
98 for m3u8_format in m3u8_formats:
99 m3u8_format.update({
100 'url': update_url_query(m3u8_format['url'], query),
101 'http_headers': {
102 'User-Agent': USER_AGENTS['Safari'],
103 },
104 })
105 formats.extend(m3u8_formats)
106 elif protocol == 'hds':
107 formats.extend(self._extract_f4m_formats(
108 manifest_url + '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0',
109 video_id, f4m_id=protocol, fatal=False))
110
111 domain_tld = domain.split('.')[-1]
112 if domain_tld in ('se', 'dk', 'no'):
113 for protocol in PROTOCOLS:
114 # Providing dsc-geo allows to bypass geo restriction in some cases
115 self._set_cookie(
116 'secure.dplay.%s' % domain_tld, 'dsc-geo',
117 json.dumps({
118 'countryCode': domain_tld.upper(),
119 'expiry': (time.time() + 20 * 60) * 1000,
120 }))
121 stream = self._download_json(
122 'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s'
123 % (domain_tld, video_id, protocol), video_id,
124 'Downloading %s stream JSON' % protocol, fatal=False)
125 if stream and stream.get(protocol):
126 extract_formats(protocol, stream[protocol])
127
128 # The last resort is to try direct unsigned hls/hds URLs from info dictionary.
129 # Sometimes this does work even when secure API with dsc-geo has failed (e.g.
130 # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/).
131 if not formats:
132 for protocol in PROTOCOLS:
133 if info.get(protocol):
134 extract_formats(protocol, info[protocol])
135
136 self._sort_formats(formats)
137
138 subtitles = {}
139 for lang in ('se', 'sv', 'da', 'nl', 'no'):
140 for format_id in ('web_vtt', 'vtt', 'srt'):
141 subtitle_url = info.get('subtitles_%s_%s' % (lang, format_id))
142 if subtitle_url:
143 subtitles.setdefault(lang, []).append({'url': subtitle_url})
144
145 return {
146 'id': video_id,
147 'display_id': display_id,
148 'title': title,
149 'description': info.get('video_metadata_longDescription'),
150 'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
151 'timestamp': int_or_none(info.get('video_publish_date')),
152 'creator': info.get('video_metadata_homeChannel'),
153 'series': info.get('video_metadata_show'),
154 'season_number': int_or_none(info.get('season')),
155 'episode_number': int_or_none(info.get('episode')),
156 'age_limit': int_or_none(info.get('minimum_age')),
157 'formats': formats,
158 'subtitles': subtitles,
159 }
160
161
162 class DPlayItIE(InfoExtractor):
163 _VALID_URL = r'https?://it\.dplay\.com/[^/]+/[^/]+/(?P<id>[^/?#]+)'
164 _GEO_COUNTRIES = ['IT']
165 _TEST = {
166 'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/',
167 'md5': '2b808ffb00fc47b884a172ca5d13053c',
168 'info_dict': {
169 'id': '6918',
170 'display_id': 'luigi-di-maio-la-psicosi-di-stanislawskij',
171 'ext': 'mp4',
172 'title': 'Biografie imbarazzanti: Luigi Di Maio: la psicosi di Stanislawskij',
173 'description': 'md5:3c7a4303aef85868f867a26f5cc14813',
174 'thumbnail': r're:^https?://.*\.jpe?g',
175 'upload_date': '20160524',
176 'series': 'Biografie imbarazzanti',
177 'season_number': 1,
178 'episode': 'Luigi Di Maio: la psicosi di Stanislawskij',
179 'episode_number': 1,
180 },
181 }
182
183 def _real_extract(self, url):
184 display_id = self._match_id(url)
185
186 webpage = self._download_webpage(url, display_id)
187
188 title = remove_end(self._og_search_title(webpage), ' | Dplay')
189
190 video_id = None
191
192 info = self._search_regex(
193 r'playback_json\s*:\s*JSON\.parse\s*\(\s*("(?:\\.|[^"\\])+?")',
194 webpage, 'playback JSON', default=None)
195 if info:
196 for _ in range(2):
197 info = self._parse_json(info, display_id, fatal=False)
198 if not info:
199 break
200 else:
201 video_id = try_get(info, lambda x: x['data']['id'])
202
203 if not info:
204 info_url = self._search_regex(
205 r'url\s*[:=]\s*["\']((?:https?:)?//[^/]+/playback/videoPlaybackInfo/\d+)',
206 webpage, 'info url')
207
208 video_id = info_url.rpartition('/')[-1]
209
210 try:
211 info = self._download_json(
212 info_url, display_id, headers={
213 'Authorization': 'Bearer %s' % self._get_cookies(url).get(
214 'dplayit_token').value,
215 'Referer': url,
216 })
217 except ExtractorError as e:
218 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (400, 403):
219 info = self._parse_json(e.cause.read().decode('utf-8'), display_id)
220 error = info['errors'][0]
221 if error.get('code') == 'access.denied.geoblocked':
222 self.raise_geo_restricted(
223 msg=error.get('detail'), countries=self._GEO_COUNTRIES)
224 raise ExtractorError(info['errors'][0]['detail'], expected=True)
225 raise
226
227 hls_url = info['data']['attributes']['streaming']['hls']['url']
228
229 formats = self._extract_m3u8_formats(
230 hls_url, display_id, ext='mp4', entry_protocol='m3u8_native',
231 m3u8_id='hls')
232
233 series = self._html_search_regex(
234 r'(?s)<h1[^>]+class=["\'].*?\bshow_title\b.*?["\'][^>]*>(.+?)</h1>',
235 webpage, 'series', fatal=False)
236 episode = self._search_regex(
237 r'<p[^>]+class=["\'].*?\bdesc_ep\b.*?["\'][^>]*>\s*<br/>\s*<b>([^<]+)',
238 webpage, 'episode', fatal=False)
239
240 mobj = re.search(
241 r'(?s)<span[^>]+class=["\']dates["\'][^>]*>.+?\bS\.(?P<season_number>\d+)\s+E\.(?P<episode_number>\d+)\s*-\s*(?P<upload_date>\d{2}/\d{2}/\d{4})',
242 webpage)
243 if mobj:
244 season_number = int(mobj.group('season_number'))
245 episode_number = int(mobj.group('episode_number'))
246 upload_date = unified_strdate(mobj.group('upload_date'))
247 else:
248 season_number = episode_number = upload_date = None
249
250 return {
251 'id': compat_str(video_id or display_id),
252 'display_id': display_id,
253 'title': title,
254 'description': self._og_search_description(webpage),
255 'thumbnail': self._og_search_thumbnail(webpage),
256 'series': series,
257 'season_number': season_number,
258 'episode': episode,
259 'episode_number': episode_number,
260 'upload_date': upload_date,
261 'formats': formats,
262 }