Skip to content

Commit

Permalink
feat: Debrid-Link support
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 12, 2024
1 parent 4bf8e88 commit dadca78
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
124 changes: 124 additions & 0 deletions comet/debrid/debridlink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import aiohttp
import asyncio

from RTN import parse

from comet.utils.general import is_video
from comet.utils.logger import logger


class DebridLink:
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://debrid-link.com/api/v2"

async def check_premium(self):
try:
check_premium = await self.session.get(
f"{self.api_url}/account/infos"
)
check_premium = await check_premium.text()
if '"accountType":1' in check_premium:
return True
except Exception as e:
logger.warning(
f"Exception while checking premium status on Debrid-Link: {e}"
)

return False

async def get_instant(self, chunk: list):
try:
get_instant = await self.session.get(
f"{self.api_url}/seedbox/cached?url={','.join(chunk)}"
)
return await get_instant.json()
except Exception as e:
logger.warning(
f"Exception while checking hashes instant availability on Debrid-Link: {e}"
)

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str
):
chunk_size = 250
chunks = [
torrent_hashes[i : i + chunk_size]
for i in range(0, len(torrent_hashes), chunk_size)
]

tasks = []
for chunk in chunks:
tasks.append(self.get_instant(chunk))

responses = await asyncio.gather(*tasks)

availability = []
for response in responses:
if response is None:
continue

availability.append(response)

files = {}
for result in availability:
if "success" not in result or not result["success"]:
continue

if type == "series":
for hash in result["value"]:
hash_files = result["value"][hash]["files"]
for file in hash_files:
filename = file["name"]

if not is_video(filename):
continue

filename_parsed = parse(filename)
if (
season in filename_parsed.season
and episode in filename_parsed.episode
):
files[hash] = {
"index": hash_files.index(file),
"title": filename,
"size": file["size"],
}

continue

for hash in result["value"]:
hash_files = result["value"][hash]["files"]
for file in hash_files:
filename = file["name"]

if not is_video(filename):
continue

files[hash] = {
"index": hash_files.index(file),
"title": filename,
"size": file["size"],
}

return files

async def generate_download_link(self, hash: str, index: str):
try:
add_torrent = await self.session.post(
f"{self.api_url}/seedbox/add",
data={
"url": hash,
"async": True
}
)
add_torrent = await add_torrent.json()

return add_torrent["value"]["files"][int(index)]["downloadUrl"]
except Exception as e:
logger.warning(
f"Exception while getting download link from Debrid-Link for {hash}|{index}: {e}"
)
3 changes: 3 additions & 0 deletions comet/debrid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .alldebrid import AllDebrid
from .premiumize import Premiumize
from .torbox import TorBox
from .debridlink import DebridLink


def getDebrid(session: aiohttp.ClientSession, config: dict):
Expand All @@ -17,3 +18,5 @@ def getDebrid(session: aiohttp.ClientSession, config: dict):
return Premiumize(session, debrid_api_key)
elif debrid_service == "torbox":
return TorBox(session, debrid_api_key)
elif debrid_service == "debridlink":
return DebridLink(session, debrid_api_key)
3 changes: 3 additions & 0 deletions comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@
<sl-option value="alldebrid">All-Debrid</sl-option>
<sl-option value="premiumize">Premiumize</sl-option>
<sl-option value="torbox">TorBox</sl-option>
<sl-option value="debridlink">Debrid-Link</sl-option>
</sl-select>
</div>

Expand All @@ -561,6 +562,8 @@
apiKeyLink.href = "https://premiumize.me/account";
} else if (selectedService === "torbox") {
apiKeyLink.href = "https://torbox.app/settings";
} else if (selectedService === "debridlink") {
apiKeyLink.href = "https://debrid-link.com/webapp/apikey";
}
});
</script>
Expand Down
2 changes: 2 additions & 0 deletions comet/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ def get_debrid_extension(debridService: str):
debrid_extension = "PM"
elif debridService == "torbox":
debrid_extension = "TB"
elif debridService == "debridlink":
debrid_extension = "DL"

return debrid_extension

Expand Down
2 changes: 1 addition & 1 deletion comet/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def check_max_results(cls, v):

@field_validator("debridService")
def check_debrid_service(cls, v):
if v not in ["realdebrid", "alldebrid", "premiumize", "torbox"]:
if v not in ["realdebrid", "alldebrid", "premiumize", "torbox", "debridlink"]:
raise ValueError("Invalid debridService")
return v

Expand Down

0 comments on commit dadca78

Please sign in to comment.