Skip to content

Commit

Permalink
fix: gogo get_info fails instead of None fallback (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdaqo committed Aug 16, 2024
1 parent 3f64667 commit f0e28ac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion api/src/anipy_api/provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ProviderInfoResult:
alternative_names: List of alternative names
"""

name: str
name: Optional[str] = None
image: Optional[str] = None
genres: Optional[List[str]] = None
synopsis: Optional[str] = None
Expand Down
48 changes: 28 additions & 20 deletions api/src/anipy_api/provider/providers/gogo_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Season,
Status,
)
from anipy_api.provider.utils import parsenum, request_page
from anipy_api.provider.utils import parsenum, request_page, safe_attr

if TYPE_CHECKING:
from anipy_api.provider import Episode
Expand Down Expand Up @@ -190,27 +190,35 @@ def get_info(self, identifier: str) -> "ProviderInfoResult":
if info_body is None:
raise BeautifulSoupLocationError("anime info", res.url)

name = info_body.find("h1").text # type: ignore
image = info_body.find("img").get("src").__str__() # type: ignore
other_info = info_body.find_all("p", {"class": "type"}) # type: ignore

synopsis = info_body.find("div", {"class": "description"}).text.replace("\n", "") # type: ignore
genres = [x["title"] for x in other_info[2].find_all("a")]
status = other_info[4].text.replace("\n", "").replace("Status: ", "")
try:
status = Status[status.upper()]
except KeyError:
status = None

alternative_names = info_body.find("p", {"class": "other-name"}).find("a").text.split(",") # type: ignore

try:
release_year = int(other_info[3].text.replace("Released: ", ""))
except (ValueError, TypeError):
release_year = None
name = safe_attr(info_body.find("h1"), "text")
image = safe_attr(info_body.find("img"), "src")

alt_names = info_body.find("p", {"class": "other-name"}) # type: ignore
if alt_names is not None:
alt_names = safe_attr(alt_names.find("a"), "text").split(",") # type: ignore
synopsis = safe_attr(info_body.find("div", {"class": "description"}), "text").replace("\n", "") # type: ignore

other_info = info_body.find_all("p", {"class": "type"}) # type: ignore
status, release_year, genres = None, None, []
for i in other_info:
cat_name = safe_attr(i.find("span"), "text")

if cat_name == "Genre:":
genres = [x["title"] for x in i.find_all("a")]
elif cat_name == "Status:":
status = safe_attr(i.find("a"), "text")
try:
status = Status[status.upper()] # type: ignore
except KeyError:
status = None
elif cat_name == "Released:":
try:
release_year = int(safe_attr(i, "text").replace("Released: ", "")) # type: ignore
except (ValueError, TypeError):
release_year = None

return ProviderInfoResult(
name, image, genres, synopsis, release_year, status, alternative_names
name, image, genres, synopsis, release_year, status, alt_names
)

def get_video(
Expand Down
12 changes: 11 additions & 1 deletion api/src/anipy_api/provider/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""These are only internal utils, which are not made to be used outside"""

from typing import TYPE_CHECKING
from typing import Union, Optional

if TYPE_CHECKING:
from requests import Request, Session, Response

from bs4 import Tag, NavigableString

def request_page(session: "Session", req: "Request") -> "Response":
"""Prepare a request and send it.
Expand Down Expand Up @@ -38,3 +39,12 @@ def parsenum(n: str):
return int(n)
except ValueError:
return float(n)

def safe_attr(bs_obj: Optional[Union["Tag", "NavigableString", int]], attr: str) -> Optional[str]:
if bs_obj is None or isinstance(bs_obj, int):
return None

if attr == "text":
return bs_obj.get_text()

return bs_obj.get(attr) # type: ignore

0 comments on commit f0e28ac

Please sign in to comment.