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

Commit

Permalink
fixed #594, fixed #595 (now you can specify to download all assets)..
Browse files Browse the repository at this point in the history
  • Loading branch information
r0oth3x49 committed Dec 19, 2020
1 parent 4608ec3 commit f623df7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- You would need FFmpeg to be installed and added to environment variable so that udemy-dl can access.
- Download [FFmpeg from here](https://ffmpeg.org/download.html)
- On ubuntu you can install it via `apt install ffmpeg`.
- Add to environment variables then udemy-dl will be able to use it when download HLS streams.
- Add to environment variables then udemy-dl will be able to use it when downloading HLS streams.

## ***Tested on***

Expand Down
31 changes: 23 additions & 8 deletions udemy-dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def download_assets(self, assets, filepath):
logger.info(msg=f"Downloading ({title})", new_line=True)
try:
retval = asset.download(
filepath=filepath, quiet=True, callback=self.show_progress,
filepath=filepath,
quiet=True,
callback=self.show_progress,
)
msg = retval.get("msg")
if msg == "already downloaded":
Expand All @@ -87,7 +89,9 @@ def download_lecture(self, lecture, filepath, current, total, quality):
logger.info(msg=f"Downloading ({title})", new_line=True)
try:
retval = lecture.download(
filepath=filepath, quiet=True, callback=self.show_progress,
filepath=filepath,
quiet=True,
callback=self.show_progress,
)
msg = retval.get("msg")
if msg == "already downloaded":
Expand All @@ -102,7 +106,7 @@ def download_lecture(self, lecture, filepath, current, total, quality):

def download_subtitles(self, subtitles, filepath, language="en", keep_vtt=False):
"""This function will simply download the subtitles.."""
if language and subtitles:
if language and subtitles and language != "all":
subtitle = subtitles[0]
subtitles = subtitle.get_subtitle(language)
if subtitles:
Expand All @@ -113,7 +117,9 @@ def download_subtitles(self, subtitles, filepath, language="en", keep_vtt=False)
logger.info(msg=f"Downloading ({title})", new_line=True)
try:
retval = sub.download(
filepath=filepath, quiet=True, callback=self.show_progress,
filepath=filepath,
quiet=True,
callback=self.show_progress,
)
msg = retval.get("msg")
if msg == "already downloaded":
Expand Down Expand Up @@ -241,7 +247,10 @@ def course_listdown(
f"{asset}", asset.extension, human_readable
)
logger.info(
indent=indent, msg=msg, new_line=True, cc=15,
indent=indent,
msg=msg,
new_line=True,
cc=15,
)
if lecture_subtitles:
for sub in lecture_subtitles:
Expand All @@ -253,7 +262,10 @@ def course_listdown(
f"{sub}", sub.extension, human_readable
)
logger.info(
indent=indent, msg=msg, new_line=True, cc=15,
indent=indent,
msg=msg,
new_line=True,
cc=15,
)
print("")

Expand Down Expand Up @@ -329,7 +341,8 @@ def course_download(
msg=f"Chapter ({chapter_title})", new_line=True, cc=15, cc_msg=60
)
logger.info(
msg=f"Found ({lectures_count}) lecture(s).", new_line=True,
msg=f"Found ({lectures_count}) lecture(s).",
new_line=True,
)
lecture_index = 0
if lecture_number:
Expand Down Expand Up @@ -580,7 +593,9 @@ def main():
if not args.info:
if args.quality and args.quality > 720 and args.skip_hls_stream:
args.quality = ""
logger.warning(msg="You cannot use --skip-hls and -q/--quality options togather, considering --skip-hls only..")
logger.warning(
msg="You cannot use --skip-hls and -q/--quality options togather, considering --skip-hls only.."
)
udemy_obj.course_download(
path=args.output,
quality=args.quality,
Expand Down
2 changes: 1 addition & 1 deletion udemy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
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=-last_accessed,-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=-last_accessed,-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"
COURSE_URL = "https://{portal_name}.udemy.com/api-2.0/courses/{course_id}/cached-subscriber-curriculum-items?fields[asset]=results,title,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"
SUBSCRIBED_COURSES = "https://www.udemy.com/api-2.0/users/me/subscribed-courses/?ordering=-last_accessed&fields[course]=id,url&page=1&page_size=12"
HEADERS = {
"Origin": "www.udemy.com",
Expand Down
36 changes: 24 additions & 12 deletions udemy/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ def _login(self, username="", password="", cookies=""):
is_exists, conf = auth.is_session_exists()
if is_exists and username and password:
logger.info(
msg="Using existing session..", new_line=True,
msg="Using existing session..",
new_line=True,
)
cookies = conf.get("cookies")
if not is_exists:
cookies = None
if not username and not password:
logger.info(
msg="Updating session cookie..", new_line=True,
msg="Updating session cookie..",
new_line=True,
)
username = conf.get("username")
password = conf.get("password")
Expand All @@ -102,7 +104,9 @@ def _login(self, username="", password="", cookies=""):
password = getpass.getpass(prompt="Password : ")
print("\n")
if not cookies and not username and not password:
logger.error(msg=f"You should either provide Fresh Access Token or Username/Password to create new udemy session..")
logger.error(
msg=f"You should either provide Fresh Access Token or Username/Password to create new udemy session.."
)
sys.exit(0)
if not cookies:
auth.username = username
Expand Down Expand Up @@ -301,12 +305,14 @@ def _extract_course_info(self, url):
new_line=True,
)
logger.info(
msg="Trying to logout now...", new_line=True,
msg="Trying to logout now...",
new_line=True,
)
if not self._cookies:
self._logout()
logger.info(
msg="Logged out successfully.", new_line=True,
msg="Logged out successfully.",
new_line=True,
)
sys.exit(0)

Expand Down Expand Up @@ -419,7 +425,7 @@ def _extract_m3u8(self, url):
def _extract_ppt(self, assets):
_temp = []
download_urls = assets.get("download_urls")
filename = self._sanitize(assets.get("filename"))
filename = assets.get("filename")
if download_urls and isinstance(download_urls, dict):
extension = filename.rsplit(".", 1)[-1] if "." in filename else ""
download_url = download_urls.get("Presentation", [])[0].get("file")
Expand All @@ -436,7 +442,7 @@ def _extract_ppt(self, assets):
def _extract_file(self, assets):
_temp = []
download_urls = assets.get("download_urls")
filename = self._sanitize(assets.get("filename"))
filename = assets.get("filename")
if download_urls and isinstance(download_urls, dict):
extension = filename.rsplit(".", 1)[-1] if "." in filename else ""
download_url = download_urls.get("File", [])[0].get("file")
Expand All @@ -453,7 +459,7 @@ def _extract_file(self, assets):
def _extract_ebook(self, assets):
_temp = []
download_urls = assets.get("download_urls")
filename = self._sanitize(assets.get("filename"))
filename = assets.get("filename")
if download_urls and isinstance(download_urls, dict):
extension = filename.rsplit(".", 1)[-1] if "." in filename else ""
download_url = download_urls.get("E-Book", [])[0].get("file")
Expand All @@ -470,7 +476,7 @@ def _extract_ebook(self, assets):
def _extract_audio(self, assets):
_temp = []
download_urls = assets.get("download_urls")
filename = self._sanitize(assets.get("filename"))
filename = assets.get("filename")
if download_urls and isinstance(download_urls, dict):
extension = filename.rsplit(".", 1)[-1] if "." in filename else ""
download_url = download_urls.get("Audio", [])[0].get("file")
Expand Down Expand Up @@ -563,7 +569,8 @@ def _extract_subtitles(self, tracks):
def _extract_supplementary_assets(self, supp_assets):
_temp = []
for entry in supp_assets:
filename = self._sanitize(entry.get("filename"))
title = self._clean(entry.get("title"))
filename = entry.get("filename")
download_urls = entry.get("download_urls")
external_url = entry.get("external_url")
asset_type = entry.get("asset_type").lower()
Expand All @@ -574,6 +581,7 @@ def _extract_supplementary_assets(self, supp_assets):
_temp.append(
{
"type": "file",
"title": title,
"filename": filename,
"extension": extension,
"download_url": download_url,
Expand All @@ -586,6 +594,7 @@ def _extract_supplementary_assets(self, supp_assets):
_temp.append(
{
"type": "source_code",
"title": title,
"filename": filename,
"extension": extension,
"download_url": download_url,
Expand All @@ -595,6 +604,7 @@ def _extract_supplementary_assets(self, supp_assets):
_temp.append(
{
"type": "external_link",
"title": title,
"filename": filename,
"extension": "txt",
"download_url": external_url,
Expand Down Expand Up @@ -625,12 +635,14 @@ def _real_extract(self, url="", skip_hls_stream=False):
msg=f"Udemy Says : '{resource}' cookies seems to be expired"
)
logger.info(
msg="Trying to logout now...", new_line=True,
msg="Trying to logout now...",
new_line=True,
)
if not self._cookies:
self._logout()
logger.info(
msg="Logged out successfully.", new_line=True,
msg="Logged out successfully.",
new_line=True,
)
sys.exit(0)

Expand Down
22 changes: 16 additions & 6 deletions udemy/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def _fetch_course(self):
if auth.get("login") == "successful":
logger.info(msg="Logged in successfully.", new_line=True)
logger.info(msg="Downloading course information ..")
self._info = self._real_extract(self._url, skip_hls_stream=self._skip_hls_stream)
self._info = self._real_extract(
self._url, skip_hls_stream=self._skip_hls_stream
)
time.sleep(1)
logger.success(msg="Downloaded course information .. ")
access_token = self._info["access_token"]
Expand All @@ -88,12 +90,14 @@ def _fetch_course(self):
for z in self._info["chapters"]
]
logger.info(
msg="Trying to logout now...", new_line=True,
msg="Trying to logout now...",
new_line=True,
)
if not self._cookies:
self._logout()
logger.info(
msg="Logged out successfully.", new_line=True,
msg="Logged out successfully.",
new_line=True,
)
self._have_basic = True
if auth.get("login") == "failed":
Expand Down Expand Up @@ -192,9 +196,15 @@ def __init__(self, assets, parent):

self._mediatype = assets.get("type")
self._extension = assets.get("extension")
self._filename = "{0:03d} {1!s}".format(
parent._lecture_index, assets.get("filename")
)
title = assets.get("title", "")
if not title:
title = assets.get("filename")
if title and title.endswith(self._extension):
ok = "{0:03d} ".format(parent._lecture_index) + title
self._filename = ok
else:
ok = "{0:03d} ".format(parent._lecture_index) + assets.get("filename")
self._filename = ok
self._url = assets.get("download_url")


Expand Down

0 comments on commit f623df7

Please sign in to comment.