From 218eb40c40f94e6f32939cb08f180947ecf8f086 Mon Sep 17 00:00:00 2001 From: sh1nobu Date: Wed, 22 Sep 2021 17:46:42 +0800 Subject: [PATCH] Refactored --- src/bitanime.py | 81 +++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/src/bitanime.py b/src/bitanime.py index fa6edb7..b562eeb 100644 --- a/src/bitanime.py +++ b/src/bitanime.py @@ -3,14 +3,15 @@ import ctypes import os import colorama import concurrent.futures -from backend import Download, CustomError +from backend import Download, CustomMessage from tqdm.contrib.concurrent import thread_map from bs4 import BeautifulSoup from colorama import Fore colorama.init(autoreset=True) -PLUS = f"[{Fore.GREEN}+{Fore.RESET}] " -MINUS = f"[{Fore.RED}-{Fore.RESET}] {Fore.LIGHTRED_EX}" +OK = f"[{Fore.GREEN}+{Fore.RESET}] " +ERR = f"[{Fore.RED}-{Fore.RESET}] " +IN = f"[{Fore.LIGHTBLUE_EX}>{Fore.RESET}] " try: ctypes.windll.kernel32.SetConsoleTitleW("BitAnime") except (AttributeError): @@ -32,7 +33,7 @@ def bitanime(): """ ) while True: - name = input(f"{PLUS}Enter anime name > ").lower() + name = input(f"{IN}Enter anime name > ").lower() if "-" in name: title = name.replace("-", " ").title().strip() else: @@ -45,10 +46,10 @@ def bitanime(): all_episodes = int(all_episodes.get_text().split("-")[-1].strip()) break else: - print(f"{MINUS}Error 404: Anime not found. Please try again.") + print(f"{ERR}Error 404: Anime not found. Please try again.") while True: quality = input( - f"{PLUS}Enter episode quality (1.SD/360P|2.HD/720P|3.FULLHD/1080P) > " + f"{IN}Enter episode quality (1.SD/360P|2.HD/720P|3.FULLHD/1080P) > " ) if quality == "1" or quality == "": episode_quality = "SDP" @@ -60,11 +61,11 @@ def bitanime(): episode_quality = "FullHDP" break else: - print(f"{MINUS}Invalid input. Please try again.") - print(f"{PLUS}Title: {Fore.LIGHTCYAN_EX}{title}") - print(f"{PLUS}Episode/s: {Fore.LIGHTCYAN_EX}{all_episodes}") - print(f"{PLUS}Quality: {Fore.LIGHTCYAN_EX}{episode_quality}") - print(f"{PLUS}Link: {Fore.LIGHTCYAN_EX}{source}") + print(f"{ERR}Invalid input. Please try again.") + print(f"{OK}Title: {Fore.LIGHTCYAN_EX}{title}") + print(f"{OK}Episode/s: {Fore.LIGHTCYAN_EX}{all_episodes}") + print(f"{OK}Quality: {Fore.LIGHTCYAN_EX}{episode_quality}") + print(f"{OK}Link: {Fore.LIGHTCYAN_EX}{source}") folder = os.path.join(os.getcwd(), title) if not os.path.exists(folder): os.mkdir(folder) @@ -74,12 +75,12 @@ def bitanime(): if all_episodes != 1: while True: choice = input( - f"{PLUS}Do you want to download all episode? (y/n) > " + f"{IN}Do you want to download all episode? (y/n) > " ).lower() if choice in ["y", "n"]: break else: - print(f"{MINUS}Invalid input. Please try again.") + print(f"{ERR}Invalid input. Please try again.") episode_start = None episode_end = None @@ -87,24 +88,24 @@ def bitanime(): if choice == "n": while True: try: - episode_start = int(input(f"{PLUS}Episode start > ")) - episode_end = int(input(f"{PLUS}Episode end > ")) + episode_start = int(input(f"{IN}Episode start > ")) + episode_end = int(input(f"{IN}Episode end > ")) if episode_start <= 0 or episode_end <= 0: - CustomError( - f"{MINUS}episode_start or episode_end cannot be less than or equal to 0" + CustomMessage( + f"{ERR}episode_start or episode_end cannot be less than or equal to 0" ).print_error() elif episode_start >= all_episodes or episode_end > all_episodes: - CustomError( - f"{MINUS}episode_start or episode_end cannot be more than {all_episodes}" + CustomMessage( + f"{ERR}episode_start or episode_end cannot be more than {all_episodes}" ).print_error() elif episode_end <= episode_start: - CustomError( - f"{MINUS}episode_end cannot be less than or equal to episode_start" + CustomMessage( + f"{ERR}episode_end cannot be less than or equal to episode_start" ).print_error() else: break except ValueError: - print(f"{MINUS}Invalid input. Please try again.") + print(f"{ERR}Invalid input. Please try again.") episode_start = episode_start if episode_start is not None else 1 episode_end = episode_end if episode_end is not None else all_episodes @@ -116,31 +117,17 @@ def bitanime(): source = f"https://gogoanime.pe/{name}" with req.get(source) as res: soup = BeautifulSoup(res.content, "html.parser") - episode_zero = soup.find("h1", {"class": "entry-title"}) - if episode_zero is None: - # Episode 0 == 200 - with concurrent.futures.ThreadPoolExecutor() as exec: - episode_links = download.get_links( - source=source if choice == "y" else None - ) - download_links = list( - exec.map(download.get_download_links, episode_links) - ) - download_urls = list( - exec.map(download.get_download_urls, download_links) - ) - else: - # Episode 0 == 404 - with concurrent.futures.ThreadPoolExecutor() as exec: - episode_links = download.get_links() - download_links = list( - exec.map(download.get_download_links, episode_links) - ) - download_urls = list( - exec.map(download.get_download_urls, download_links) - ) + episode_zero = soup.find("h1", {"class": "entry-title"}) # value: 404 + + if choice == "n" or episode_zero is not None: + source = None + + episode_links = download.get_links(source) + with concurrent.futures.ThreadPoolExecutor() as exec: + download_links = list(exec.map(download.get_download_links, episode_links)) + download_urls = list(exec.map(download.get_download_urls, download_links)) print( - f"{PLUS}Downloading {Fore.LIGHTCYAN_EX}{len(download_urls)}{Fore.RESET} episode/s" + f"{OK}Downloading {Fore.LIGHTCYAN_EX}{len(download_urls)}{Fore.RESET} episode/s" ) thread_map( download.download_episodes, @@ -155,7 +142,7 @@ def bitanime(): opener = "open" if sys.platform == "darwin" else "xdg-open" subprocess.call([opener, folder]) - use_again = input(f"{PLUS}Do you want to use the app again? (y|n) > ").lower() + use_again = input(f"{IN}Do you want to use the app again? (y|n) > ").lower() if use_again == "y": os.system("cls") else: