Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 4, 2024
1 parent 32d1a94 commit 1999013
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 179 deletions.
55 changes: 3 additions & 52 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
from RTN import Torrent, parse, sort_torrents, title_match

from comet.debrid.manager import getDebrid

from comet.utils.general import (
bytes_to_size,
config_check,
get_indexer_manager,
get_torrent_hash,
is_video,
translate,
get_balanced_hashes,
)
Expand Down Expand Up @@ -74,7 +72,7 @@ async def stream(request: Request, b64config: str, type: str, id: str):
name = translate(name)
logName = name
if type == "series":
logName = f"{name} S{season:02d}E{episode:02d}"
logName = f"{name} S0{season}E0{episode}"

cache_key = hashlib.md5(
json.dumps(
Expand Down Expand Up @@ -214,58 +212,11 @@ async def stream(request: Request, b64config: str, type: str, id: str):

logger.info(f"{len(torrent_hashes)} info hashes found for {logName}")

torrent_hashes = list(set([hash for hash in torrent_hashes if hash]))

if len(torrent_hashes) == 0:
return {"streams": []}

hashes_checked = await debrid.check_hashes_cache(torrent_hashes)

availability = {}
for response in hashes_checked:
if not response:
continue

availability.update(await response.json())

files = {}
for hash, details in availability.items():
if "rd" not in details:
continue

if type == "series":
for variants in details["rd"]:
for index, file in variants.items():
filename = file["filename"]

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": index,
"title": filename,
"size": file["filesize"],
}

continue

for variants in details["rd"]:
for index, file in variants.items():
filename = file["filename"]

if not is_video(filename):
continue

files[hash] = {
"index": index,
"title": filename,
"size": file["filesize"],
}
availability = await debrid.get_availability(torrent_hashes)
files = await debrid.get_files(availability, type, season, episode)

ranked_files = set()
for hash in files:
Expand Down
3 changes: 0 additions & 3 deletions comet/debrid/alldebrid.py

This file was deleted.

5 changes: 1 addition & 4 deletions comet/debrid/manager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import aiohttp

from .realdebrid import RealDebrid
from .alldebrid import AllDebrid


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)
elif debrid_service == "alldebrid":
return AllDebrid(session, debrid_api_key)
return RealDebrid(session, debrid_api_key)
87 changes: 71 additions & 16 deletions comet/debrid/realdebrid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import aiohttp, asyncio
import aiohttp
import asyncio

from RTN import parse

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

Expand All @@ -25,26 +29,77 @@ async def check_premium(self):
f"Exception while checking premium status on Real Debrid: {e}"
)
return False

async def get_instant(self, hash: str):
try:
response = await self.session.get(
f"{self.api_url}/torrents/instantAvailability/{hash}"
)
return await response.json()
except Exception as e:
logger.warning(
f"Exception while checking hash cache on Real Debrid for {hash}: {e}"
)
return

async def check_hashes_cache(self, hashes: list):
async def check(hash: str):
try:
response = await self.session.get(
f"{self.api_url}/torrents/instantAvailability/{hash}"
)
return response
except Exception as e:
logger.warning(
f"Exception while checking hash cache on Real Debrid for {hash}: {e}"
)
return

async def get_availability(self, hashes: list):
tasks = []
for hash in hashes:
tasks.append(check(hash))
tasks.append(self.get_instant(hash))

responses = await asyncio.gather(*tasks)
return responses

availability = {}
for response in responses:
if not response:
continue

availability.update(response)

return availability


async def get_files(self, availability: dict, type: str, season: str, episode: str):
files = {}
for hash, details in availability.items():
if "rd" not in details:
continue

if type == "series":
for variants in details["rd"]:
for index, file in variants.items():
filename = file["filename"]

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": index,
"title": filename,
"size": file["filesize"],
}

continue

for variants in details["rd"]:
for index, file in variants.items():
filename = file["filename"]

if not is_video(filename):
continue

files[hash] = {
"index": index,
"title": filename,
"size": file["filesize"],
}

return files

async def generate_download_link(self, hash: str, index: str):
try:
Expand Down
1 change: 0 additions & 1 deletion comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@
<div class="form-item">
<sl-select id="debridService" value="realdebrid" label="Debrid Service" placeholder="Select debrid service">
<sl-option value="realdebrid">Real-Debrid</sl-option>
<sl-option value="alldebrid">All-Debrid</sl-option>
</sl-select>
</div>

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

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

Expand Down
Loading

0 comments on commit 1999013

Please sign in to comment.