Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
updated code and fixed #331 (course information extraction failed)
Browse files Browse the repository at this point in the history
  • Loading branch information
r0oth3x49 committed Mar 2, 2019
1 parent a2581e1 commit 85a5465
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
4 changes: 4 additions & 0 deletions udemy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
LOGIN_URL = 'https://www.udemy.com/api-2.0/auth/udemy-auth/login/?fields[user]=access_token'
LOGOUT_URL = 'https://www.udemy.com/user/logout'

WISHLIST_URL = "https://{portal_name}.udemy.com/api-2.0/users/me/wishlisted-courses?fields[course]=id,url,published_title&ordering=-access_time&page=1&page_size=1000"
COLLECTION_URL = "https://{portal_name}.udemy.com/api-2.0/users/me/subscribed-courses-collections/?collection_has_courses=True&course_limit=20&fields[course]=last_accessed_time,published_title&fields[user_has_subscribed_courses_collection]=@all&page=1&page_size=1000"
MY_COURSES_URL = "https://{portal_name}.udemy.com/api-2.0/users/me/subscribed-courses?fields[course]=id,url,published_title&ordering=-access_time&page=1&page_size=10000"
COURSE_SEARCH = "https://{portal_name}.udemy.com/api-2.0/users/me/subscribed-courses?fields[course]=id,url,published_title&page=1&page_size=1000&ordering=-access_time&search={course_name}"
COURSE_URL = 'https://{portal_name}.udemy.com/api-2.0/courses/{course_id}/cached-subscriber-curriculum-items?fields[asset]=results,external_url,time_estimation,download_urls,slide_urls,filename,asset_type,captions,stream_urls,body&fields[chapter]=object_index,title,sort_order&fields[lecture]=id,title,object_index,asset,supplementary_assets,view_html&page_size=10000'
Expand Down Expand Up @@ -106,6 +108,8 @@
'NO_DEFAULT',
'COURSE_URL',
'LOGOUT_URL',
'WISHLIST_URL',
'COLLECTION_URL',
'MY_COURSES_URL',
'COURSE_SEARCH'
]
88 changes: 62 additions & 26 deletions udemy/_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
COURSE_URL,
ParseCookie,
MY_COURSES_URL,
COURSE_SEARCH
COURSE_SEARCH,
COLLECTION_URL
)
from ._sanitize import (
slugify,
Expand Down Expand Up @@ -111,16 +112,8 @@ def _login(self, username='', password='', cookies=''):
def _logout(self):
return self._session.terminate()

def __extract_course(self, response, course_name):
_temp = {}
if response:
for entry in response:
if entry.get('published_title') == course_name:
_temp = entry
return _temp

def _extract_course_info(self, url):
portal_name, course_name = self._course_name(url)
def _subscribed_courses(self, portal_name, course_name):
results = []
self._session._headers.update({
'Host' : '{portal_name}.udemy.com'.format(portal_name=portal_name),
'Referer' : 'https://{portal_name}.udemy.com/home/my-courses/search/?q={course_name}'.format(portal_name=portal_name, course_name=course_name)
Expand All @@ -138,25 +131,68 @@ def _extract_course_info(self, url):
sys.exit(0)
else:
results = webpage.get('results', [])
if not results:
try:
url = MY_COURSES_URL.format(portal_name=portal_name)
webpage = self._session._get(url).json()
except conn_error as e:
sys.stdout.write(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Connection error : make sure your internet connection is working.\n")
time.sleep(0.8)
sys.exit(0)
except (ValueError, Exception) as e:
sys.stdout.write(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "%s.\n" % (e))
time.sleep(0.8)
sys.exit(0)
else:
results = webpage.get('results', [])
return results

def _my_courses(self, portal_name):
results = []
try:
url = MY_COURSES_URL.format(portal_name=portal_name)
webpage = self._session._get(url).json()
except conn_error as e:
sys.stdout.write(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Connection error : make sure your internet connection is working.\n")
time.sleep(0.8)
sys.exit(0)
except (ValueError, Exception) as e:
sys.stdout.write(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "%s.\n" % (e))
time.sleep(0.8)
sys.exit(0)
else:
results = webpage.get('results', [])
return results

def _subscribed_collection_courses(self, portal_name):
url = COLLECTION_URL.format(portal_name=portal_name)
courses_lists = []
try:
webpage = self._session._get(url).json()
except conn_error as e:
sys.stdout.write(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Connection error : make sure your internet connection is working.\n")
time.sleep(0.8)
sys.exit(0)
except (ValueError, Exception) as e:
sys.stdout.write(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "%s.\n" % (e))
time.sleep(0.8)
sys.exit(0)
else:
results = webpage.get('results', [])
if results:
[courses_lists.extend(courses.get('courses', [])) for courses in results if courses.get('courses', [])]
return courses_lists

def __extract_course(self, response, course_name):
_temp = {}
if response:
for entry in response:
if entry.get('published_title') == course_name:
_temp = entry
return _temp

def _extract_course_info(self, url):
portal_name, course_name = self._course_name(url)
course = {}
results = self._subscribed_courses(portal_name=portal_name, course_name=course_name)
if not results:
results = self._my_courses(portal_name=portal_name)
if results:
course = self.__extract_course(response=results, course_name=course_name)
if not course:
results = self._subscribed_collection_courses(portal_name=portal_name)
course = self.__extract_course(response=results, course_name=course_name)

if course:
course.update({'portal_name' : portal_name})
return course.get('id'), course
else:
if not course:
sys.stdout.write('\033[2K\033[1G\r\r' + fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fg + sb + "Downloading course information, course id not found .. (%s%sfailed%s%s)\n" % (fr, sb, fg, sb))
sys.stdout.write(fc + sd + "[" + fw + sb + "i" + fc + sd + "] : " + fw + sb + "It seems either you are not enrolled or you have to visit the course atleast once while you are logged in.\n")
sys.stdout.write(fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sb + "Trying to logout now...\n")
Expand Down

0 comments on commit 85a5465

Please sign in to comment.