[toggle] Extract thumbnails
[youtube-dl] / youtube_dl / extractor / togglesg.py
index 9f958d453576948c0e7a1c13eca57d28678dc0b3..a2b89d6bb49d93b333dec1d46a4597e07b1e4bde 100644 (file)
@@ -3,17 +3,15 @@ from __future__ import unicode_literals
 
 import json
 import re
-import itertools
 
 from .common import InfoExtractor
 from ..utils import (
+    determine_ext,
     ExtractorError,
     int_or_none,
-    determine_ext,
     parse_iso8601,
-    remove_end
+    sanitized_Request,
 )
-from ..compat import compat_urllib_request
 
 
 class ToggleSgIE(InfoExtractor):
@@ -87,38 +85,43 @@ class ToggleSgIE(InfoExtractor):
     def _real_extract(self, url):
         video_id = self._match_id(url)
 
-        webpage = self._download_webpage(url, video_id, note='Downloading video page')
+        webpage = self._download_webpage(
+            url, video_id, note='Downloading video page')
 
         api_user = self._search_regex(
-            r'apiUser:\s*"([^"]+)"', webpage, 'apiUser', default=self._API_USER)
+            r'apiUser\s*:\s*(["\'])(?P<user>.+?)\1', webpage, 'apiUser',
+            default=self._API_USER, group='user')
         api_pass = self._search_regex(
-            r'apiPass:\s*"([^"]+)"', webpage, 'apiPass', default=self._API_PASS)
+            r'apiPass\s*:\s*(["\'])(?P<pass>.+?)\1', webpage, 'apiPass',
+            default=self._API_PASS, group='pass')
 
         params = {
             'initObj': {
                 'Locale': {
-                    'LocaleLanguage': '', 'LocaleCountry': '',
-                    'LocaleDevice': '', 'LocaleUserState': 0
+                    'LocaleLanguage': '',
+                    'LocaleCountry': '',
+                    'LocaleDevice': '',
+                    'LocaleUserState': 0
                 },
-                'Platform': 0, 'SiteGuid': 0, 'DomainID': '0', 'UDID': '',
-                'ApiUser': api_user, 'ApiPass': api_pass
+                'Platform': 0,
+                'SiteGuid': 0,
+                'DomainID': '0',
+                'UDID': '',
+                'ApiUser': api_user,
+                'ApiPass': api_pass
             },
             'MediaID': video_id,
             'mediaType': 0,
         }
 
-        req = compat_urllib_request.Request(
+        req = sanitized_Request(
             'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
             json.dumps(params).encode('utf-8'))
         info = self._download_json(req, video_id, 'Downloading video info json')
 
         title = info['MediaName']
-        duration = int_or_none(info.get('Duration'))
-        thumbnail = info.get('PicURL')
-        description = info.get('Description')
-        created_at = parse_iso8601(info.get('CreationDate') or None)
-        formats = []
 
+        formats = []
         for video_file in info.get('Files', []):
             ext = determine_ext(video_file['URL'])
             vid_format = video_file['Format'].replace(' ', '')
@@ -128,11 +131,10 @@ class ToggleSgIE(InfoExtractor):
                     video_file['URL'], video_id, ext='mp4', m3u8_id=vid_format,
                     note='Downloading %s m3u8 information' % vid_format,
                     errnote='Failed to download %s m3u8 information' % vid_format,
-                    fatal=False
-                )
+                    fatal=False)
                 if m3u8_formats:
                     formats.extend(m3u8_formats)
-            if ext in ['mp4', 'wvm']:
+            elif ext in ('mp4', 'wvm'):
                 # wvm are drm-protected files
                 formats.append({
                     'ext': ext,
@@ -141,19 +143,40 @@ class ToggleSgIE(InfoExtractor):
                     'preference': self._FORMAT_PREFERENCES.get(ext + '-' + vid_format) or -1,
                     'format_note': 'DRM-protected video' if ext == 'wvm' else None
                 })
-
         if not formats:
             # Most likely because geo-blocked
             raise ExtractorError('No downloadable videos found', expected=True)
-
         self._sort_formats(formats)
 
+        duration = int_or_none(info.get('Duration'))
+        description = info.get('Description')
+        created_at = parse_iso8601(info.get('CreationDate') or None)
+
+        thumbnails = []
+        for picture in info.get('Pictures', []):
+            if not isinstance(picture, dict):
+                continue
+            pic_url = picture.get('URL')
+            if not pic_url:
+                continue
+            thumbnail = {
+                'url': pic_url,
+            }
+            pic_size = picture.get('PicSize', '')
+            m = re.search(r'(?P<width>\d+)[xX](?P<height>\d+)', pic_size)
+            if m:
+                thumbnail.update({
+                    'width': int(m.group('width')),
+                    'height': int(m.group('height')),
+                })
+            thumbnails.append(thumbnail)
+
         return {
             'id': video_id,
             'title': title,
             'description': description,
             'duration': duration,
             'timestamp': created_at,
-            'thumbnail': thumbnail,
+            'thumbnails': thumbnails,
             'formats': formats,
         }