[youtube] Add support for invidiou.sh (#20309)
[youtube-dl] / youtube_dl / extractor / dramafever.py
index c7a048f9d5fce7113775f46e2471c2f58582bf62..db1de699fafc5885da2c48f72f5e102825449703 100644 (file)
@@ -2,10 +2,11 @@
 from __future__ import unicode_literals
 
 import itertools
+import json
 
 from .common import InfoExtractor
 from ..compat import (
-    compat_str,
+    compat_HTTPError,
     compat_urlparse,
 )
 from ..utils import (
@@ -14,14 +15,12 @@ from ..utils import (
     int_or_none,
     parse_age_limit,
     parse_duration,
-    sanitized_Request,
     unified_timestamp,
-    urlencode_postdata
+    url_or_none,
 )
 
 
 class DramaFeverBaseIE(InfoExtractor):
-    _LOGIN_URL = 'https://www.dramafever.com/accounts/login/'
     _NETRC_MACHINE = 'dramafever'
 
     _CONSUMER_SECRET = 'DA59dtVXYLxajktV'
@@ -39,11 +38,11 @@ class DramaFeverBaseIE(InfoExtractor):
             'consumer secret', default=self._CONSUMER_SECRET)
 
     def _real_initialize(self):
-        self._login()
         self._consumer_secret = self._get_consumer_secret()
+        self._login()
 
     def _login(self):
-        (username, password) = self._get_login_info()
+        username, password = self._get_login_info()
         if username is None:
             return
 
@@ -52,19 +51,29 @@ class DramaFeverBaseIE(InfoExtractor):
             'password': password,
         }
 
-        request = sanitized_Request(
-            self._LOGIN_URL, urlencode_postdata(login_form))
-        response = self._download_webpage(
-            request, None, 'Logging in')
+        try:
+            response = self._download_json(
+                'https://www.dramafever.com/api/users/login', None, 'Logging in',
+                data=json.dumps(login_form).encode('utf-8'), headers={
+                    'x-consumer-key': self._consumer_secret,
+                })
+        except ExtractorError as e:
+            if isinstance(e.cause, compat_HTTPError) and e.cause.code in (403, 404):
+                response = self._parse_json(
+                    e.cause.read().decode('utf-8'), None)
+            else:
+                raise
+
+        # Successful login
+        if response.get('result') or response.get('guid') or response.get('user_guid'):
+            return
 
-        if all(logout_pattern not in response
-               for logout_pattern in ['href="/accounts/logout/"', '>Log out<']):
-            error = self._html_search_regex(
-                r'(?s)<h\d[^>]+\bclass="hidden-xs prompt"[^>]*>(.+?)</h\d',
-                response, 'error message', default=None)
-            if error:
-                raise ExtractorError('Unable to login: %s' % error, expected=True)
-            raise ExtractorError('Unable to log in')
+        errors = response.get('errors')
+        if errors and isinstance(errors, list):
+            error = errors[0]
+            message = error.get('message') or error['reason']
+            raise ExtractorError('Unable to login: %s' % message, expected=True)
+        raise ExtractorError('Unable to log in')
 
 
 class DramaFeverIE(DramaFeverBaseIE):
@@ -130,8 +139,8 @@ class DramaFeverIE(DramaFeverBaseIE):
         for sub in subs:
             if not isinstance(sub, dict):
                 continue
-            sub_url = sub.get('url')
-            if not sub_url or not isinstance(sub_url, compat_str):
+            sub_url = url_or_none(sub.get('url'))
+            if not sub_url:
                 continue
             subtitles.setdefault(
                 sub.get('code') or sub.get('language') or 'en', []).append({
@@ -154,8 +163,8 @@ class DramaFeverIE(DramaFeverBaseIE):
             for format_id, format_dict in download_assets.items():
                 if not isinstance(format_dict, dict):
                     continue
-                format_url = format_dict.get('url')
-                if not format_url or not isinstance(format_url, compat_str):
+                format_url = url_or_none(format_dict.get('url'))
+                if not format_url:
                     continue
                 formats.append({
                     'url': format_url,