Example #1
0
    def test_metadata_case_1(self):
        title = 'Absolute Boyfriend'

        self.CURRENTLY_PENDING_DB_SEARCH.append(title)

        self.MangaTaggerLib_AppSettings.mode_settings = { 'write_comicinfo': False }

        with open(Path(self.data_dir, title, self.data_file), encoding='utf-8') as data:
            jikan_details = json.load(data)

        with open(Path(self.data_dir, title, self.staff_file), encoding='utf-8') as data:
            anilist_details = json.load(data)

        expected_manga_metadata = Metadata(title, {}, jikan_details, anilist_details)
        actual_manga_metadata = metadata_tagger(title, '001', {})

        self.assertEqual(expected_manga_metadata.test_value(), actual_manga_metadata.test_value())
Example #2
0
    def test_comicinfo_xml_creation_case_3(self):
        title = 'G-Maru Edition'

        self.MangaTaggerLib_AppSettings.mode_settings = {}

        with open(Path(self.data_dir, title, self.data_file), encoding='utf-8') as data:
            jikan_details = json.load(data)

        with open(Path(self.data_dir, title, self.staff_file), encoding='utf-8') as data:
            anilist_details = json.load(data)

        manga_metadata = Metadata(title, {}, jikan_details, anilist_details)

        self.assertTrue(construct_comicinfo_xml(manga_metadata, '001', {}))
def metadata_tagger(manga_title,
                    manga_chapter_number,
                    manga_chapter_title,
                    logging_info,
                    manga_file_path=None,
                    old_file_path=None):
    manga_search = None
    db_exists = False

    LOG.info(f'Table search value is "{manga_title}"', extra=logging_info)

    for x in range(4):
        manga_search = dbSearch(manga_title, x)
        if manga_search is not None:
            db_exists = True
            break
    # Metadata already exists
    if db_exists:
        if re.sub(r"[$.]", "_",
                  manga_title) in ProcSeriesTable.processed_series:
            LOG.info(f'Found an entry in manga_metadata for "{manga_title}".',
                     extra=logging_info)
        else:
            LOG.info(
                f'Found an entry in manga_metadata for "{manga_title}"; unlocking series for processing.',
                extra=logging_info)
            ProcSeriesTable.processed_series.add(
                re.sub(r"[$.]", "_", manga_title))
            CURRENTLY_PENDING_DB_SEARCH.remove(manga_title)

        manga_metadata = Metadata(manga_title,
                                  logging_info,
                                  db_details=manga_search)
        logging_info['metadata'] = manga_metadata.__dict__
    # Get metadata
    else:
        # sources["Kitsu"] = Kitsu
        results = {}
        metadata = None
        try:
            results["MAL"] = sources["MAL"].search('manga', manga_title)
        except:
            results["MAL"] = []
            pass
        results["AniList"] = sources["AniList"].search(manga_title,
                                                       logging_info)
        results["MangaUpdates"] = sources["MangaUpdates"].search(manga_title)
        results["NHentai"] = sources["NHentai"].search(manga_title)
        results["Fakku"] = sources["Fakku"].search(manga_title)
        try:
            for source in preferences:
                for result in results[source]:
                    if source == "AniList":
                        # Construct Anilist XML
                        titles = [
                            x[1] for x in result["title"].items()
                            if x[1] is not None
                        ]
                        [titles.append(x) for x in result["synonyms"]]
                        for title in titles:
                            if compare(manga_title, title) >= 0.9:
                                manga = sources["AniList"].manga(
                                    result["id"], logging_info)
                                manga["source"] = "AniList"
                                metadata = Data(manga, manga_title)
                                raise MangaMatchedException("Found a match")
                    elif source == "MangaUpdates":
                        # Construct MangaUpdates XML
                        if compare(manga_title, result['title']) >= 0.9:
                            manga = sources["MangaUpdates"].series(
                                result["id"])
                            manga["source"] = "MangaUpdates"
                            metadata = Data(manga, manga_title, result["id"])
                            raise MangaMatchedException("Found a match")
                    elif source == "MAL":
                        if compare(manga_title, result['title']) >= 0.9:
                            try:
                                manga = sources["MAL"].manga(result["mal_id"])
                            except (APIException, ConnectionError) as e:
                                LOG.warning(e, extra=logging_info)
                                LOG.warning(
                                    'Manga Tagger has unintentionally breached the API limits on Jikan. Waiting 60s to clear '
                                    'all rate limiting limits...')
                                time.sleep(60)
                                manga = sources["MAL"].manga(result["mal_id"])
                            manga["source"] = "MAL"
                            metadata = Data(manga, manga_title,
                                            result["mal_id"])
                            raise MangaMatchedException("Found a match")
                    elif source == "Fakku":
                        if result["success"]:
                            manga = sources["Fakku"].manga(result["url"])
                            manga["source"] = "Fakku"
                            metadata = Data(manga, manga_title)
                            raise MangaMatchedException("Found a match")
                    elif source == "NHentai":
                        filenametoolong = False
                        if len(old_file_path.absolute().__str__()) == 259:
                            if fuzz.partial_ratio(manga_title,
                                                  result["title"]) == 100:
                                filenametoolong = True
                        if compare(manga_title,
                                   result["title"]) >= 0.8 or filenametoolong:
                            manga = sources["NHentai"].manga(
                                result["id"], result["title"])
                            manga["source"] = "NHentai"
                            metadata = Data(manga, manga_title, result["id"])
                            raise MangaMatchedException("Found a match")
            formats = [(r"(\w)([A-Z])", r"\1 \2"), (r"[ ][,]", ","),
                       (r"[.]", ""), (r"([^ ]+)[']([^ ]+)", ""),
                       (r"([^ ]+)[.]([^ ]+)", ""), (r"[ ][-]([^ ]+)", r" \1")]
            for x in range(len(formats)):
                combinations = itertools.combinations(formats, x + 1)
                for y in combinations:
                    for z in y:
                        formatted = manga_title
                        formatted = re.sub(z[0], z[1], formatted)
                        formattedresults = sources["NHentai"].search(formatted)
                        for formattedresult in formattedresults:
                            if compare(manga_title,
                                       formattedresult["title"]) >= 0.8:
                                manga = sources["NHentai"].manga(
                                    formattedresult["id"],
                                    formattedresult["title"])
                                manga["source"] = "NHentai"
                                metadata = Data(manga, manga_title,
                                                formattedresult["id"])
                                raise MangaMatchedException("Found a match")
            raise MangaNotFoundError(manga_title)
        except MangaNotFoundError as mnfe:
            LOG.exception(mnfe, extra=logging_info)
            raise
        except MangaMatchedException:
            pass

        manga_metadata = Metadata(manga_title,
                                  logging_info,
                                  details=metadata.toDict())
        logging_info['metadata'] = manga_metadata.__dict__

        if AppSettings.mode_settings is None or (
                'database_insert' in AppSettings.mode_settings.keys()
                and AppSettings.mode_settings['database_insert']):
            MetadataTable.insert(manga_metadata, logging_info)

        LOG.info(
            f'Retrieved metadata for "{manga_title}" from the Anilist and MyAnimeList APIs; '
            f'now unlocking series for processing!',
            extra=logging_info)
        ProcSeriesTable.processed_series.add(re.sub(r"[$.]", "_", manga_title))
        CURRENTLY_PENDING_DB_SEARCH.remove(manga_title)

    if AppSettings.mode_settings is None or (
            'write_comicinfo' in AppSettings.mode_settings.keys()
            and AppSettings.mode_settings['write_comicinfo']):
        manga_metadata.title = manga_chapter_title
        comicinfo_xml = construct_comicinfo_xml(manga_metadata,
                                                manga_chapter_number,
                                                logging_info)
        reconstruct_manga_chapter(comicinfo_xml[0], manga_file_path,
                                  comicinfo_xml[1], logging_info)

    return manga_metadata