[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / vshare.py
index ea39a9051ebf8531113f24a8f7fb96d13d080a40..c631ac1faa7243ebc0424330859720f5dc2c181b 100644 (file)
@@ -5,7 +5,10 @@ import re
 
 from .common import InfoExtractor
 from ..compat import compat_chr
-from ..utils import decode_packed_codes
+from ..utils import (
+    decode_packed_codes,
+    ExtractorError,
+)
 
 
 class VShareIE(InfoExtractor):
@@ -23,13 +26,20 @@ class VShareIE(InfoExtractor):
         'only_matching': True,
     }]
 
+    @staticmethod
+    def _extract_urls(webpage):
+        return re.findall(
+            r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?vshare\.io/v/[^/?#&]+)',
+            webpage)
+
     def _extract_packed(self, webpage):
-        packed = self._search_regex(r'(eval\(function.+)', webpage, 'packed code')
+        packed = self._search_regex(
+            r'(eval\(function.+)', webpage, 'packed code')
         unpacked = decode_packed_codes(packed)
         digits = self._search_regex(r'\[((?:\d+,?)+)\]', unpacked, 'digits')
-        digits = digits.split(',')
-        digits = [int(digit) for digit in digits]
-        key_digit = self._search_regex(r'fromCharCode\(.+?(\d+)\)}', unpacked, 'key digit')
+        digits = [int(digit) for digit in digits.split(',')]
+        key_digit = self._search_regex(
+            r'fromCharCode\(.+?(\d+)\)}', unpacked, 'key digit')
         chars = [compat_chr(d - int(key_digit)) for d in digits]
         return ''.join(chars)
 
@@ -37,22 +47,28 @@ class VShareIE(InfoExtractor):
         video_id = self._match_id(url)
 
         webpage = self._download_webpage(
-            'https://vshare.io/v/%s/width-650/height-430/1' % video_id, video_id)
+            'https://vshare.io/v/%s/width-650/height-430/1' % video_id,
+            video_id, headers={'Referer': url})
 
-        title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'title')
+        title = self._html_search_regex(
+            r'<title>([^<]+)</title>', webpage, 'title')
         title = title.split(' - ')[0]
 
-        unpacked = self._extract_packed(webpage)
-        video_urls = re.findall(r'<source src="([^"]+)', unpacked)
-        formats = [{'url': video_url} for video_url in video_urls]
-        return {
+        error = self._html_search_regex(
+            r'(?s)<div[^>]+\bclass=["\']xxx-error[^>]+>(.+?)</div', webpage,
+            'error', default=None)
+        if error:
+            raise ExtractorError(error, expected=True)
+
+        info = self._parse_html5_media_entries(
+            url, '<video>%s</video>' % self._extract_packed(webpage),
+            video_id)[0]
+
+        self._sort_formats(info['formats'])
+
+        info.update({
             'id': video_id,
             'title': title,
-            'formats': formats,
-        }
+        })
 
-    @staticmethod
-    def _extract_urls(webpage):
-        return re.findall(
-            r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?vshare\.io/v/[^/?#&]+)',
-            webpage)
+        return info