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