]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dplay.py
Tidy up cleanup rule and keep lintian happy.
[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 compat_urlparse
10 from ..utils import (
11 int_or_none,
12 update_url_query,
13 )
14
15
16 class DPlayIE(InfoExtractor):
17 _VALID_URL = r'https?://(?P<domain>it\.dplay\.com|www\.dplay\.(?:dk|se|no))/[^/]+/(?P<id>[^/?#]+)'
18
19 _TESTS = [{
20 # geo restricted, via direct unsigned hls URL
21 'url': 'http://it.dplay.com/take-me-out/stagione-1-episodio-25/',
22 'info_dict': {
23 'id': '1255600',
24 'display_id': 'stagione-1-episodio-25',
25 'ext': 'mp4',
26 'title': 'Episodio 25',
27 'description': 'md5:cae5f40ad988811b197d2d27a53227eb',
28 'duration': 2761,
29 'timestamp': 1454701800,
30 'upload_date': '20160205',
31 'creator': 'RTIT',
32 'series': 'Take me out',
33 'season_number': 1,
34 'episode_number': 25,
35 'age_limit': 0,
36 },
37 'expected_warnings': ['Unable to download f4m manifest'],
38 }, {
39 # non geo restricted, via secure api, unsigned download hls URL
40 'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
41 'info_dict': {
42 'id': '3172',
43 'display_id': 'season-1-svensken-lar-sig-njuta-av-livet',
44 'ext': 'mp4',
45 'title': 'Svensken lär sig njuta av livet',
46 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
47 'duration': 2650,
48 'timestamp': 1365454320,
49 'upload_date': '20130408',
50 'creator': 'Kanal 5 (Home)',
51 'series': 'Nugammalt - 77 händelser som format Sverige',
52 'season_number': 1,
53 'episode_number': 1,
54 'age_limit': 0,
55 },
56 }, {
57 # geo restricted, via secure api, unsigned download hls URL
58 'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/',
59 'info_dict': {
60 'id': '70816',
61 'display_id': 'season-6-episode-12',
62 'ext': 'mp4',
63 'title': 'Episode 12',
64 'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90',
65 'duration': 2563,
66 'timestamp': 1429696800,
67 'upload_date': '20150422',
68 'creator': 'Kanal 4 (Home)',
69 'series': 'Mig og min mor',
70 'season_number': 6,
71 'episode_number': 12,
72 'age_limit': 0,
73 },
74 }, {
75 # geo restricted, via direct unsigned hls URL
76 'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/',
77 'only_matching': True,
78 }]
79
80 def _real_extract(self, url):
81 mobj = re.match(self._VALID_URL, url)
82 display_id = mobj.group('id')
83 domain = mobj.group('domain')
84
85 webpage = self._download_webpage(url, display_id)
86
87 video_id = self._search_regex(
88 r'data-video-id=["\'](\d+)', webpage, 'video id')
89
90 info = self._download_json(
91 'http://%s/api/v2/ajax/videos?video_id=%s' % (domain, video_id),
92 video_id)['data'][0]
93
94 title = info['title']
95
96 PROTOCOLS = ('hls', 'hds')
97 formats = []
98
99 def extract_formats(protocol, manifest_url):
100 if protocol == 'hls':
101 m3u8_formats = self._extract_m3u8_formats(
102 manifest_url, video_id, ext='mp4',
103 entry_protocol='m3u8_native', m3u8_id=protocol, fatal=False)
104 # Sometimes final URLs inside m3u8 are unsigned, let's fix this
105 # ourselves
106 query = compat_urlparse.parse_qs(compat_urlparse.urlparse(manifest_url).query)
107 for m3u8_format in m3u8_formats:
108 m3u8_format['url'] = update_url_query(m3u8_format['url'], query)
109 formats.extend(m3u8_formats)
110 elif protocol == 'hds':
111 formats.extend(self._extract_f4m_formats(
112 manifest_url + '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0',
113 video_id, f4m_id=protocol, fatal=False))
114
115 domain_tld = domain.split('.')[-1]
116 if domain_tld in ('se', 'dk', 'no'):
117 for protocol in PROTOCOLS:
118 # Providing dsc-geo allows to bypass geo restriction in some cases
119 self._set_cookie(
120 'secure.dplay.%s' % domain_tld, 'dsc-geo',
121 json.dumps({
122 'countryCode': domain_tld.upper(),
123 'expiry': (time.time() + 20 * 60) * 1000,
124 }))
125 stream = self._download_json(
126 'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s'
127 % (domain_tld, video_id, protocol), video_id,
128 'Downloading %s stream JSON' % protocol, fatal=False)
129 if stream and stream.get(protocol):
130 extract_formats(protocol, stream[protocol])
131
132 # The last resort is to try direct unsigned hls/hds URLs from info dictionary.
133 # Sometimes this does work even when secure API with dsc-geo has failed (e.g.
134 # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/).
135 if not formats:
136 for protocol in PROTOCOLS:
137 if info.get(protocol):
138 extract_formats(protocol, info[protocol])
139
140 self._sort_formats(formats)
141
142 subtitles = {}
143 for lang in ('se', 'sv', 'da', 'nl', 'no'):
144 for format_id in ('web_vtt', 'vtt', 'srt'):
145 subtitle_url = info.get('subtitles_%s_%s' % (lang, format_id))
146 if subtitle_url:
147 subtitles.setdefault(lang, []).append({'url': subtitle_url})
148
149 return {
150 'id': video_id,
151 'display_id': display_id,
152 'title': title,
153 'description': info.get('video_metadata_longDescription'),
154 'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
155 'timestamp': int_or_none(info.get('video_publish_date')),
156 'creator': info.get('video_metadata_homeChannel'),
157 'series': info.get('video_metadata_show'),
158 'season_number': int_or_none(info.get('season')),
159 'episode_number': int_or_none(info.get('episode')),
160 'age_limit': int_or_none(info.get('minimum_age')),
161 'formats': formats,
162 'subtitles': subtitles,
163 }