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