summary refs log tree commit diff
diff options
context:
space:
mode:
authorSergey M․ <dstftw@gmail.com>2014-11-26 21:25:43 +0600
committerSergey M․ <dstftw@gmail.com>2014-11-26 21:25:43 +0600
commit61c0663c1e40a6bfabb2a7ac933ce468f2989808 (patch)
treee4022ed5f94e34ed7b883f424fae9ed365185d94
parent81a7a521c5bdbd862b7c7d161425295fed63d212 (diff)
downloadyoutube-dl-61c0663c1e40a6bfabb2a7ac933ce468f2989808.tar.gz
youtube-dl-61c0663c1e40a6bfabb2a7ac933ce468f2989808.tar.xz
youtube-dl-61c0663c1e40a6bfabb2a7ac933ce468f2989808.zip
[udemy] Generalize download json and fix login
-rw-r--r--youtube_dl/extractor/udemy.py27
1 files changed, 15 insertions, 12 deletions
diff --git a/youtube_dl/extractor/udemy.py b/youtube_dl/extractor/udemy.py
index cf7bd0ae5..0e4d386a8 100644
--- a/youtube_dl/extractor/udemy.py
+++ b/youtube_dl/extractor/udemy.py
@@ -40,12 +40,7 @@ class UdemyIE(InfoExtractor):
                 error_str += ' - %s' % error_data.get('formErrors')
             raise ExtractorError(error_str, expected=True)
 
-    def _download_json(self, url, video_id, note='Downloading JSON metadata'):
-        response = super(UdemyIE, self)._download_json(url, video_id, note)
-        self._handle_error(response)
-        return response
-
-    def _download_json_cookies(self, url, video_id, note):
+    def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
         headers = {
             'X-Udemy-Snail-Case': 'true',
             'X-Requested-With': 'XMLHttpRequest',
@@ -55,8 +50,16 @@ class UdemyIE(InfoExtractor):
                 headers['X-Udemy-Client-Id'] = cookie.value
             elif cookie.name == 'access_token':
                 headers['X-Udemy-Bearer-Token'] = cookie.value
-        request = compat_urllib_request.Request(url, headers=headers)
-        return self._download_json(request, video_id, note)
+
+        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 = compat_urllib_request.Request(url_or_request, headers=headers)
+
+        response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
+        self._handle_error(response)
+        return response
 
     def _real_initialize(self):
         self._login()
@@ -87,7 +90,7 @@ class UdemyIE(InfoExtractor):
             'isSubmitted': '1',
         }
         request = compat_urllib_request.Request(
-            self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
+            self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
         response = self._download_json(
             request, None, 'Logging in as %s' % username)
 
@@ -100,7 +103,7 @@ class UdemyIE(InfoExtractor):
         mobj = re.match(self._VALID_URL, url)
         lecture_id = mobj.group('id')
 
-        lecture = self._download_json_cookies(
+        lecture = self._download_json(
             'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id,
             lecture_id, 'Downloading lecture JSON')
 
@@ -164,7 +167,7 @@ class UdemyCourseIE(UdemyIE):
         mobj = re.match(self._VALID_URL, url)
         course_path = mobj.group('coursepath')
 
-        response = self._download_json_cookies(
+        response = self._download_json(
             'https://www.udemy.com/api-1.1/courses/%s' % course_path,
             course_path, 'Downloading course JSON')
 
@@ -180,7 +183,7 @@ class UdemyCourseIE(UdemyIE):
         elif self._ALREADY_ENROLLED in webpage:
             self.to_screen('%s: Already enrolled in' % course_id)
 
-        response = self._download_json_cookies(
+        response = self._download_json(
             'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
             course_id, 'Downloading course curriculum')