comictagger / metron_talker

Metron.cloud comictagger talker plugin
5 stars 3 forks source link

Mokkari api should be persistent #4

Closed lordwelch closed 1 year ago

lordwelch commented 1 year ago

Currently the Mokkari api is instantiated for each request. This probably allows bypassing rate limits and makes every cache lookup take longer (yes the cache builtin to comictagger has the same issue). The api object should be stored on the Talker object. It should be created in __init__ and in parse_settings and assigned to eg self.metron_api instead of here

    def _get_metron_content(
        self, endpoint: str, params: dict[str, Any] | int
    ) -> list[Series] | list[Issue] | Issue | Series | SeriesList | IssuesList:
        """Use the mokkari python library to retrieve data from Metron.cloud"""
        try:
            metron_api = mokkari.api(
                self.username,
                self.user_password,
                cache=mokkari.sqlite_cache.SqliteCache(str(self.cache_folder / "metron_cache.db"), expire=7),
                user_agent="comictagger/" + self.version,
            )
            result = getattr(metron_api, endpoint)(params)
        except mokkari.exceptions.AuthenticationError:
            logger.debug("Access denied. Invalid username or password.")
            raise TalkerNetworkError(self.name, 1, "Access denied. Invalid username or password.")
        except mokkari.exceptions.ApiError as e:
            logger.debug(f"API error: {e}")
            raise TalkerNetworkError(self.name, 1, f"API error: {e}")

        return result
mizaki commented 1 year ago

Because the GUI search is in another thread sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 139795925960512 and this is thread id 139792956237504. Maybe using the mokkari cache is not possible after all?

I presume this was correct (enough)?

    def __init__(self, version: str, cache_folder: pathlib.Path):
        super().__init__(version, cache_folder)
        # Default settings
        self.username: str = ""
        self.user_password: str = self.api_key
        self.mokkari_api = mokkari.api(
                self.username,
                self.user_password,
                cache=mokkari.sqlite_cache.SqliteCache(str(self.cache_folder / "metron_cache.db"), expire=7),
                user_agent="comictagger/" + self.version,
            )
    def parse_settings(self, settings: dict[str, Any]) -> dict[str, Any]:
        settings = super().parse_settings(settings)

        self.use_series_start_as_volume = settings["met_use_series_start_as_volume"]
        self.display_variants = settings["met_display_variants"]
        self.find_series_covers = settings["met_series_covers"]
        self.use_ongoing_issue_count = settings["met_use_ongoing"]
        self.username = settings["met_username"]
        self.user_password = settings["metron_key"]

        self.mokkari_api = mokkari.api(
            self.username,
            self.user_password,
            cache=mokkari.sqlite_cache.SqliteCache(str(self.cache_folder / "metron_cache.db"), expire=7),
            user_agent="comictagger/" + self.version,
        )

        return settings

Using CT cache doesn't present the problem.

lordwelch commented 1 year ago

I see this was done in 7469c65. I'll close this issue now