Skip to content

Commit

Permalink
feat: cosmetics + important fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 5, 2024
1 parent e5a3755 commit a1441c8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
15 changes: 13 additions & 2 deletions comet/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fastapi.templating import Jinja2Templates

from comet.utils.models import settings
from comet.utils.general import config_check

templates = Jinja2Templates("comet/templates")
main = APIRouter()
Expand Down Expand Up @@ -58,11 +59,21 @@ async def configure(request: Request):

@main.get("/manifest.json")
@main.get("/{b64config}/manifest.json")
async def manifest():
async def manifest(b64config: str = None):
config = config_check(b64config)
if not config:
config = {"debridService": None}

debrid_extension = None
if config["debridService"] == "realdebrid":
debrid_extension = "RD"
if config["debridService"] == "alldebrid":
debrid_extension = "AD"

return {
"id": settings.ADDON_ID,
"version": "1.0.0",
"name": settings.ADDON_NAME,
"name": f"{settings.ADDON_NAME}{(' | ' + debrid_extension) if debrid_extension is not None else ''}",
"description": "Stremio's fastest torrent/debrid search add-on.",
"logo": "https://i.imgur.com/jmVoVMu.jpeg",
"background": "https://i.imgur.com/WwnXB3k.jpeg",
Expand Down
33 changes: 28 additions & 5 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ async def stream(request: Request, b64config: str, type: str, id: str):
async with aiohttp.ClientSession(connector=connector) as session:
debrid = getDebrid(session, config)

check_debrid = await debrid.check_premium()
if not check_debrid:
check_premium = await debrid.check_premium()
if not check_premium:
additional_info = ""
if config["debridService"] == "alldebrid":
additional_info = "\nCheck your email!"

return {
"streams": [
{
"name": "[⚠️] Comet",
"title": f"Invalid {config['debridService']} account.",
"title": f"Invalid {config['debridService']} account.{additional_info}",
"url": "https://comet.fast",
}
]
Expand Down Expand Up @@ -120,14 +124,20 @@ async def stream(request: Request, b64config: str, type: str, id: str):
)
sorted_ranked_files = json.loads(sorted_ranked_files[0])

debrid_extension = "EZ"
if config["debridService"] == "realdebrid":
debrid_extension = "RD"
if config["debridService"] == "alldebrid":
debrid_extension = "AD"

balanced_hashes = await get_balanced_hashes(sorted_ranked_files, config)
results = []
for hash, hash_data in sorted_ranked_files.items():
for resolution, hash_list in balanced_hashes.items():
if hash in hash_list:
results.append(
{
"name": f"[RD⚡] Comet {hash_data['data']['resolution'][0] if hash_data['data']['resolution'] else 'Unknown'}",
"name": f"[{debrid_extension}⚡] Comet {hash_data['data']['resolution'][0] if hash_data['data']['resolution'] else 'Unknown'}",
"title": f"{hash_data['data']['title']}\n💾 {bytes_to_size(hash_data['data']['size'])}",
"url": f"{request.url.scheme}://{request.url.netloc}/{b64config}/playback/{hash}/{hash_data['data']['index']}",
}
Expand Down Expand Up @@ -261,14 +271,20 @@ async def stream(request: Request, b64config: str, type: str, id: str):
)
logger.info(f"Results have been cached for {logName}")

debrid_extension = "EZ"
if config["debridService"] == "realdebrid":
debrid_extension = "RD"
if config["debridService"] == "alldebrid":
debrid_extension = "AD"

balanced_hashes = await get_balanced_hashes(sorted_ranked_files, config)
results = []
for hash, hash_data in sorted_ranked_files.items():
for resolution, hash_list in balanced_hashes.items():
if hash in hash_list:
results.append(
{
"name": f"[RD⚡] Comet {hash_data['data']['resolution'][0] if hash_data['data']['resolution'] else 'Unknown'}",
"name": f"[{debrid_extension}⚡] Comet {hash_data['data']['resolution'][0] if hash_data['data']['resolution'] else 'Unknown'}",
"title": f"{hash_data['data']['title']}\n💾 {bytes_to_size(hash_data['data']['size'])}",
"url": f"{request.url.scheme}://{request.url.netloc}/{b64config}/playback/{hash}/{hash_data['data']['index']}",
}
Expand All @@ -289,6 +305,9 @@ async def playback(b64config: str, hash: str, index: str):
debrid = getDebrid(session, config)
download_link = await debrid.generate_download_link(hash, index)

if download_link is None:
return

return RedirectResponse(download_link, status_code=302)


Expand All @@ -301,6 +320,10 @@ 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)

if download_link is None:
return

proxy = (
debrid.proxy if config["debridService"] == "alldebrid" else None
) # proxy is not needed to proxy realdebrid stream
Expand Down
36 changes: 28 additions & 8 deletions comet/debrid/alldebrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import aiohttp
import asyncio

from RTN import parse

Expand Down Expand Up @@ -31,20 +32,39 @@ async def check_premium(self):

return False

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str
):
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 torrent_hashes)}"
f"{self.api_url}/magnet/instant?agent={self.agent}&magnets[]={'&magnets[]='.join(hash for hash in chunk)}"
)
availability = await get_instant.json()
return await get_instant.json()
except Exception as e:
logger.warning(
f"Exception while checking hash cache on All Debrid for {hash}: {e}"
f"Exception while checking hashes instant availability on All Debrid: {e}"
)
return

return {}
async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str
):
chunk_size = 500
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.update(response)

if "status" not in availability or availability["status"] != "success":
return {}
Expand Down Expand Up @@ -128,4 +148,4 @@ async def generate_download_link(self, hash: str, index: str):
logger.warning(
f"Exception while getting download link from All Debrid for {hash}|{index}: {e}"
)
return "https://comet.fast"
return
2 changes: 1 addition & 1 deletion comet/debrid/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ async def generate_download_link(self, hash: str, index: str):
logger.warning(
f"Exception while getting download link from Real Debrid for {hash}|{index}: {e}"
)
return "https://comet.fast"
return
21 changes: 19 additions & 2 deletions comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@
</div>

<div class="form-item">
<sl-input id="debridStreamProxyPassword" label="Debrid Stream Proxy Password" placeholder="Enter secret password" help-text="Debrid Stream Proxying allows you to use your Debrid Service from multiple IPs at same time!"></sl-input>
<sl-input id="debridStreamProxyPassword" label="Debrid Stream Proxy Password" placeholder="Configuration required, see docs" help-text="Debrid Stream Proxying allows you to use your Debrid Service from multiple IPs at same time!"></sl-input>
</div>

<div class="form-item">
Expand All @@ -526,8 +526,25 @@
</div>

<div class="form-item">
<sl-input id="debridApiKey" label="Debrid API Key" placeholder="Enter API key"></sl-input>
<label for="debridService">
Debrid API Key -
<a id="apiKeyLink" href="https://real-debrid.com/apitoken" target="_blank">Get It Here</a>
</label>
<sl-input id="debridApiKey" placeholder="Enter API key"></sl-input>
</div>

<script>
document.getElementById("debridService").addEventListener("sl-change", function(event) {
const selectedService = event.target.value;
const apiKeyLink = document.getElementById("apiKeyLink");

if (selectedService === "realdebrid") {
apiKeyLink.href = "https://real-debrid.com/apitoken";
} else if (selectedService === "alldebrid") {
apiKeyLink.href = "https://alldebrid.fr/apikeys";
}
});
</script>

<div class="centered-item">
<sl-button id="install" variant="neutral">Install</sl-button>
Expand Down

0 comments on commit a1441c8

Please sign in to comment.