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