def find_id_best_match(title: str, year: int) -> Optional[int]: media_id = None # logger.warning('[ANILIST] Searching AniList for title: %s' % (title)) match_title = clean_title(title) match_year = str(year) list_items = search_by_name(title) if list_items: for item in list_items: if item[0] is not None and item[0].media: for media_item in item[0].media: title_english = "" title_english_for_matching = "" title_romaji = "" title_romaji_for_matching = "" synonyms = "" synonyms_for_matching = "" started_year = "" if hasattr(media_item.title, "english" ) and media_item.title.english is not None: title_english = media_item.title.english title_english_for_matching = clean_title(title_english) if hasattr( media_item.title, "romaji") and media_item.title.romaji is not None: title_romaji = media_item.title.romaji title_romaji_for_matching = clean_title(title_romaji) if hasattr(media_item.startDate, "year"): started_year = str(media_item.startDate.year) # logger.info('Comparing AniList: %s | %s[%s] <===> %s[%s]' % (title_english, title_romaji, started_year, match_title, match_year)) if (match_title == title_english_for_matching and match_year == started_year): media_id = media_item.id logger.warning( f"[ANILIST] Found match: {title_english} [{media_id}]" ) break if (match_title == title_romaji_for_matching and match_year == started_year): media_id = media_item.id logger.warning( f"[ANILIST] Found match: {title_romaji} [{media_id}]" ) break if hasattr(media_item, "synonyms") and media_item.synonyms is not None: for synonym in media_item.synonyms: synonyms = synonym synonyms_for_matching = clean_title(synonyms) if (match_title == synonyms_for_matching and match_year == started_year): media_id = media_item.id logger.warning( f"[ANILIST] Found match in synonyms: {synonyms} [{media_id}]" ) break if (match_title == title_romaji_for_matching and match_year != started_year): logger.info( f"[ANILIST] Found match however started year is a mismatch: {title_romaji} [AL: {started_year} <==> Plex: {match_year}] " ) elif (match_title == title_english_for_matching and match_year != started_year): logger.info( f"[ANILIST] Found match however started year is a mismatch: {title_english} [AL: {started_year} <==> Plex: {match_year}] " ) if media_id is None: logger.error(f"[ANILIST] No match found for title: {title}") return media_id
def find_id_season_best_match(title: str, season: int, year: int) -> Optional[int]: media_id = None # logger.warning('[ANILIST] Searching AniList for title: %s | season: %s' % (title, season)) match_title = clean_title(title) match_year = int(year) match_title_season_suffix1 = f"{match_title} {int_to_roman_numeral(season)}" match_title_season_suffix2 = f"{match_title} season {season}" match_title_season_suffix3 = f"{match_title} part {season}" match_title_season_suffix4 = f"{match_title} {season}" # oridinal season (1st 2nd etc..) try: p_engine = inflect.engine() match_title_season_suffix5 = f"{match_title} {p_engine.ordinal(season)} season" except BaseException: logger.error( "Error while converting season to ordinal string, make sure Inflect pip package is installed" ) match_title_season_suffix5 = match_title_season_suffix2 # oridinal season - variation 1 (1st 2nd Thread) - see AniList ID: 21000 try: p_engine = inflect.engine() match_title_season_suffix6 = f"{match_title} {p_engine.ordinal(season)} thread" except BaseException: logger.error( "Error while converting season to ordinal string, make sure Inflect pip package is installed" ) match_title_season_suffix6 = match_title_season_suffix2 potential_titles = [ match_title_season_suffix1.lower().strip(), match_title_season_suffix2.lower().strip(), match_title_season_suffix3.lower().strip(), match_title_season_suffix4.lower().strip(), match_title_season_suffix5.lower().strip(), match_title_season_suffix6.lower().strip(), ] list_items = search_by_name(title) if list_items: for item in list_items: if item[0] is not None and item[0].media: for media_item in item[0].media: title_english = "" title_english_for_matching = "" title_romaji = "" title_romaji_for_matching = "" started_year = "" if hasattr(media_item.title, "english" ) and media_item.title.english is not None: title_english = media_item.title.english title_english_for_matching = clean_title(title_english) if hasattr( media_item.title, "romaji") and media_item.title.romaji is not None: title_romaji = media_item.title.romaji title_romaji_for_matching = clean_title(title_romaji) if hasattr( media_item.startDate, "year") and media_item.startDate.year is not None: started_year = int(media_item.startDate.year) else: logger.warning( "[ANILIST] Anilist series did not have year attribute so skipping this result and moving to next: " f"{title_english} | {title_romaji}") continue for potential_title in potential_titles: potential_title = clean_title(potential_title) # logger.info('Comparing AniList: %s | %s[%s] <===> %s' % # (title_english_for_matching, title_romaji_for_matching, started_year, potential_title)) if title_english_for_matching == potential_title: if started_year < match_year: logger.warning( f"[ANILIST] Found match: {title_english} [{media_id}] | " f"skipping as it was released before first season ({started_year} <==> {match_year})" ) else: media_id = media_item.id logger.info( f"[ANILIST] Found match: {title_english} [{media_id}]" ) break if title_romaji_for_matching == potential_title: if started_year < match_year: logger.warning( f"[ANILIST] Found match: {title_romaji} [{media_id}] | " f"skipping as it was released before first season ({started_year} <==> {match_year})" ) else: media_id = media_item.id logger.info( f"[ANILIST] Found match: {title_romaji} [{media_id}]" ) break if media_id == 0: logger.error(f"[ANILIST] No match found for title: {title}") return media_id