Skip to content

Update fetch_anime_and_play.py #11658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion web_programming/fetch_anime_and_play.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

Check failure on line 1 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

web_programming/fetch_anime_and_play.py:1:8: F401 `os` imported but unused
import subprocess
import requests
from bs4 import BeautifulSoup, NavigableString, Tag
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

BASE_URL = "https://ww1.gogoanime2.org"

Check failure on line 7 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

web_programming/fetch_anime_and_play.py:1:1: I001 Import block is un-sorted or un-formatted
Expand Down Expand Up @@ -39,7 +41,7 @@

# get list of anime
anime_ul = soup.find("ul", {"class": "items"})
if anime_ul is None or isinstance(anime_ul, NavigableString):

Check failure on line 44 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:44:49: F821 Undefined name `NavigableString`
msg = f"Could not find and anime with name {anime_name}"
raise ValueError(msg)
anime_li = anime_ul.children
Expand All @@ -47,12 +49,12 @@
# for each anime, insert to list. the name and url.
anime_list = []
for anime in anime_li:
if isinstance(anime, Tag):

Check failure on line 52 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:52:30: F821 Undefined name `Tag`
anime_url = anime.find("a")
if anime_url is None or isinstance(anime_url, NavigableString):

Check failure on line 54 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:54:59: F821 Undefined name `NavigableString`
continue
anime_title = anime.find("a")
if anime_title is None or isinstance(anime_title, NavigableString):

Check failure on line 57 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:57:63: F821 Undefined name `NavigableString`
continue

anime_list.append({"title": anime_title["title"], "url": anime_url["href"]})
Expand Down Expand Up @@ -91,19 +93,19 @@

# With this id. get the episode list.
episode_page_ul = soup.find("ul", {"id": "episode_related"})
if episode_page_ul is None or isinstance(episode_page_ul, NavigableString):

Check failure on line 96 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:96:63: F821 Undefined name `NavigableString`
msg = f"Could not find any anime eposiodes with name {anime_name}"
raise ValueError(msg)
episode_page_li = episode_page_ul.children

episode_list = []
for episode in episode_page_li:
if isinstance(episode, Tag):

Check failure on line 103 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:103:32: F821 Undefined name `Tag`
url = episode.find("a")
if url is None or isinstance(url, NavigableString):

Check failure on line 105 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:105:47: F821 Undefined name `NavigableString`
continue
title = episode.find("div", {"class": "name"})
if title is None or isinstance(title, NavigableString):

Check failure on line 108 in web_programming/fetch_anime_and_play.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

web_programming/fetch_anime_and_play.py:108:51: F821 Undefined name `NavigableString`
continue

episode_list.append(
Expand Down Expand Up @@ -154,6 +156,18 @@
return [f"{BASE_URL}{episode_url}", f"{BASE_URL}{download_url}"]


def download_video(download_url: str, output_filename: str) -> None:
"""Download video using ffmpeg."""
command = ["ffmpeg", "-i", download_url, output_filename]
try:
subprocess.run(command, check=True)
except FileNotFoundError:
# If ffmpeg is not found, instruct the user on how to install it
print("Error: ffmpeg is not installed.")
print("Please install ffmpeg using the following command:")
print("brew install ffmpeg") # This is the command for macOS


if __name__ == "__main__":
anime_name = input("Enter anime name: ").strip()
anime_list = search_scraper(anime_name)
Expand Down Expand Up @@ -186,3 +200,21 @@
episode_url, download_url = get_anime_episode(chosen_episode["url"])
print(f"\nTo watch, ctrl+click on {episode_url}.")
print(f"To download, ctrl+click on {download_url}.")

# Add an option to download or not
download_choice = (
input("\nDo you want to download this episode? (yes/no): ")
.strip()
.lower()
)
if download_choice in ["yes", "y"]:
output_filename = f"{chosen_anime['title']} - {chosen_episode['title']}.mp4" # Change extension as needed
download_video(download_url, output_filename)
print(
f"{chosen_episode['title']} has been downloaded as {output_filename}."
)
else:
print("Download skipped.")

# if error download please install ffmeg
# brew install ffmpeg for mac
Loading