Skip to content

Commit

Permalink
fix: I forgot something I need to release for elfhosted bruh
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 5, 2024
1 parent 7d16482 commit 14d6ebf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
12 changes: 7 additions & 5 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ async def stream(request: Request, b64config: str, type: str, id: str):

name = metadata["d"][0 if len(metadata["d"]) == 1 else 1]["l"]
except Exception as e:
logger.warning(
f"Exception while getting metadata for {id}: {e}"
)
logger.warning(f"Exception while getting metadata for {id}: {e}")

return {
"streams": [
Expand Down Expand Up @@ -303,7 +301,9 @@ async def playback(request: Request, b64config: str, hash: str, index: str):
async with aiohttp.ClientSession() as session:
debrid = getDebrid(session, config)
download_link = await debrid.generate_download_link(hash, index)
proxy = debrid.proxy if config["debridService"] == "alldebrid" else None # proxy is not needed to proxy realdebrid stream
proxy = (
debrid.proxy if config["debridService"] == "alldebrid" else None
) # proxy is not needed to proxy realdebrid stream

if (
settings.PROXY_DEBRID_STREAM
Expand All @@ -313,7 +313,9 @@ async def playback(request: Request, b64config: str, hash: str, index: str):

async def stream_content(headers: dict):
async with aiohttp.ClientSession() as session:
async with session.get(download_link, headers=headers, proxy=proxy) as response:
async with session.get(
download_link, headers=headers, proxy=proxy
) as response:
while True:
chunk = await response.content.read(
settings.PROXY_DEBRID_STREAM_BYTES_PER_CHUNK
Expand Down
8 changes: 5 additions & 3 deletions comet/debrid/alldebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ async def check_premium(self):

return False

async def get_files(self, torrent_hashes: list, type: str, season: str, episode: str):
async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str
):
try:
get_instant = await self.session.get(
f"{self.api_url}/magnet/instant?agent={self.agent}&magnets[]={'&magnets[]='.join(hash for hash in torrent_hashes)}"
Expand All @@ -44,9 +46,9 @@ async def get_files(self, torrent_hashes: list, type: str, season: str, episode:

return {}

if not "status" in availability or availability["status"] != "success":
if "status" not in availability or availability["status"] != "success":
return {}

files = {}
for magnet in availability["data"]["magnets"]:
if not magnet["instant"]:
Expand Down
28 changes: 17 additions & 11 deletions comet/debrid/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class RealDebrid:
def __init__(self, session: aiohttp.ClientSession, debrid_api_key: str):
session.headers["Authorization"] = f"Bearer {debrid_api_key}"
self.session = session
self.proxy = None

self.api_url = "https://api.real-debrid.com/rest/1.0"

Expand Down Expand Up @@ -40,7 +41,9 @@ async def get_instant(self, hash: str):
)
return

async def get_files(self, torrent_hashes: list, type: str, season: str, episode: str):
async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str
):
tasks = []
for hash in torrent_hashes:
tasks.append(self.get_instant(hash))
Expand All @@ -53,7 +56,7 @@ async def get_files(self, torrent_hashes: list, type: str, season: str, episode:
continue

availability.update(response)

files = {}
for hash, details in availability.items():
if "rd" not in details:
Expand Down Expand Up @@ -99,44 +102,47 @@ async def generate_download_link(self, hash: str, index: str):
try:
check_blacklisted = await self.session.get("https://real-debrid.com/vpn")
check_blacklisted = await check_blacklisted.text()
proxy = None
if (
"Your ISP or VPN provider IP address is currently blocked on our website"
in check_blacklisted
):
proxy = settings.DEBRID_PROXY_URL
if not proxy:
self.proxy = settings.DEBRID_PROXY_URL
if not self.proxy:
logger.warning(
"Real-Debrid blacklisted server's IP. No proxy found."
)
else:
logger.warning(
f"Real-Debrid blacklisted server's IP. Switching to proxy {proxy} for {hash}|{index}"
f"Real-Debrid blacklisted server's IP. Switching to proxy {self.proxy} for {hash}|{index}"
)

add_magnet = await self.session.post(
f"{self.api_url}/torrents/addMagnet",
data={"magnet": f"magnet:?xt=urn:btih:{hash}"},
proxy=proxy,
proxy=self.proxy,
)
add_magnet = await add_magnet.json()

get_magnet_info = await self.session.get(add_magnet["uri"], proxy=proxy)
get_magnet_info = await self.session.get(
add_magnet["uri"], proxy=self.proxy
)
get_magnet_info = await get_magnet_info.json()

await self.session.post(
f"{self.api_url}/torrents/selectFiles/{add_magnet['id']}",
data={"files": index},
proxy=proxy,
proxy=self.proxy,
)

get_magnet_info = await self.session.get(add_magnet["uri"], proxy=proxy)
get_magnet_info = await self.session.get(
add_magnet["uri"], proxy=self.proxy
)
get_magnet_info = await get_magnet_info.json()

unrestrict_link = await self.session.post(
f"{self.api_url}/unrestrict/link",
data={"link": get_magnet_info["links"][0]},
proxy=proxy,
proxy=self.proxy,
)
unrestrict_link = await unrestrict_link.json()

Expand Down

0 comments on commit 14d6ebf

Please sign in to comment.