Skip to content

Commit

Permalink
feat: torbox integration
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 11, 2024
1 parent 110f91c commit db1a003
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 8 deletions.
2 changes: 2 additions & 0 deletions comet/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ async def manifest(b64config: str = None):
debrid_extension = "AD"
elif config["debridService"] == "premiumize":
debrid_extension = "PM"
elif config["debridService"] == "torbox":
debrid_extension = "TB"

return {
"id": settings.ADDON_ID,
Expand Down
4 changes: 4 additions & 0 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ async def stream(request: Request, b64config: str, type: str, id: str):
debrid_extension = "AD"
elif config["debridService"] == "premiumize":
debrid_extension = "PM"
elif config["debridService"] == "torbox":
debrid_extension = "TB"

balanced_hashes = await get_balanced_hashes(sorted_ranked_files, config)

Expand Down Expand Up @@ -282,6 +284,8 @@ async def stream(request: Request, b64config: str, type: str, id: str):
debrid_extension = "AD"
elif config["debridService"] == "premiumize":
debrid_extension = "PM"
elif config["debridService"] == "torbox":
debrid_extension = "TB"

balanced_hashes = await get_balanced_hashes(sorted_ranked_files, config)

Expand Down
2 changes: 1 addition & 1 deletion comet/debrid/alldebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def check_premium(self):
async def get_instant(self, chunk: list):
try:
get_instant = await self.session.get(
f"{self.api_url}/magnet/instant?agent={self.agent}&magnets[]={'&magnets[]='.join(hash for hash in chunk)}"
f"{self.api_url}/magnet/instant?agent={self.agent}&magnets[]={'&magnets[]='.join(chunk)}"
)
return await get_instant.json()
except Exception as e:
Expand Down
7 changes: 5 additions & 2 deletions comet/debrid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from .realdebrid import RealDebrid
from .alldebrid import AllDebrid
from .premiumize import Premiumize
from .torbox import TorBox


def getDebrid(session: aiohttp.ClientSession, config: dict):
debrid_service = config["debridService"]
debrid_api_key = config["debridApiKey"]
if debrid_service == "realdebrid":
return RealDebrid(session, debrid_api_key)
if debrid_service == "alldebrid":
elif debrid_service == "alldebrid":
return AllDebrid(session, debrid_api_key)
if debrid_service == "premiumize":
elif debrid_service == "premiumize":
return Premiumize(session, debrid_api_key)
elif debrid_service == "torbox":
return TorBox(session, debrid_api_key)
4 changes: 2 additions & 2 deletions comet/debrid/premiumize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Premiumize:
def __init__(self, session: aiohttp.ClientSession, debrid_api_key: str):
self.session = session
self.proxy = None
self.debrid_api_key = debrid_api_key

self.api_url = "https://premiumize.me/api"
self.debrid_api_key = debrid_api_key

async def check_premium(self):
try:
Expand All @@ -36,7 +36,7 @@ async def check_premium(self):
async def get_instant(self, chunk: list):
try:
response = await self.session.get(
f"{self.api_url}/cache/check?apikey={self.debrid_api_key}&items[]={'&items[]='.join(hash for hash in chunk)}"
f"{self.api_url}/cache/check?apikey={self.debrid_api_key}&items[]={'&items[]='.join(chunk)}"
)

response = await response.json()
Expand Down
10 changes: 8 additions & 2 deletions comet/debrid/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def check_premium(self):
async def get_instant(self, chunk: list):
try:
response = await self.session.get(
f"{self.api_url}/torrents/instantAvailability/{'/'.join(hash for hash in chunk)}"
f"{self.api_url}/torrents/instantAvailability/{'/'.join(chunk)}"
)
return await response.json()
except Exception as e:
Expand Down Expand Up @@ -135,7 +135,13 @@ async def generate_download_link(self, hash: str, index: str):

await self.session.post(
f"{self.api_url}/torrents/selectFiles/{add_magnet['id']}",
data={"files": ",".join(str(file["id"]) for file in get_magnet_info["files"] if is_video(file["path"]))}, # "all" because bad
data={
"files": ",".join(
str(file["id"])
for file in get_magnet_info["files"]
if is_video(file["path"])
)
}, # "all" because bad
proxy=self.proxy,
)

Expand Down
146 changes: 146 additions & 0 deletions comet/debrid/torbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import aiohttp
import asyncio

from RTN import parse

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


class TorBox:
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.torbox.app/v1/api"
self.debrid_api_key = debrid_api_key

async def check_premium(self):
try:
check_premium = await self.session.get(
f"{self.api_url}/user/me?settings=false"
)
check_premium = await check_premium.text()
if '"success":true' in check_premium and '"plan":0' not in check_premium:
return True
except Exception as e:
logger.warning(f"Exception while checking premium status on TorBox: {e}")

return False

async def get_instant(self, chunk: list):
try:
response = await self.session.get(
f"{self.api_url}/torrents/checkcached?hash={','.join(chunk)}&list_files=true"
)
return await response.json()
except Exception as e:
logger.warning(
f"Exception while checking hash instant availability on TorBox: {e}"
)

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str
):
chunk_size = 100
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 not result["success"]:
continue

if type == "series":
for hash in result["data"]:
hash_files = result["data"][hash]["files"]
for file in hash_files:
filename = file["name"].split("/")[1]

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["data"]:
hash_files = result["data"][hash]["files"]
for file in hash_files:
filename = file["name"].split("/")[1]

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:
get_torrents = await self.session.get(
f"{self.api_url}/torrents/mylist?bypass_cache=true"
)
get_torrents = await get_torrents.json()
exists = False
for torrent in get_torrents["data"]:
if torrent["hash"] == hash:
torrent_id = torrent["id"]
exists = True
break
if not exists:
create_torrent = await self.session.post(
f"{self.api_url}/torrents/createtorrent",
data={"magnet": f"magnet:?xt=urn:btih:{hash}"},
)
create_torrent = await create_torrent.json()
torrent_id = create_torrent["data"]["torrent_id"]

get_torrents = await self.session.get(
f"{self.api_url}/torrents/mylist?bypass_cache=true"
)
get_torrents = await get_torrents.json()

for torrent in get_torrents["data"]:
if torrent["id"] == torrent_id:
file_id = torrent["files"][int(index)]["id"]

get_download_link = await self.session.get(
f"{self.api_url}/torrents/requestdl?token={self.debrid_api_key}&torrent_id={torrent_id}&file_id={file_id}&zip=false",
)
get_download_link = await get_download_link.json()

return get_download_link["data"]
except Exception as e:
logger.warning(
f"Exception while getting download link from TorBox for {hash}|{index}: {e}"
)
3 changes: 3 additions & 0 deletions comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@
<sl-option value="realdebrid">Real-Debrid</sl-option>
<sl-option value="alldebrid">All-Debrid</sl-option>
<sl-option value="premiumize">Premiumize</sl-option>
<sl-option value="torbox">TorBox</sl-option>
</sl-select>
</div>

Expand All @@ -558,6 +559,8 @@
apiKeyLink.href = "https://alldebrid.fr/apikeys";
} else if (selectedService === "premiumize") {
apiKeyLink.href = "https://premiumize.me/account";
} else if (selectedService === "torbox") {
apiKeyLink.href = "https://torbox.app/settings";
}
});
</script>
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"]:
if v not in ["realdebrid", "alldebrid", "premiumize", "torbox"]:
raise ValueError("Invalid debridService")
return v

Expand Down

0 comments on commit db1a003

Please sign in to comment.