GoGoDownloader/src/bitanime.py

144 lines
5.3 KiB
Python
Raw Normal View History

2021-09-01 16:34:08 +08:00
# Dependencies
import requests
import ctypes
import os
import backend as bd
import colorama
import concurrent.futures
2021-09-01 16:34:08 +08:00
from tqdm.contrib.concurrent import thread_map
from bs4 import BeautifulSoup
from colorama import Fore
colorama.init(autoreset=True)
2021-09-01 16:23:47 +02:00
try:
ctypes.windll.kernel32.SetConsoleTitleW("BitAnime")
2021-09-02 15:25:21 +08:00
except (AttributeError):
2021-09-01 16:23:47 +02:00
pass
2021-09-01 16:34:08 +08:00
2021-09-02 15:25:21 +08:00
2021-09-01 16:34:08 +08:00
def bitanime():
again = True
while again:
print(
f""" {Fore.LIGHTBLUE_EX}
____ _ _ _ _
| __ )(_) |_ / \ _ __ (_)_ __ ___ ___
| _ \| | __| / _ \ | '_ \| | '_ ` _ \ / _ \\
| |_) | | |_ / ___ \| | | | | | | | | | __/
|____/|_|\__/_/ \_\_| |_|_|_| |_| |_|\___|
{Fore.LIGHTYELLOW_EX}
By: sh1nobu
Github: https://github.com/sh1nobuu/BitAnime
"""
)
"""
Ask user for input and then check if the anime provided exists or if not, loop
"""
check = True
while check:
name = input(f"Enter anime name >> ").lower()
if "-" in name:
title = name.replace("-", " ").title().strip()
else:
title = name.title().strip()
source = f"https://gogoanime.pe/category/{name}"
resp = requests.get(source)
if resp.status_code == 200:
print(f"{Fore.LIGHTGREEN_EX}====================================")
check = False
else:
print(
f"{Fore.LIGHTRED_EX}Error 404: Anime not found. Please try again."
)
check = True
"""
Get how many episode/s the anime has
"""
soup = BeautifulSoup(resp.content, "html.parser")
episode_number = soup.find("ul", {"id": "episode_page"})
episode_number = episode_number.get_text().split("-")[-1].strip()
2021-09-01 16:34:08 +08:00
"""
Print the anime name, episode, and the link of the anime
"""
print(f"Title: {Fore.LIGHTCYAN_EX}{title}")
print(f"Episode/s: {Fore.LIGHTCYAN_EX}{episode_number}")
print(f"Link: {Fore.LIGHTCYAN_EX}{source}")
print(f"{Fore.LIGHTGREEN_EX}====================================")
"""
Create a download folder for the anime
"""
folder = os.path.join(os.getcwd(), title)
if not os.path.exists(folder):
os.mkdir(folder)
"""
Check if the anime has episode 0 or not
"""
source = f"https://gogoanime.pe/{name}"
resp = requests.get(source)
soup = BeautifulSoup(resp.content, "html.parser")
episode_zero = soup.find("h1", {"class": "entry-title"})
if episode_zero is None:
# Episode 0 does exist
exec = concurrent.futures.ThreadPoolExecutor()
2021-09-01 16:34:08 +08:00
episode_links = bd.get_links(name, episode_number, source)
download_links = list(exec.map(bd.get_download_links, episode_links))
download_urls = list(exec.map(bd.get_download_urls, download_links))
conv_download_urls = {
episode_title: url for episode_title, url in enumerate(download_urls)
}
download_urls = sorted(set(conv_download_urls.items()))
2021-09-01 16:34:08 +08:00
print(f"Downloading {Fore.LIGHTCYAN_EX}{len(download_urls)} episode/s")
print(f"{Fore.LIGHTGREEN_EX}====================================")
print(download_urls)
print(len(download_urls))
# bd.get_path(folder)
# thread_map(
# bd.download_episodes, download_urls, ncols=75, total=len(download_urls)
# )
# try:
# os.startfile(folder)
# except (AttributeError):
# import sys, subprocess
2021-09-02 15:25:21 +08:00
# opener = "open" if sys.platform == "darwin" else "xdg-open"
# subprocess.call([opener, folder])
2021-09-01 16:34:08 +08:00
else:
# Episode 0 does not exist
exec = concurrent.futures.ThreadPoolExecutor()
2021-09-01 16:34:08 +08:00
episode_links = bd.get_links(name, episode_number)
download_links = list(exec.map(bd.get_download_links, episode_links))
download_urls = list(exec.map(bd.get_download_urls, download_links))
conv_download_urls = {
episode_title + 1: url
for episode_title, url in enumerate(download_urls)
}
download_urls = sorted(set(conv_download_urls.items()))
print(f"Downloading {Fore.LIGHTCYAN_EX}{len(download_urls)} episode/s")
2021-09-01 16:34:08 +08:00
print(f"{Fore.LIGHTGREEN_EX}====================================")
print(download_urls)
print(len(download_urls))
# bd.get_path(folder)
# thread_map(
# bd.download_episodes, download_urls, ncols=75, total=len(download_urls)
# )
# try:
# os.startfile(folder)
# except (AttributeError):
# import sys, subprocess
2021-09-02 15:25:21 +08:00
# opener = "open" if sys.platform == "darwin" else "xdg-open"
# subprocess.call([opener, folder])
2021-09-01 16:23:47 +02:00
2021-09-01 16:34:08 +08:00
use_again = input("Do you want to download other anime? (y|n) >> ").lower()
if use_again == "y":
again = True
os.system("cls")
else:
again = False
if __name__ == "__main__":
bitanime()