diff --git a/addic7ed/__init__.py b/addic7ed/__init__.py index c9e440f..d8ae4e3 100644 --- a/addic7ed/__init__.py +++ b/addic7ed/__init__.py @@ -4,7 +4,7 @@ from .file_crawler import FileCrawler from .logger import init_logger from .config import Config - +from .error_reporting import friendly_msg def addic7ed(): try: @@ -14,7 +14,11 @@ def addic7ed(): except (EOFError, KeyboardInterrupt, SystemExit): print(colored("\nBye!", "yellow")) exit(0) - + except Exception as e: + print(colored(e, "red"), + end="\n\n") + friendly_msg(e) + exit(0) def main(): crawler = FileCrawler() @@ -44,16 +48,12 @@ def main(): end="\n\n") continue - try: - if Config.rename != "sub": - filename = subs[int(version)].download(ep.dir) - if filename and Config.rename == "video": - print(ep.rename(filename)) - else: - filename = subs[int(version)] \ - .download(ep.dir, "%s.srt" % ep.filename) - print(colored("Downloaded %s subtitle file" % - filename, "green"), end="\n\n") - except Exception as e: - print(colored(e, "red"), - end="\n\n") + if Config.rename != "sub": + filename = subs[int(version)].download(ep.dir) + if filename and Config.rename == "video": + print(ep.rename(filename)) + else: + filename = subs[int(version)] \ + .download(ep.dir, "%s.srt" % ep.filename) + print(colored("Downloaded %s subtitle file" % + filename, "green"), end="\n\n") diff --git a/addic7ed/error_reporting.py b/addic7ed/error_reporting.py new file mode 100644 index 0000000..473276d --- /dev/null +++ b/addic7ed/error_reporting.py @@ -0,0 +1,22 @@ +from addic7ed.parser import IncompleteError +from termcolor import colored + +def friendly_msg(e): + + if e.__class__ == ValueError: + print(colored('please enter numbers instead of words', 'yellow')) + + elif e.__class__ == IncompleteError: + print(colored('please select another subtitle', 'yellow')) + + elif e.__class__ == IOError: + print(colored('error related with file or user input/output', 'yellow')) + + elif e.__class__ == IndexError: + print(colored('please input a correct number', 'yellow')) + + elif e.__class__ == PermissionError: + print(colored('check permissions of this directory', 'yellow')) + + else: + print(colored('look for the error at: https://docs.python.org/2/library/exceptions.html', 'yellow')) diff --git a/addic7ed/file_crawler.py b/addic7ed/file_crawler.py index a5cfda6..3fe30db 100644 --- a/addic7ed/file_crawler.py +++ b/addic7ed/file_crawler.py @@ -50,14 +50,11 @@ def __init__(self, f, serie, season, episode, group): self.filename, self.ext = os.path.splitext(os.path.basename(f)) def rename(self, new_name): - try: - os.rename("%s/%s%s" % (self.dir, self.filename, self.ext), - "%s/%s%s" % (self.dir, new_name, self.ext)) - ret = colored("Renamed %s to %s" % (self.filename, new_name), - "green") - self.filename = new_name - except Exception as e: - ret = colored(e, "red") + os.rename("%s/%s%s" % (self.dir, self.filename, self.ext), + "%s/%s%s" % (self.dir, new_name, self.ext)) + ret = colored("Renamed %s to %s" % (self.filename, new_name), + "green") + self.filename = new_name return ret