classMangaDexReader(BaseReader):def__init__(self)->None:self.base_url="https://api.mangadex.org"def_get_manga_info(self,title:str):try:manga_response=requests.get(f"{self.base_url}/manga",params={"title":title})manga_response.raise_for_status()manga_data=manga_response.json()["data"]iflen(manga_data):returnmanga_data[0]else:logger.warning(f"No match found for title '{title}'")returnNoneexceptrequests.exceptions.HTTPErrorashttp_error:logger.error(f"HTTP error: {http_error}")exceptrequests.exceptions.RequestExceptionasreq_error:logger.error(f"Request Error: {req_error}")returnNone# Authors and artists are combineddef_get_manga_author(self,id:str):try:author_response=requests.get(f"{self.base_url}/author",params={"ids[]":[id]})author_response.raise_for_status()returnauthor_response.json()["data"][0]exceptrequests.exceptions.HTTPErrorashttp_error:logger.error(f"HTTP error: {http_error}")exceptrequests.exceptions.RequestExceptionasreq_error:logger.error(f"Request Error: {req_error}")returnNonedef_get_manga_chapters(self,manga_id:str,lang:str):try:chapter_response=requests.get(f"{self.base_url}/manga/{manga_id}/feed",params={"translatedLanguage[]":[lang],"order[chapter]":"asc",},)chapter_response.raise_for_status()returnchapter_response.json()exceptrequests.exceptions.HTTPErrorashttp_error:logger.error(f"HTTP error: {http_error}")exceptrequests.exceptions.RequestExceptionasreq_error:logger.error(f"Request Error: {req_error}")returnNonedefload_data(self,titles:List[str],lang:str="en")->List[Document]:"""Load data from the MangaDex API. Args: title (List[str]): List of manga titles lang (str, optional): ISO 639-1 language code. Defaults to 'en'. Returns: List[Document]: A list of Documents. """result=[]fortitleintitles:manga=self._get_manga_info(title)ifnotmanga:continueauthor_name,artist_name=None,Noneforrinmanga["relationships"]:ifr["type"]=="author":author=self._get_manga_author(r["id"])author_name=author["attributes"]["name"]ifr["type"]=="artist":artist=self._get_manga_author(r["id"])artist_name=artist["attributes"]["name"]chapters=self._get_manga_chapters(manga["id"],lang)chapter_count=chapters.get("total",None)latest_chapter_published_at=Noneiflen(chapters["data"]):latest_chapter=chapters["data"][-1]latest_chapter_published_at=latest_chapter["attributes"]["publishAt"]# Get tags for the selected languagetags=[]fortaginmanga["attributes"]["tags"]:tag_name_dict=tag["attributes"]["name"]iflangintag_name_dict:tags.append(tag_name_dict[lang])doc=Document(text=manga["attributes"]["title"].get(lang,title),extra_info={"id":manga["id"],"author":author_name,"artist":artist_name,"description":manga["attributes"]["description"].get(lang,None),"original_language":manga["attributes"]["originalLanguage"],"tags":tags,"chapter_count":chapter_count,"latest_chapter_published_at":latest_chapter_published_at,},)result.append(doc)returnresult
defload_data(self,titles:List[str],lang:str="en")->List[Document]:"""Load data from the MangaDex API. Args: title (List[str]): List of manga titles lang (str, optional): ISO 639-1 language code. Defaults to 'en'. Returns: List[Document]: A list of Documents. """result=[]fortitleintitles:manga=self._get_manga_info(title)ifnotmanga:continueauthor_name,artist_name=None,Noneforrinmanga["relationships"]:ifr["type"]=="author":author=self._get_manga_author(r["id"])author_name=author["attributes"]["name"]ifr["type"]=="artist":artist=self._get_manga_author(r["id"])artist_name=artist["attributes"]["name"]chapters=self._get_manga_chapters(manga["id"],lang)chapter_count=chapters.get("total",None)latest_chapter_published_at=Noneiflen(chapters["data"]):latest_chapter=chapters["data"][-1]latest_chapter_published_at=latest_chapter["attributes"]["publishAt"]# Get tags for the selected languagetags=[]fortaginmanga["attributes"]["tags"]:tag_name_dict=tag["attributes"]["name"]iflangintag_name_dict:tags.append(tag_name_dict[lang])doc=Document(text=manga["attributes"]["title"].get(lang,title),extra_info={"id":manga["id"],"author":author_name,"artist":artist_name,"description":manga["attributes"]["description"].get(lang,None),"original_language":manga["attributes"]["originalLanguage"],"tags":tags,"chapter_count":chapter_count,"latest_chapter_published_at":latest_chapter_published_at,},)result.append(doc)returnresult