From c4a265d1682afe6ea3bb6c17981920f66749937c Mon Sep 17 00:00:00 2001 From: sh1nobu Date: Thu, 9 Sep 2021 20:34:23 +0800 Subject: [PATCH] changed some code in the bitanime and backend file to add a dynamic episode quality feature --- src/backend.py | 76 ++++++++++++++++++++++++++++++++++++++----------- src/bitanime.py | 49 +++++++++++++++++++++++++++---- 2 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/backend.py b/src/backend.py index f532456..c21b7e6 100644 --- a/src/backend.py +++ b/src/backend.py @@ -40,26 +40,70 @@ def get_download_links(episode_link): link = soup.find("li", {"class": "dowloads"}) return link.a.get("href") else: - pass + return None def get_download_urls(download_link): - link = requests.get(download_link) + link = requests.get(download_link[1]) soup = BeautifulSoup(link.content, "html.parser") - link = soup.find_all("div", {"class": "dowload"}) - return link[0].a.get("href") + link = soup.find("div", {"class": "mirror_link"}).find_all( + "div", {"class": "dowload"} + ) + try: + if len(link) > 5: + return [ + download_link[1].split("+")[-1], + link[int(download_link[0]) + 1].a.get("href"), + ] + else: + return [ + download_link[1].split("+")[-1], + link[int(download_link[0])].a.get("href"), + ] + except IndexError: + return [download_link[1].split("+")[-1], link[0].a.get("href")] def download_episodes(url): - header = { - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0", - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", - "Accept-Language": "en-US,en;q=0.5", - "Accept-Encoding": "gzip, deflate", - "Connection": "close", - } - url_resp = requests.get(url, headers=header, stream=True) - episode_name = f"{url.split('-')[-2]}.mp4" - file_name = os.path.join(folder_path, episode_name) - with open(file_name, "wb") as file: - shutil.copyfileobj(url_resp.raw, file) + if "cdn" in url[1]: + header = { + "Host": "gogo-cdn.com", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.5", + "Accept-Encoding": "gzip, deflate", + "Referer": "https://streamani.io/", + "Upgrade-Insecure-Requests": "1", + "Sec-Fetch-Dest": "document", + "Sec-Fetch-Mode": "navigate", + "Sec-Fetch-Site": "cross-site", + "Sec-Fetch-User": "?1", + "Te": "trailers", + "Connection": "close", + } + url_resp = requests.get(url[1], headers=header, stream=True) + episode_name = f"{url[0]}.mp4" + file_name = os.path.join(folder_path, episode_name) + with open(file_name, "wb") as file: + shutil.copyfileobj(url_resp.raw, file) + else: + header = { + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.5", + "Accept-Encoding": "gzip, deflate", + "Connection": "close", + } + url_resp = requests.get(url, headers=header, stream=True) + episode_name = f"{url[0]}.mp4" + file_name = os.path.join(folder_path, episode_name) + with open(file_name, "wb") as file: + shutil.copyfileobj(url_resp.raw, file) + # url_resp = requests.get(url, headers=header, stream=True) + # try: + # episode_name = f"{url.split('/')[-1].split('-')[-2]}.mp4" + # except IndexError: + # episode_name = f"{url_resp.url.split('/')[-1].split('.')[-3]}.mp4" + # file_name = os.path.join(folder_path, episode_name) + # with open(file_name, "wb") as file: + # shutil.copyfileobj(url_resp.raw, file) diff --git a/src/bitanime.py b/src/bitanime.py index bb1b48e..9e22e90 100644 --- a/src/bitanime.py +++ b/src/bitanime.py @@ -1,5 +1,7 @@ # Dependencies +# DYNAMIC EPISODE QUALITY - WIP + import requests import ctypes import os @@ -37,7 +39,7 @@ def bitanime(): """ check = True while check: - name = input(f"Enter anime name >> ").lower() + name = input("Enter anime name >> ").lower() if "-" in name: title = name.replace("-", " ").title().strip() else: @@ -45,13 +47,35 @@ def bitanime(): source = f"https://gogoanime.pe/category/{name}" resp = requests.get(source) if resp.status_code == 200: - print(f"{Fore.LIGHTGREEN_EX}===========================================") + print( + f"{Fore.LIGHTGREEN_EX}===========================================" + ) check = False else: print( f"{Fore.LIGHTRED_EX}Error 404: Anime not found. Please try again." ) check = True + check = True + while check: + quality = input("Enter episode quality >> ") + episode_quality = "" + if quality == "1" or quality == "": + episode_quality = "360P" + quality = "1" + check = False + elif quality == "2": + episode_quality = "480P" + check = False + elif quality == "3": + episode_quality = "720P" + check = False + elif quality == "4": + episode_quality = "1080P" + check = False + else: + print(f"{Fore.LIGHTRED_EX}Invalid input. Please try again.") + check = True """ Get how many episode/s the anime has """ @@ -63,6 +87,7 @@ def bitanime(): """ print(f"Title: {Fore.LIGHTCYAN_EX}{title}") print(f"Episode/s: {Fore.LIGHTCYAN_EX}{episode_number}") + print(f"Quality: {Fore.LIGHTCYAN_EX}{episode_quality}") print(f"Link: {Fore.LIGHTCYAN_EX}{source}") print(f"{Fore.LIGHTGREEN_EX}===========================================") """ @@ -83,8 +108,14 @@ def bitanime(): exec = concurrent.futures.ThreadPoolExecutor() episode_links = bd.get_links(name, episode_number) download_links = list(exec.map(bd.get_download_links, episode_links)) - filtered_download_links = [download_link for download_link in download_links if download_link] - download_urls = list(exec.map(bd.get_download_urls, filtered_download_links)) + filtered_download_links = [ + [quality, download_link] + for download_link in download_links + if download_link + ] + download_urls = list( + exec.map(bd.get_download_urls, filtered_download_links) + ) print(f"Downloading {Fore.LIGHTCYAN_EX}{len(download_urls)} episode/s") print(f"{Fore.LIGHTGREEN_EX}===========================================") bd.get_path(folder) @@ -104,8 +135,14 @@ def bitanime(): exec = concurrent.futures.ThreadPoolExecutor() episode_links = bd.get_links(name, episode_number) download_links = list(exec.map(bd.get_download_links, episode_links)) - filtered_download_links = [download_link for download_link in download_links if download_link] - download_urls = list(exec.map(bd.get_download_urls, filtered_download_links)) + filtered_download_links = [ + [quality, download_link] + for download_link in download_links + if download_link + ] + download_urls = list( + exec.map(bd.get_download_urls, filtered_download_links) + ) print(f"Downloading {Fore.LIGHTCYAN_EX}{len(download_urls)} episode/s") print(f"{Fore.LIGHTGREEN_EX}===========================================") bd.get_path(folder)