Skip to content

Commit

Permalink
Merge pull request #265 from eggplants/fix-264
Browse files Browse the repository at this point in the history
jsonエンドポイントの `/html/body/script[@id="episode-json"]` 移行対応
  • Loading branch information
eggplants authored Oct 1, 2024
2 parents 8014be9 + c5fd4a9 commit f1a6171
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions getjump/getjump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import requests
from bs4 import BeautifulSoup
from bs4.element import Tag
from PIL import Image
from rich.progress import (
BarColumn,
Expand Down Expand Up @@ -93,13 +94,22 @@ def get( # noqa: PLR0913
self.__check_url(url)
self.login(url, username=username, password=password)

url = url if url.endswith(".json") else re.sub(r"((episode|magazine|volume)/\d+)", r"\1.json", url)
url = url[:-5] if url.endswith(".json") else url

res = self._session.get(url, headers=HEADERS)

self.__check_content_type(res.headers["content-type"])

j = res.json()["readableProduct"]
script_tag = BeautifulSoup(res.content, "html.parser").find("script", id="episode-json")
if not isinstance(script_tag, Tag):
msg = "wrong type of script element."
raise TypeError(msg)

json_value = script_tag.attrs.get("data-value", None)
if json_value is None:
msg = "json data is missing."
raise ValueError(msg)

j = json.loads(json_value)["readableProduct"]

nxt = j["nextReadableProductUri"]
nxt = self.__check_next(nxt)
Expand Down Expand Up @@ -191,8 +201,8 @@ def __check_url(self, url: str) -> None:

@staticmethod
def __check_content_type(type_: str) -> None:
if "application/json" not in type_:
msg = f"got '{type_}', expect 'application/json'. Is given URL correct?"
if "text/html" not in type_:
msg = f"got '{type_}', expect 'text/html'. Is given URL correct?"
raise TypeError(msg)

@staticmethod
Expand Down

0 comments on commit f1a6171

Please sign in to comment.