Skip to content

Commit 945803f

Browse files
CaedenPHgithub-actions
and
github-actions
authored
Unmark fetch anime and play as BROKEN and fix type errors (#8988)
* updating DIRECTORY.md * type(fetch-anime-and-play): Fix type errors and re-enable * updating DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 5f7819e commit 945803f

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,7 @@
12131213
* [Daily Horoscope](web_programming/daily_horoscope.py)
12141214
* [Download Images From Google Query](web_programming/download_images_from_google_query.py)
12151215
* [Emails From Url](web_programming/emails_from_url.py)
1216+
* [Fetch Anime And Play](web_programming/fetch_anime_and_play.py)
12161217
* [Fetch Bbc News](web_programming/fetch_bbc_news.py)
12171218
* [Fetch Github Info](web_programming/fetch_github_info.py)
12181219
* [Fetch Jobs](web_programming/fetch_jobs.py)

web_programming/fetch_anime_and_play.py.BROKEN web_programming/fetch_anime_and_play.py

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from xml.dom import NotFoundErr
2-
31
import requests
4-
from bs4 import BeautifulSoup, NavigableString
2+
from bs4 import BeautifulSoup, NavigableString, Tag
53
from fake_useragent import UserAgent
64

75
BASE_URL = "https://ww1.gogoanime2.org"
@@ -41,25 +39,23 @@ def search_scraper(anime_name: str) -> list:
4139

4240
# get list of anime
4341
anime_ul = soup.find("ul", {"class": "items"})
42+
if anime_ul is None or isinstance(anime_ul, NavigableString):
43+
msg = f"Could not find and anime with name {anime_name}"
44+
raise ValueError(msg)
4445
anime_li = anime_ul.children
4546

4647
# for each anime, insert to list. the name and url.
4748
anime_list = []
4849
for anime in anime_li:
49-
if not isinstance(anime, NavigableString):
50-
try:
51-
anime_url, anime_title = (
52-
anime.find("a")["href"],
53-
anime.find("a")["title"],
54-
)
55-
anime_list.append(
56-
{
57-
"title": anime_title,
58-
"url": anime_url,
59-
}
60-
)
61-
except (NotFoundErr, KeyError):
62-
pass
50+
if isinstance(anime, Tag):
51+
anime_url = anime.find("a")
52+
if anime_url is None or isinstance(anime_url, NavigableString):
53+
continue
54+
anime_title = anime.find("a")
55+
if anime_title is None or isinstance(anime_title, NavigableString):
56+
continue
57+
58+
anime_list.append({"title": anime_title["title"], "url": anime_url["href"]})
6359

6460
return anime_list
6561

@@ -93,22 +89,24 @@ def search_anime_episode_list(episode_endpoint: str) -> list:
9389

9490
# With this id. get the episode list.
9591
episode_page_ul = soup.find("ul", {"id": "episode_related"})
92+
if episode_page_ul is None or isinstance(episode_page_ul, NavigableString):
93+
msg = f"Could not find any anime eposiodes with name {anime_name}"
94+
raise ValueError(msg)
9695
episode_page_li = episode_page_ul.children
9796

9897
episode_list = []
9998
for episode in episode_page_li:
100-
try:
101-
if not isinstance(episode, NavigableString):
102-
episode_list.append(
103-
{
104-
"title": episode.find("div", {"class": "name"}).text.replace(
105-
" ", ""
106-
),
107-
"url": episode.find("a")["href"],
108-
}
109-
)
110-
except (KeyError, NotFoundErr):
111-
pass
99+
if isinstance(episode, Tag):
100+
url = episode.find("a")
101+
if url is None or isinstance(url, NavigableString):
102+
continue
103+
title = episode.find("div", {"class": "name"})
104+
if title is None or isinstance(title, NavigableString):
105+
continue
106+
107+
episode_list.append(
108+
{"title": title.text.replace(" ", ""), "url": url["href"]}
109+
)
112110

113111
return episode_list
114112

@@ -140,11 +138,16 @@ def get_anime_episode(episode_endpoint: str) -> list:
140138

141139
soup = BeautifulSoup(response.text, "html.parser")
142140

143-
try:
144-
episode_url = soup.find("iframe", {"id": "playerframe"})["src"]
145-
download_url = episode_url.replace("/embed/", "/playlist/") + ".m3u8"
146-
except (KeyError, NotFoundErr) as e:
147-
raise e
141+
url = soup.find("iframe", {"id": "playerframe"})
142+
if url is None or isinstance(url, NavigableString):
143+
msg = f"Could not find url and download url from {episode_endpoint}"
144+
raise RuntimeError(msg)
145+
146+
episode_url = url["src"]
147+
if not isinstance(episode_url, str):
148+
msg = f"Could not find url and download url from {episode_endpoint}"
149+
raise RuntimeError(msg)
150+
download_url = episode_url.replace("/embed/", "/playlist/") + ".m3u8"
148151

149152
return [f"{BASE_URL}{episode_url}", f"{BASE_URL}{download_url}"]
150153

0 commit comments

Comments
 (0)