"""This is the Synchronus version of `KunYu()` class.This module contains the **SyncKunYu()** class, which acts as the primary entry pointfor interacting with `Myanimelist <https://myanimelist.net/>`__ data, as well as helper classes and functions."""__all__=["SyncKunYu"]fromtypingimportDict,Optional,Listimporthttpxfrom._modelimportAnime,Characterfrom.sync_malscraperimportSyncMalScraper
[docs]classSyncKunYu:""" The main Class for interacting with MAL Synchronusly. """
[docs]def__init__(self,use_cache:bool=False,db_path:str="cache.db",timeout:int=10)->None:""" If you want to cache the data locally pass ``use_cache=True`` and specify the database path where you want to cache ``db_path='cache.db'.`` It uses `sqlite3` for storing data. Args: use_cache (bool): If data should be cached. (Default: False) db_path: (str): The path of the database. (Default: cache.db) """self._shared_client:Optional[httpx.Client]=Noneself.use_cache=use_cacheself.db_path=db_pathself.timeout=timeoutself._Scraper=SyncMalScraper(client=self._shared_client,use_cache=self.use_cache,db_path=self.db_path,timeout=self.timeout,)
def__enter__(self):self._shared_client=httpx.Client(headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Language":"en-US,en;q=0.5","Connection":"keep-alive","Upgrade-Insecure-Requests":"1"})returnselfdef__exit__(self,exc_type,exc_val,exc_tb):ifself._shared_client:self._shared_client.close()
[docs]defsearch_anime(self,anime_name:str)->Anime:""" Fetches and Returns Anime details from myanimelist. Args: anime_name (str): Name of the anime you want to search. Returns: Anime: Returns Anime object with anime details. Example: >>> from AnimeScraper import SyncKunYu >>> scraper = SyncKunYu() >>> anime = scraper.search_anime("dr stone") >>> print(anime.title) """withself._Scraperasscraper:anime=scraper.search_anime(anime_name)returnanime
[docs]defsearch_character(self,character_name:str)->Character:""" Fetches and Returns Character details from myanimelist. Args: character_name (str): Name of the Character you want to search. Returns: Character: Returns Character object with the character details. Example: >>> from AnimeScraper import SyncKunYu >>> with SyncKunYu() as scraper: >>> character = scraper.search_character("Togashi Yuuta") >>> print(character.name) Notes: You can use ``with SyncKunYu`` context manager for same session use. """withself._Scraperasscraper:character=scraper.search_character(character_name)returncharacter
[docs]defget_anime(self,anime_id:str)->Anime:""" Fetches anime details from MyAnimeList. Args: anime_id (str): The ID of the anime. Returns: Anime: An object containing anime details. """withself._Scraperasscraper:anime=scraper.get_anime(anime_id)returnanime
[docs]defget_character(self,character_id:str)->Character:""" Fetches character details from MyAnimeList. Args: character_id (str): The ID of the character. Returns: Character: An object containing character details. """withself._Scraperasscraper:character=scraper.get_character(character_id)returncharacter
[docs]defget_batch_anime(self,anime_ids:List[str])->List[Anime]:""" Fetches multiple anime from the list of anime id. Args: anime_ids (List[str]): A list of anime id. (anime ID from Myanimelist) Returns: List[Anime]: A list of Anime object containing anime details. """withself._Scraperasscraper:anime=scraper.get_batch_anime(anime_ids)returnanime
[docs]defget_batch_character(self,character_ids:List[str])->List[Character]:""" Fetches multiple character from the list of character id. Args: character_ids (List[str]): A list of character id. (character ID from Myanimelist) Returns: List[Character]: A list of Character object containing character details. """withself._Scraperasscraper:characters=scraper.get_batch_character(character_ids)returncharacters
[docs]defsearch_batch_anime(self,anime_names:List[str])->List[Anime]:""" Fetches anime details in batch. Args: anime_names (List(str)): List of anime names. Returns: List[Anime]: A list of Anime objects with Anime details. """withself._Scraperasscraper:anime_list=scraper.search_batch_anime(anime_names)returnanime_list
[docs]defsearch_batch_character(self,character_names:List[str])->List[Character]:""" Fetches character details in batch. Args: character_names (List(str)): List of characters name. Returns: List[Character]: A list of Character objects with character's details. """withself._Scraperasscraper:batch_characters=scraper.search_batch_character(character_names)returnbatch_characters
[docs]deftop_anime_list(self,sort_by:str|None=None)->List[Dict[str,str]]:""" Fetches Top Anime List From MAL. returns top anime list by popularity, rates etc. Args: sort_by (str): Sort by 'bypopularity', 'favorite', 'tv', 'movie', 'airing', 'upcoming', 'ova' etc. Returns: List[Dict[str, str]]: Returns a list/array of dictionary with anime name, img, url """withself._Scraperasscraper:topAnime=scraper.top_anime(sort_by)returntopAnime