]> asedeno.scripts.mit.edu Git - youtube-dl.git/blob - youtube_dl/extractor/toggle.py
[twitch:clips] Add access token query to download URLs (closes #29136)
[youtube-dl.git] / youtube_dl / extractor / toggle.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     float_or_none,
12     int_or_none,
13     parse_iso8601,
14     strip_or_none,
15 )
16
17
18 class ToggleIE(InfoExtractor):
19     IE_NAME = 'toggle'
20     _VALID_URL = r'(?:https?://(?:(?:www\.)?mewatch|video\.toggle)\.sg/(?:en|zh)/(?:[^/]+/){2,}|toggle:)(?P<id>[0-9]+)'
21     _TESTS = [{
22         'url': 'http://www.mewatch.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://www.mewatch.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         # this also tests correct video id extraction
50         'note': 'm3u8 links are geo-restricted, but Android/mp4 is okay',
51         'url': 'http://www.mewatch.sg/en/series/28th-sea-games-5-show/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://www.mewatch.sg/en/clips/seraph-sun-aloysius-will-suddenly-sing-some-old-songs-in-high-pitch-on-set/343331',
69         'only_matching': True,
70     }, {
71         'url': 'http://www.mewatch.sg/zh/series/zero-calling-s2-hd/ep13/336367',
72         'only_matching': True,
73     }, {
74         'url': 'http://www.mewatch.sg/en/series/vetri-s2/webisodes/jeeva-is-an-orphan-vetri-s2-webisode-7/342302',
75         'only_matching': True,
76     }, {
77         'url': 'http://www.mewatch.sg/en/movies/seven-days/321936',
78         'only_matching': True,
79     }, {
80         'url': 'https://www.mewatch.sg/en/tv-show/news/may-2017-cna-singapore-tonight/fri-19-may-2017/512456',
81         'only_matching': True,
82     }, {
83         'url': 'http://www.mewatch.sg/en/channels/eleven-plus/401585',
84         'only_matching': True,
85     }]
86
87     _API_USER = 'tvpapi_147'
88     _API_PASS = '11111'
89
90     def _real_extract(self, url):
91         video_id = self._match_id(url)
92
93         params = {
94             'initObj': {
95                 'Locale': {
96                     'LocaleLanguage': '',
97                     'LocaleCountry': '',
98                     'LocaleDevice': '',
99                     'LocaleUserState': 0
100                 },
101                 'Platform': 0,
102                 'SiteGuid': 0,
103                 'DomainID': '0',
104                 'UDID': '',
105                 'ApiUser': self._API_USER,
106                 'ApiPass': self._API_PASS
107             },
108             'MediaID': video_id,
109             'mediaType': 0,
110         }
111
112         info = self._download_json(
113             'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
114             video_id, 'Downloading video info json', data=json.dumps(params).encode('utf-8'))
115
116         title = info['MediaName']
117
118         formats = []
119         for video_file in info.get('Files', []):
120             video_url, vid_format = video_file.get('URL'), video_file.get('Format')
121             if not video_url or video_url == 'NA' or not vid_format:
122                 continue
123             ext = determine_ext(video_url)
124             vid_format = vid_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_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                 for f in m3u8_formats:
133                     # Apple FairPlay Streaming
134                     if '/fpshls/' in f['url']:
135                         continue
136                     formats.append(f)
137             elif ext == 'mpd':
138                 formats.extend(self._extract_mpd_formats(
139                     video_url, video_id, mpd_id=vid_format,
140                     note='Downloading %s MPD manifest' % vid_format,
141                     errnote='Failed to download %s MPD manifest' % vid_format,
142                     fatal=False))
143             elif ext == 'ism':
144                 formats.extend(self._extract_ism_formats(
145                     video_url, video_id, ism_id=vid_format,
146                     note='Downloading %s ISM manifest' % vid_format,
147                     errnote='Failed to download %s ISM manifest' % vid_format,
148                     fatal=False))
149             elif ext == 'mp4':
150                 formats.append({
151                     'ext': ext,
152                     'url': video_url,
153                     'format_id': vid_format,
154                 })
155         if not formats:
156             for meta in (info.get('Metas') or []):
157                 if meta.get('Key') == 'Encryption' and meta.get('Value') == '1':
158                     raise ExtractorError(
159                         'This video is DRM protected.', expected=True)
160             # Most likely because geo-blocked
161             raise ExtractorError('No downloadable videos found', expected=True)
162         self._sort_formats(formats)
163
164         thumbnails = []
165         for picture in info.get('Pictures', []):
166             if not isinstance(picture, dict):
167                 continue
168             pic_url = picture.get('URL')
169             if not pic_url:
170                 continue
171             thumbnail = {
172                 'url': pic_url,
173             }
174             pic_size = picture.get('PicSize', '')
175             m = re.search(r'(?P<width>\d+)[xX](?P<height>\d+)', pic_size)
176             if m:
177                 thumbnail.update({
178                     'width': int(m.group('width')),
179                     'height': int(m.group('height')),
180                 })
181             thumbnails.append(thumbnail)
182
183         def counter(prefix):
184             return int_or_none(
185                 info.get(prefix + 'Counter') or info.get(prefix.lower() + '_counter'))
186
187         return {
188             'id': video_id,
189             'title': title,
190             'description': strip_or_none(info.get('Description')),
191             'duration': int_or_none(info.get('Duration')),
192             'timestamp': parse_iso8601(info.get('CreationDate') or None),
193             'average_rating': float_or_none(info.get('Rating')),
194             'view_count': counter('View'),
195             'like_count': counter('Like'),
196             'thumbnails': thumbnails,
197             'formats': formats,
198         }
199
200
201 class MeWatchIE(InfoExtractor):
202     IE_NAME = 'mewatch'
203     _VALID_URL = r'https?://(?:(?:www|live)\.)?mewatch\.sg/watch/[^/?#&]+-(?P<id>[0-9]+)'
204     _TESTS = [{
205         'url': 'https://www.mewatch.sg/watch/Recipe-Of-Life-E1-179371',
206         'info_dict': {
207             'id': '1008625',
208             'ext': 'mp4',
209             'title': 'Recipe Of Life 味之道',
210             'timestamp': 1603306526,
211             'description': 'md5:6e88cde8af2068444fc8e1bc3ebf257c',
212             'upload_date': '20201021',
213         },
214         'params': {
215             'skip_download': 'm3u8 download',
216         },
217     }, {
218         'url': 'https://www.mewatch.sg/watch/Little-Red-Dot-Detectives-S2-搜密。打卡。小红点-S2-E1-176232',
219         'only_matching': True,
220     }, {
221         'url': 'https://www.mewatch.sg/watch/Little-Red-Dot-Detectives-S2-%E6%90%9C%E5%AF%86%E3%80%82%E6%89%93%E5%8D%A1%E3%80%82%E5%B0%8F%E7%BA%A2%E7%82%B9-S2-E1-176232',
222         'only_matching': True,
223     }, {
224         'url': 'https://live.mewatch.sg/watch/Recipe-Of-Life-E41-189759',
225         'only_matching': True,
226     }]
227
228     def _real_extract(self, url):
229         item_id = self._match_id(url)
230         custom_id = self._download_json(
231             'https://cdn.mewatch.sg/api/items/' + item_id,
232             item_id, query={'segments': 'all'})['customId']
233         return self.url_result(
234             'toggle:' + custom_id, ToggleIE.ie_key(), custom_id)