Merge branch 'master' of github.com-rndusr:rg3/youtube-dl into fix/str-item-assignment
[youtube-dl] / youtube_dl / extractor / viu.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 compat_str
8 from ..utils import (
9     ExtractorError,
10     int_or_none,
11 )
12
13
14 class ViuBaseIE(InfoExtractor):
15     def _real_initialize(self):
16         viu_auth_res = self._request_webpage(
17             'https://www.viu.com/api/apps/v2/authenticate', None,
18             'Requesting Viu auth', query={
19                 'acct': 'test',
20                 'appid': 'viu_desktop',
21                 'fmt': 'json',
22                 'iid': 'guest',
23                 'languageid': 'default',
24                 'platform': 'desktop',
25                 'userid': 'guest',
26                 'useridtype': 'guest',
27                 'ver': '1.0'
28             }, headers=self.geo_verification_headers())
29         self._auth_token = viu_auth_res.info()['X-VIU-AUTH']
30
31     def _call_api(self, path, *args, **kwargs):
32         headers = self.geo_verification_headers()
33         headers.update({
34             'X-VIU-AUTH': self._auth_token
35         })
36         headers.update(kwargs.get('headers', {}))
37         kwargs['headers'] = headers
38         response = self._download_json(
39             'https://www.viu.com/api/' + path, *args, **kwargs)['response']
40         if response.get('status') != 'success':
41             raise ExtractorError('%s said: %s' % (
42                 self.IE_NAME, response['message']), expected=True)
43         return response
44
45
46 class ViuIE(ViuBaseIE):
47     _VALID_URL = r'(?:viu:|https?://[^/]+\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)'
48     _TESTS = [{
49         'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059',
50         'info_dict': {
51             'id': '1116705532',
52             'ext': 'mp4',
53             'title': 'Citizen Khan - Ep 1',
54             'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e',
55         },
56         'params': {
57             'skip_download': 'm3u8 download',
58         },
59         'skip': 'Geo-restricted to India',
60     }, {
61         'url': 'https://www.viu.com/en/media/1130599965',
62         'info_dict': {
63             'id': '1130599965',
64             'ext': 'mp4',
65             'title': 'Jealousy Incarnate - Episode 1',
66             'description': 'md5:d3d82375cab969415d2720b6894361e9',
67         },
68         'params': {
69             'skip_download': 'm3u8 download',
70         },
71         'skip': 'Geo-restricted to Indonesia',
72     }, {
73         'url': 'https://india.viu.com/en/media/1126286865',
74         'only_matching': True,
75     }]
76
77     def _real_extract(self, url):
78         video_id = self._match_id(url)
79
80         video_data = self._call_api(
81             'clip/load', video_id, 'Downloading video data', query={
82                 'appid': 'viu_desktop',
83                 'fmt': 'json',
84                 'id': video_id
85             })['item'][0]
86
87         title = video_data['title']
88
89         m3u8_url = None
90         url_path = video_data.get('urlpathd') or video_data.get('urlpath')
91         tdirforwhole = video_data.get('tdirforwhole')
92         # #EXT-X-BYTERANGE is not supported by native hls downloader
93         # and ffmpeg (#10955)
94         # hls_file = video_data.get('hlsfile')
95         hls_file = video_data.get('jwhlsfile')
96         if url_path and tdirforwhole and hls_file:
97             m3u8_url = '%s/%s/%s' % (url_path, tdirforwhole, hls_file)
98         else:
99             # m3u8_url = re.sub(
100             #     r'(/hlsc_)[a-z]+(\d+\.m3u8)',
101             #     r'\1whe\2', video_data['href'])
102             m3u8_url = video_data['href']
103         formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
104         self._sort_formats(formats)
105
106         subtitles = {}
107         for key, value in video_data.items():
108             mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key)
109             if not mobj:
110                 continue
111             subtitles.setdefault(mobj.group('lang'), []).append({
112                 'url': value,
113                 'ext': mobj.group('ext')
114             })
115
116         return {
117             'id': video_id,
118             'title': title,
119             'description': video_data.get('description'),
120             'series': video_data.get('moviealbumshowname'),
121             'episode': title,
122             'episode_number': int_or_none(video_data.get('episodeno')),
123             'duration': int_or_none(video_data.get('duration')),
124             'formats': formats,
125             'subtitles': subtitles,
126         }
127
128
129 class ViuPlaylistIE(ViuBaseIE):
130     IE_NAME = 'viu:playlist'
131     _VALID_URL = r'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
132     _TEST = {
133         'url': 'https://www.viu.com/en/listing/playlist-22461380',
134         'info_dict': {
135             'id': '22461380',
136             'title': 'The Good Wife',
137         },
138         'playlist_count': 16,
139         'skip': 'Geo-restricted to Indonesia',
140     }
141
142     def _real_extract(self, url):
143         playlist_id = self._match_id(url)
144         playlist_data = self._call_api(
145             'container/load', playlist_id,
146             'Downloading playlist info', query={
147                 'appid': 'viu_desktop',
148                 'fmt': 'json',
149                 'id': 'playlist-' + playlist_id
150             })['container']
151
152         entries = []
153         for item in playlist_data.get('item', []):
154             item_id = item.get('id')
155             if not item_id:
156                 continue
157             item_id = compat_str(item_id)
158             entries.append(self.url_result(
159                 'viu:' + item_id, 'Viu', item_id))
160
161         return self.playlist_result(
162             entries, playlist_id, playlist_data.get('title'))
163
164
165 class ViuOTTIE(InfoExtractor):
166     IE_NAME = 'viu:ott'
167     _VALID_URL = r'https?://(?:www\.)?viu\.com/ott/(?P<country_code>[a-z]{2})/[a-z]{2}-[a-z]{2}/vod/(?P<id>\d+)'
168     _TESTS = [{
169         'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
170         'info_dict': {
171             'id': '3421',
172             'ext': 'mp4',
173             'title': 'A New Beginning',
174             'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
175         },
176         'params': {
177             'skip_download': 'm3u8 download',
178         },
179         'skip': 'Geo-restricted to Singapore',
180     }, {
181         'url': 'http://www.viu.com/ott/hk/zh-hk/vod/7123/%E5%A4%A7%E4%BA%BA%E5%A5%B3%E5%AD%90',
182         'info_dict': {
183             'id': '7123',
184             'ext': 'mp4',
185             'title': '這就是我的生活之道',
186             'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f',
187         },
188         'params': {
189             'skip_download': 'm3u8 download',
190         },
191         'skip': 'Geo-restricted to Hong Kong',
192     }]
193
194     def _real_extract(self, url):
195         country_code, video_id = re.match(self._VALID_URL, url).groups()
196
197         product_data = self._download_json(
198             'http://www.viu.com/ott/%s/index.php' % country_code, video_id,
199             'Downloading video info', query={
200                 'r': 'vod/ajax-detail',
201                 'platform_flag_label': 'web',
202                 'product_id': video_id,
203             })['data']
204
205         video_data = product_data.get('current_product')
206         if not video_data:
207             raise ExtractorError('This video is not available in your region.', expected=True)
208
209         stream_data = self._download_json(
210             'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code,
211             video_id, 'Downloading stream info', query={
212                 'ccs_product_id': video_data['ccs_product_id'],
213             })['data']['stream']
214
215         stream_sizes = stream_data.get('size', {})
216         formats = []
217         for vid_format, stream_url in stream_data.get('url', {}).items():
218             height = int_or_none(self._search_regex(
219                 r's(\d+)p', vid_format, 'height', default=None))
220             formats.append({
221                 'format_id': vid_format,
222                 'url': stream_url,
223                 'height': height,
224                 'ext': 'mp4',
225                 'filesize': int_or_none(stream_sizes.get(vid_format))
226             })
227         self._sort_formats(formats)
228
229         subtitles = {}
230         for sub in video_data.get('subtitle', []):
231             sub_url = sub.get('url')
232             if not sub_url:
233                 continue
234             subtitles.setdefault(sub.get('name'), []).append({
235                 'url': sub_url,
236                 'ext': 'srt',
237             })
238
239         title = video_data['synopsis'].strip()
240
241         return {
242             'id': video_id,
243             'title': title,
244             'description': video_data.get('description'),
245             'series': product_data.get('series', {}).get('name'),
246             'episode': title,
247             'episode_number': int_or_none(video_data.get('number')),
248             'duration': int_or_none(stream_data.get('duration')),
249             'thumbnail': video_data.get('cover_image_url'),
250             'formats': formats,
251             'subtitles': subtitles,
252         }