Skip to content

Commit

Permalink
fix:translate(idk how i didn't notice that)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichind committed Dec 24, 2024
1 parent 674b8e1 commit 7889955
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
78 changes: 41 additions & 37 deletions pybalt/cobalt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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


Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setup(
name="pybalt",
version="2024.12.8",
version="2024.12.9",
author="nichind",
author_email="[email protected]",
description="Download mediafiles from YouTube, Twitter (X), Instagram, Reddit & more. CLI & python module for @imputnet's cobalt processing instance api.",
Expand Down

0 comments on commit 7889955

Please sign in to comment.