[toggle] Use sanitized_Request
[youtube-dl] / youtube_dl / extractor / togglesg.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6 import itertools
7
8 from .common import InfoExtractor
9 from ..utils import (
10     ExtractorError,
11     int_or_none,
12     determine_ext,
13     parse_iso8601,
14     remove_end,
15     sanitized_Request,
16 )
17 from ..compat import compat_urllib_request
18
19
20 class ToggleSgIE(InfoExtractor):
21     IE_NAME = 'togglesg'
22     _VALID_URL = r'https?://video\.toggle\.sg/(?:en|zh)/(?:series|clips|movies)/.+?/(?P<id>[0-9]+)'
23     _TESTS = [{
24         'url': 'http://video.toggle.sg/en/series/lion-moms-tif/trailers/lion-moms-premier/343115',
25         'info_dict': {
26             'id': '343115',
27             'ext': 'mp4',
28             'title': 'Lion Moms Premiere',
29             'description': 'md5:aea1149404bff4d7f7b6da11fafd8e6b',
30             'upload_date': '20150910',
31             'timestamp': 1441858274,
32         },
33         'params': {
34             'skip_download': 'm3u8 download',
35         }
36     }, {
37         'note': 'DRM-protected video',
38         'url': 'http://video.toggle.sg/en/movies/dug-s-special-mission/341413',
39         'info_dict': {
40             'id': '341413',
41             'ext': 'wvm',
42             'title': 'Dug\'s Special Mission',
43             'description': 'md5:e86c6f4458214905c1772398fabc93e0',
44             'upload_date': '20150827',
45             'timestamp': 1440644006,
46         },
47         'params': {
48             'skip_download': 'DRM-protected wvm download',
49         }
50     }, {
51         'note': 'm3u8 links are geo-restricted, but Android/mp4 is okay',
52         'url': 'http://video.toggle.sg/en/series/28th-sea-games-5-show/ep11/332861',
53         'info_dict': {
54             'id': '332861',
55             'ext': 'mp4',
56             'title': '28th SEA Games (5 Show) -  Episode  11',
57             'description': 'md5:3cd4f5f56c7c3b1340c50a863f896faa',
58             'upload_date': '20150605',
59             'timestamp': 1433480166,
60         },
61         'params': {
62             'skip_download': 'DRM-protected wvm download',
63         },
64         'skip': 'm3u8 links are geo-restricted'
65     }, {
66         'url': 'http://video.toggle.sg/en/clips/seraph-sun-aloysius-will-suddenly-sing-some-old-songs-in-high-pitch-on-set/343331',
67         'only_matching': True,
68     }, {
69         'url': 'http://video.toggle.sg/zh/series/zero-calling-s2-hd/ep13/336367',
70         'only_matching': True,
71     }, {
72         'url': 'http://video.toggle.sg/en/series/vetri-s2/webisodes/jeeva-is-an-orphan-vetri-s2-webisode-7/342302',
73         'only_matching': True,
74     }, {
75         'url': 'http://video.toggle.sg/en/movies/seven-days/321936',
76         'only_matching': True,
77     }]
78
79     _FORMAT_PREFERENCES = {
80         'wvm-STBMain': -10,
81         'wvm-iPadMain': -20,
82         'wvm-iPhoneMain': -30,
83         'wvm-Android': -40,
84     }
85     _API_USER = 'tvpapi_147'
86     _API_PASS = '11111'
87
88     def _real_extract(self, url):
89         video_id = self._match_id(url)
90
91         webpage = self._download_webpage(url, video_id, note='Downloading video page')
92
93         api_user = self._search_regex(
94             r'apiUser:\s*"([^"]+)"', webpage, 'apiUser', default=self._API_USER)
95         api_pass = self._search_regex(
96             r'apiPass:\s*"([^"]+)"', webpage, 'apiPass', default=self._API_PASS)
97
98         params = {
99             'initObj': {
100                 'Locale': {
101                     'LocaleLanguage': '', 'LocaleCountry': '',
102                     'LocaleDevice': '', 'LocaleUserState': 0
103                 },
104                 'Platform': 0, 'SiteGuid': 0, 'DomainID': '0', 'UDID': '',
105                 'ApiUser': api_user, 'ApiPass': api_pass
106             },
107             'MediaID': video_id,
108             'mediaType': 0,
109         }
110
111         req = sanitized_Request(
112             'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
113             json.dumps(params).encode('utf-8'))
114         info = self._download_json(req, video_id, 'Downloading video info json')
115
116         title = info['MediaName']
117         duration = int_or_none(info.get('Duration'))
118         thumbnail = info.get('PicURL')
119         description = info.get('Description')
120         created_at = parse_iso8601(info.get('CreationDate') or None)
121         formats = []
122
123         for video_file in info.get('Files', []):
124             ext = determine_ext(video_file['URL'])
125             vid_format = video_file['Format'].replace(' ', '')
126             # if geo-restricted, m3u8 is inaccessible, but mp4 is okay
127             if ext == 'm3u8':
128                 m3u8_formats = self._extract_m3u8_formats(
129                     video_file['URL'], video_id, ext='mp4', m3u8_id=vid_format,
130                     note='Downloading %s m3u8 information' % vid_format,
131                     errnote='Failed to download %s m3u8 information' % vid_format,
132                     fatal=False
133                 )
134                 if m3u8_formats:
135                     formats.extend(m3u8_formats)
136             if ext in ['mp4', 'wvm']:
137                 # wvm are drm-protected files
138                 formats.append({
139                     'ext': ext,
140                     'url': video_file['URL'],
141                     'format_id': vid_format,
142                     'preference': self._FORMAT_PREFERENCES.get(ext + '-' + vid_format) or -1,
143                     'format_note': 'DRM-protected video' if ext == 'wvm' else None
144                 })
145
146         if not formats:
147             # Most likely because geo-blocked
148             raise ExtractorError('No downloadable videos found', expected=True)
149
150         self._sort_formats(formats)
151
152         return {
153             'id': video_id,
154             'title': title,
155             'description': description,
156             'duration': duration,
157             'timestamp': created_at,
158             'thumbnail': thumbnail,
159             'formats': formats,
160         }