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

Commit

Permalink
updated code quality, fixed #552 (issue with specifying output direct…
Browse files Browse the repository at this point in the history
…ory other than current directory)
  • Loading branch information
r0oth3x49 committed Sep 15, 2020
1 parent 3b1af2a commit e258e2a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
5 changes: 1 addition & 4 deletions udemy-dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ def course_download(
lecture_number=None,
lecture_start=None,
lecture_end=None,
logs_filepath=None,
keep_vtt=False,
skip_hls_stream=False,
):
Expand All @@ -293,8 +292,6 @@ def course_download(
if "~" in path:
path = os.path.expanduser(path)
course_path = os.path.join(path, course_name)
if not logs_filepath:
logs_filepath = os.path.join(course_path, "udemy-dl.log")
chapters = course.get_chapters(
chapter_number=chapter_number,
chapter_start=chapter_start,
Expand All @@ -315,7 +312,7 @@ def course_download(
)
lectures_count = chapter.lectures
filepath = to_filepath(course_path, chapter_title)
logger.set_log_filepath(logs_filepath)
logger.set_log_filepath(course_path)
chapter_progress = (
chapter_index
if chapter_number
Expand Down
27 changes: 15 additions & 12 deletions udemy/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import sys
import logging
from udemy.compat import os, re
from colorama import init, Fore, Style
from udemy.progress import ProgressBar

Expand Down Expand Up @@ -60,17 +61,19 @@ class Logging(ProgressBar):
Custom logging class for udemy
"""

def __init__(self, log_filepath=None):
self._log_filepath = log_filepath
def __init__(self):
self._log_filepath = None

def set_log_filepath(self, log_filepath):
file_handler = logging.FileHandler(log_filepath)
self._log_filepath = log_filepath
logging.basicConfig(
format="[%(asctime)s][%(name)s] %(levelname)-5.5s %(message)s",
level=logging.INFO,
handlers=[file_handler],
)
def set_log_filepath(self, course_path):
course_path = re.sub(r'"', "", course_path.strip())
if os.path.exists(course_path):
self._log_filepath = os.path.join(course_path, "udemy-dl.log")
file_handler = logging.FileHandler(self._log_filepath)
logging.basicConfig(
format="[%(asctime)s][%(name)s] %(levelname)-5.5s %(message)s",
level=logging.INFO,
handlers=[file_handler],
)

def info(
self,
Expand Down Expand Up @@ -193,7 +196,7 @@ def failed(self, msg):
+ "] : "
)
if self._log_filepath:
log.error(msg)
log.error(f"{msg} (failed)")
msg = (
set_color(f"{msg} (", level=70)
+ set_color("failed", level=40)
Expand Down Expand Up @@ -289,7 +292,7 @@ def download_skipped(self, msg, reason=""):
+ "] : "
)
if self._log_filepath:
log.warning(msg)
log.warning(f"{msg} (download skipped)")
msg = (
set_color(f"{msg} (", level=70)
+ set_color("download skipped", level=30)
Expand Down
8 changes: 7 additions & 1 deletion udemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ def to_human_readable(content_length):


def to_filepath(base, name):
base = re.sub(r'"', "", base.strip())
filepath = os.path.join(base, name)
try:
os.makedirs(filepath)
except Exception as e:
except:
pass
return filepath

Expand Down Expand Up @@ -142,6 +143,8 @@ def to_configs(
cfq = configs.get("quality")
cfl = configs.get("language")
cfo = configs.get("output")
if cfo:
cfo = re.sub(r'"', "", cfo.strip())
if username and cfu != username:
configs.update({"username": username})
if password and cfp != password:
Expand All @@ -153,10 +156,13 @@ def to_configs(
if language and cfl != language:
configs.update({"language": language})
if output and cfo != output:
output = re.sub(r'"', "", output.strip())
configs.update({"output": output})
with open(fname, fmode) as fd:
json.dump(configs, fd, indent=4)
if not configs:
if output:
output = re.sub(r'"', "", output.strip())
creds = {
"username": username,
"password": password,
Expand Down

0 comments on commit e258e2a

Please sign in to comment.