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