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