Skip to content

Commit

Permalink
feat: implement fetch for /lists api
Browse files Browse the repository at this point in the history
  • Loading branch information
tbdsux committed May 31, 2023
1 parent 947c9d9 commit b040e45
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
80 changes: 80 additions & 0 deletions api/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,83 @@ def _get_main_container(self) -> None:

def _get(self) -> None:
self._get_main_container()


class FetchList(BaseFetch):
def __init__(self, soup: BeautifulSoup, query: str, code: int, ok: bool) -> None:
super().__init__(soup, query, code, ok)

def _get_main_container(self) -> None:
container = self.soup.find("div", class_="app-body")

# get list title
header = container.find("div", class_="box-header")
self.info["title"] = header.find("h1").get_text().strip()

description = header.find("div", class_="description")
self.info["description"] = (
description.get_text().strip() if description is not None else ""
)

# get list
container_list = container.find("div", class_="collection-list")
all_items = container_list.find_all("li")
list_items = []
for i in all_items:
# parse list image
list_img_container = str(
i.find("img", class_="img-responsive")["data-src"]
).split("/1280/")
list_img = ""
if len(list_img_container) > 1:
list_img = list_img_container[1]
else:
list_img = list_img_container[0]

list_img = list_img.replace("t.jpg", "c.jpg") # replace image url to give the bigger size

list_header = i.find("h2")
list_title = list_header.find("a").get_text().strip()
list_title_rank = (
list_header.get_text().replace(list_title, "").strip().strip(".")
)
list_url = urljoin(MYDRAMALIST_WEBSITE, list_header.find("a").get("href"))
list_slug = list_header.find("a").get("href")

# parse example: `Korean Drama - 2020, 16 episodes`
list_details_container = i.find(class_="text-muted") # could be `p` or `div`
list_details_xx = list_details_container.get_text().split(",")
list_details_type = list_details_xx[0].split("-")[0].strip()
list_details_year = list_details_xx[0].split("-")[1].strip()

list_details_episodes = None
if len(list_details_xx) > 1:
list_details_episodes = int(list_details_xx[1].replace("episodes", "").strip())


# try to get description, it is missing on some lists
list_short_summary = ""
list_short_summary_container = i.find("div", class_="col-xs-12 m-t-sm")
if list_short_summary_container is not None:
list_short_summary = list_short_summary_container.get_text().replace("...more", "...").strip()


# append to list items
list_items.append(
{
"title": list_title,
"image": list_img,
"rank": list_title_rank,
"url": list_url,
"slug": list_slug,
"type": list_details_type,
"year": list_details_year,
"episodes": list_details_episodes,
"short_summary": list_short_summary
}
)

self.info["list"] = list_items

def _get(self) -> None:
self._get_main_container()
9 changes: 8 additions & 1 deletion api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ async def fetch_cast(drama_id: str, response: Response) -> Dict[str, Any]:
async def fetch_reviews(
drama_id: str, response: Response, page: int = 1
) -> Dict[str, Any]:

code, r = await fetch_func(query=f"{drama_id}/reviews?page={page}", t="reviews")

response.status_code = code
Expand All @@ -69,6 +68,14 @@ async def person(person_id: str, response: Response) -> Dict[str, Any]:
return r


@app.get("/list/{list_id}")
async def lists(list_id: str, response: Response) -> Dict[str, Any]:
code, r = await fetch_func(query=f"list/{list_id}", t="lists")

response.status_code = code
return r


# get seasonal drama list -- official api available, use it with cloudflare bypass
@app.get("/seasonal/{year}/{quarter}")
async def mdlSeasonal(year: int, quarter: int) -> Any:
Expand Down
3 changes: 2 additions & 1 deletion api/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, Any, Tuple

from api.search import Search
from api.fetch import FetchDrama, FetchPerson, FetchCast, FetchReviews
from api.fetch import FetchDrama, FetchList, FetchPerson, FetchCast, FetchReviews


def error(code: int, description: str) -> Dict[str, Any]:
Expand Down Expand Up @@ -30,6 +30,7 @@ async def search_func(query: str) -> Tuple[int, Dict[str, Any]]:
"person": FetchPerson,
"cast": FetchCast,
"reviews": FetchReviews,
"lists": FetchList,
}


Expand Down

0 comments on commit b040e45

Please sign in to comment.