Skip to content

Commit

Permalink
fix: alldebrid stream proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 5, 2024
1 parent 8c77727 commit 1502d4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ 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

if (
settings.PROXY_DEBRID_STREAM
Expand All @@ -312,7 +313,7 @@ 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) 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 All @@ -331,7 +332,7 @@ async def stream_content(headers: dict):
range = f"bytes={start}-{end}"

async with await session.get(
download_link, headers={"Range": f"bytes={start}-{end}"}
download_link, headers={"Range": f"bytes={start}-{end}"}, proxy=proxy
) as response:
await session.close()

Expand Down
14 changes: 7 additions & 7 deletions comet/debrid/alldebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AllDebrid:
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 = "http://api.alldebrid.com/v4"
self.agent = "comet"
Expand Down Expand Up @@ -91,33 +92,32 @@ async def generate_download_link(self, hash: str, index: str):
f"{self.api_url}/magnet/upload?agent=comet&magnets[]={hash}"
)
check_blacklisted = await check_blacklisted.text()
proxy = None
if "NO_SERVER" in check_blacklisted:
proxy = settings.DEBRID_PROXY_URL
if not proxy:
self.proxy = settings.DEBRID_PROXY_URL
if not self.proxy:
logger.warning(
"All-Debrid blacklisted server's IP. No proxy found."
)
else:
logger.warning(
f"All-Debrid blacklisted server's IP. Switching to proxy {proxy} for {hash}|{index}"
f"All-Debrid blacklisted server's IP. Switching to proxy {self.proxy} for {hash}|{index}"
)

upload_magnet = await self.session.get(
f"{self.api_url}/magnet/upload?agent=comet&magnets[]={hash}",
proxy=proxy,
proxy=self.proxy,
)
upload_magnet = await upload_magnet.json()

get_magnet_status = await self.session.get(
f"{self.api_url}/magnet/status?agent=comet&id={upload_magnet['data']['magnets'][0]['id']}",
proxy=proxy,
proxy=self.proxy,
)
get_magnet_status = await get_magnet_status.json()

unlock_link = await self.session.get(
f"{self.api_url}/link/unlock?agent=comet&link={get_magnet_status['data']['magnets']['links'][int(index)]['link']}",
proxy=proxy,
proxy=self.proxy,
)
unlock_link = await unlock_link.json()

Expand Down

0 comments on commit 1502d4c

Please sign in to comment.