Simplify formats accumulation for f4m/m3u8/smil formats
[youtube-dl] / youtube_dl / extractor / funimation.py
index 06a57779ac12c07cacc531900392a6bcace26339..0f37ed7863c93da4813a6ac6be2d6402fc7bbd0a 100644 (file)
@@ -1,14 +1,12 @@
 # coding: utf-8
 from __future__ import unicode_literals
 
-import re
-
 from .common import InfoExtractor
-from ..compat import compat_urllib_request
 from ..utils import (
     clean_html,
     determine_ext,
     encode_dict,
+    int_or_none,
     sanitized_Request,
     ExtractorError,
     urlencode_postdata
@@ -16,30 +14,41 @@ from ..utils import (
 
 
 class FunimationIE(InfoExtractor):
-    _VALID_URL = r'https?://(?:www\.)?funimation\.com/shows/[^/]+/videos/official/(?P<id>[^?]+)'
+    _VALID_URL = r'https?://(?:www\.)?funimation\.com/shows/[^/]+/videos/(?:official|promotional)/(?P<id>[^/?#&]+)'
+
+    _NETRC_MACHINE = 'funimation'
 
-    _TEST = {
+    _TESTS = [{
         'url': 'http://www.funimation.com/shows/air/videos/official/breeze',
         'info_dict': {
-            'id': 'AIRENG0001',
-            'title': 'Air - 1 - Breeze ',
+            'id': '658',
+            'display_id': 'breeze',
             'ext': 'mp4',
-            'thumbnail': 'http://www.funimation.com/admin/uploads/default/recap_thumbnails/7555590/home_spotlight/AIR0001.jpg',
-            'description': 'Travelling puppeteer Yukito arrives in a small town where he hopes to earn money through the magic of his puppets. When a young girl named Misuzu lures him to her home with the promise of food, his life changes forever. ',
-        }
-    }
-
-    def _download_webpage(self, url_or_request, video_id, note='Downloading webpage'):
-        HEADERS = {
-            'User-Agent': 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0',
-        }
-        if isinstance(url_or_request, compat_urllib_request.Request):
-            for header, value in HEADERS.items():
-                url_or_request.add_header(header, value)
-        else:
-            url_or_request = sanitized_Request(url_or_request, headers=HEADERS)
-        response = super(FunimationIE, self)._download_webpage(url_or_request, video_id, note)
-        return response
+            'title': 'Air - 1 - Breeze',
+            'description': 'md5:1769f43cd5fc130ace8fd87232207892',
+            'thumbnail': 're:https?://.*\.jpg',
+        },
+    }, {
+        'url': 'http://www.funimation.com/shows/hacksign/videos/official/role-play',
+        'info_dict': {
+            'id': '31128',
+            'display_id': 'role-play',
+            'ext': 'mp4',
+            'title': '.hack//SIGN - 1 - Role Play',
+            'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
+            'thumbnail': 're:https?://.*\.jpg',
+        },
+    }, {
+        'url': 'http://www.funimation.com/shows/attack-on-titan-junior-high/videos/promotional/broadcast-dub-preview',
+        'info_dict': {
+            'id': '9635',
+            'display_id': 'broadcast-dub-preview',
+            'ext': 'mp4',
+            'title': 'Attack on Titan: Junior High - Broadcast Dub Preview',
+            'description': 'md5:f8ec49c0aff702a7832cd81b8a44f803',
+            'thumbnail': 're:https?://.*\.(?:jpg|png)',
+        },
+    }]
 
     def _login(self):
         (username, password) = self._get_login_info()
@@ -50,12 +59,19 @@ class FunimationIE(InfoExtractor):
             'password_field': password,
         }))
         login_request = sanitized_Request('http://www.funimation.com/login', data, headers={
+            'User-Agent': 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0',
             'Content-Type': 'application/x-www-form-urlencoded'
         })
-        login = self._download_webpage(
+        login_page = self._download_webpage(
             login_request, None, 'Logging in as %s' % username)
-        if re.search(r'<meta property="og:url" content="http://www.funimation.com/login"/>', login) is not None:
-            raise ExtractorError('Unable to login, wrong username or password.', expected=True)
+        if any(p in login_page for p in ('funimation.com/logout', '>Log Out<')):
+            return
+        error = self._html_search_regex(
+            r'(?s)<div[^>]+id=["\']errorMessages["\'][^>]*>(.+?)</div>',
+            login_page, 'error messages', default=None)
+        if error:
+            raise ExtractorError('Unable to login: %s' % error, expected=True)
+        raise ExtractorError('Unable to log in')
 
     def _real_initialize(self):
         self._login()
@@ -63,15 +79,8 @@ class FunimationIE(InfoExtractor):
     def _real_extract(self, url):
         display_id = self._match_id(url)
 
-        webpage = self._download_webpage(url, display_id)
-
-        items = self._parse_json(
-            self._search_regex(
-                r'var\s+playersData\s*=\s*(\[.+?\]);\n',
-                webpage, 'players data'),
-            display_id)[0]['playlist'][0]['items']
-
-        item = next(item for item in items if item.get('itemAK') == display_id)
+        errors = []
+        formats = []
 
         ERRORS_MAP = {
             'ERROR_MATURE_CONTENT_LOGGED_IN': 'matureContentLoggedIn',
@@ -86,48 +95,75 @@ class FunimationIE(InfoExtractor):
             'ERROR_STREAM_NOT_FOUND': 'streamNotFound',
         }
 
-        error_messages = {}
-        video_error_messages = self._search_regex(
-            r'var\s+videoErrorMessages\s*=\s*({.+?});\n',
-            webpage, 'error messages', default=None)
-        if video_error_messages:
-            error_messages_json = self._parse_json(video_error_messages, display_id, fatal=False)
-            if error_messages_json:
-                for _, error in error_messages_json.items():
-                    type_ = error.get('type')
-                    description = error.get('description')
-                    content = error.get('content')
-                    if type_ == 'text' and description and content:
-                        error_message = ERRORS_MAP.get(description)
-                        if error_message:
-                            error_messages[error_message] = content
-
-        errors = []
-        formats = []
-        for video in item['videoSet']:
-            auth_token = video.get('authToken')
-            if not auth_token:
-                continue
-            funimation_id = video.get('FUNImationID') or video.get('videoId')
-            if not auth_token.startswith('?'):
-                auth_token = '?%s' % auth_token
-            for quality in ('sd', 'hd', 'hd1080'):
-                format_url = video.get('%sUrl' % quality)
-                if not format_url:
+        USER_AGENTS = (
+            # PC UA is served with m3u8 that provides some bonus lower quality formats
+            ('pc', 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'),
+            # Mobile UA allows to extract direct links and also does not fail when
+            # PC UA fails with hulu error (e.g.
+            # http://www.funimation.com/shows/hacksign/videos/official/role-play)
+            ('mobile', 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36'),
+        )
+
+        for kind, user_agent in USER_AGENTS:
+            request = sanitized_Request(url)
+            request.add_header('User-Agent', user_agent)
+            webpage = self._download_webpage(
+                request, display_id, 'Downloading %s webpage' % kind)
+
+            playlist = self._parse_json(
+                self._search_regex(
+                    r'var\s+playersData\s*=\s*(\[.+?\]);\n',
+                    webpage, 'players data'),
+                display_id)[0]['playlist']
+
+            items = next(item['items'] for item in playlist if item.get('items'))
+            item = next(item for item in items if item.get('itemAK') == display_id)
+
+            error_messages = {}
+            video_error_messages = self._search_regex(
+                r'var\s+videoErrorMessages\s*=\s*({.+?});\n',
+                webpage, 'error messages', default=None)
+            if video_error_messages:
+                error_messages_json = self._parse_json(video_error_messages, display_id, fatal=False)
+                if error_messages_json:
+                    for _, error in error_messages_json.items():
+                        type_ = error.get('type')
+                        description = error.get('description')
+                        content = error.get('content')
+                        if type_ == 'text' and description and content:
+                            error_message = ERRORS_MAP.get(description)
+                            if error_message:
+                                error_messages[error_message] = content
+
+            for video in item.get('videoSet', []):
+                auth_token = video.get('authToken')
+                if not auth_token:
                     continue
-                if not format_url.startswith(('http', '//')):
-                    errors.append(format_url)
-                    continue
-                if determine_ext(format_url) == 'm3u8':
-                    m3u8_formats = self._extract_m3u8_formats(
-                        format_url + auth_token, display_id, 'mp4', entry_protocol='m3u8_native',
-                        m3u8_id=funimation_id or 'hls', fatal=False)
-                    if m3u8_formats:
-                        formats.extend(m3u8_formats)
-                else:
-                    formats.append({
-                        'url': format_url + auth_token,
-                    })
+                funimation_id = video.get('FUNImationID') or video.get('videoId')
+                preference = 1 if video.get('languageMode') == 'dub' else 0
+                if not auth_token.startswith('?'):
+                    auth_token = '?%s' % auth_token
+                for quality, height in (('sd', 480), ('hd', 720), ('hd1080', 1080)):
+                    format_url = video.get('%sUrl' % quality)
+                    if not format_url:
+                        continue
+                    if not format_url.startswith(('http', '//')):
+                        errors.append(format_url)
+                        continue
+                    if determine_ext(format_url) == 'm3u8':
+                        formats.extend(self._extract_m3u8_formats(
+                            format_url + auth_token, display_id, 'mp4', entry_protocol='m3u8_native',
+                            preference=preference, m3u8_id='%s-hls' % funimation_id, fatal=False))
+                    else:
+                        tbr = int_or_none(self._search_regex(
+                            r'-(\d+)[Kk]', format_url, 'tbr', default=None))
+                        formats.append({
+                            'url': format_url + auth_token,
+                            'format_id': '%s-http-%dp' % (funimation_id, height),
+                            'height': height,
+                            'tbr': tbr,
+                            'preference': preference,
+                        })
 
         if not formats and errors:
             raise ExtractorError(
@@ -135,6 +171,8 @@ class FunimationIE(InfoExtractor):
                 % (self.IE_NAME, clean_html(error_messages.get(errors[0], errors[0]))),
                 expected=True)
 
+        self._sort_formats(formats)
+
         title = item['title']
         artist = item.get('artist')
         if artist: