From 78899558f589abfac6b63f101deb4dc3d33e3e73 Mon Sep 17 00:00:00 2001 From: nichind Date: Tue, 24 Dec 2024 11:50:58 +0300 Subject: [PATCH] fix:translate(idk how i didn't notice that) --- pybalt/cobalt.py | 78 +++++++++++++++++++++++++----------------------- setup.py | 2 +- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/pybalt/cobalt.py b/pybalt/cobalt.py index 2701905..eea292e 100644 --- a/pybalt/cobalt.py +++ b/pybalt/cobalt.py @@ -39,7 +39,7 @@ def translate(self, key: str, locale: Literal["en", "ru"] = None) -> str: file = path.join(path.dirname(__file__), "locales", f"{locale}.txt") if not path.exists(file): if locale.upper() != "EN": - return self.translate_string(key, "EN") + return self.translate(key, "EN") return key with open(file) as f: for line in f.readlines(): @@ -55,7 +55,7 @@ def translate(self, key: str, locale: Literal["en", "ru"] = None) -> str: translated = translated[:-1] return translated if locale.upper() != "EN": - return self.translate_string(key, "EN") + return self.translate(key, "EN") return key @@ -170,6 +170,7 @@ def __init__( """ load_dotenv() + self.instances = [] self.api_instance = ( api_instance or getenv("COBALT_API_URL") or "https://dwnld.nichind.dev" ) @@ -207,41 +208,43 @@ async def get_instance(self): """ headers = self.headers.copy() async with ClientSession(headers=headers) as cs: - async with cs.get( - "https://instances.cobalt.best/api/instances.json" - ) as resp: - instances = await resp.json() - good_instances = [] - for instance in instances: - if ( - "version" not in instance - or int(instance["version"].split(".")[0]) < 10 - ): - continue - dead_services = sum( - 1 - for service, status in instance["services"].items() - if not status - ) - if dead_services > 7: - continue - good_instances.append(instance) - while good_instances: - good_instances.sort( - key=lambda instance: instance["score"], reverse=True - ) - next_instance = good_instances.pop(0) - try: - async with cs.get( - next_instance["protocol"] + "://" + next_instance["api"] - ) as resp: - json = await resp.json() - if json["cobalt"]["url"] in self.skipped_instances: - raise exceptions.BadInstance() - self.api_instance = json["cobalt"]["url"] - break - except Exception: - pass + if not self.instances or len(self.instances) == 0: + async with cs.get( + "https://instances.cobalt.best/api/instances.json" + ) as resp: + instances = await resp.json() + good_instances = [] + for instance in instances: + if ( + "version" not in instance + or int(instance["version"].split(".")[0]) < 10 + ): + continue + dead_services = sum( + 1 + for service, status in instance["services"].items() + if not status + ) + if dead_services > 7: + continue + good_instances.append(instance) + self.instances = good_instances + while self.instances: + self.instances.sort( + key=lambda instance: instance["score"], reverse=True + ) + next_instance = self.instances.pop(0) + try: + async with cs.get( + next_instance["protocol"] + "://" + next_instance["api"] + ) as resp: + json = await resp.json() + if json["cobalt"]["url"] in self.skipped_instances: + raise exceptions.BadInstance() + self.api_instance = json["cobalt"]["url"] + break + except Exception: + pass return self.api_instance async def get( @@ -367,6 +370,7 @@ async def get( or json["error"]["code"].split(".")[-1] == "not_found" ): + print(1) self.skipped_instances.append(self.api_instance) await self.get_instance() return await self.get( diff --git a/setup.py b/setup.py index fcc4baf..05cc900 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def readme(): setup( name="pybalt", - version="2024.12.8", + version="2024.12.9", author="nichind", author_email="nichinddev@gmail.com", description="Download mediafiles from YouTube, Twitter (X), Instagram, Reddit & more. CLI & python module for @imputnet's cobalt processing instance api.",