Skip to content

Commit

Permalink
feat(debrid): add support for stremthru
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Nov 21, 2024
1 parent 5266603 commit e66bcaa
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 33 deletions.
1 change: 1 addition & 0 deletions .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ PROXY_DEBRID_STREAM_DEBRID_DEFAULT_SERVICE=realdebrid # if you want your users w
PROXY_DEBRID_STREAM_DEBRID_DEFAULT_APIKEY=CHANGE_ME # if you want your users who use the Debrid Stream Proxy not to have to specify Debrid information, but to use the default one instead
TITLE_MATCH_CHECK=True # disable if you only use Torrentio / MediaFusion and are sure you're only scraping good titles, for example (keep it True if Zilean is enabled)
REMOVE_ADULT_CONTENT=False # detect and remove adult content
STREMTHRU_DEFAULT_URL=None # if you want your users to use StremThru without having to specify it
CUSTOM_HEADER_HTML=None # only set it if you know what it is
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Direct Torrent supported (do not specify a Debrid API Key on the configuration page (webui) to activate it - it will use the cached results of other users using debrid service)
- [Kitsu](https://kitsu.io/) support (anime)
- Adult Content Filter
- [StremThru](https://github.com/MunifTanjim/stremthru) support

# Installation
To customize your Comet experience to suit your needs, please first take a look at all the [environment variables](https://github.com/g0ldyy/comet/blob/main/.env-sample)!
Expand Down
1 change: 1 addition & 0 deletions comet/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async def configure(request: Request):
"webConfig": web_config,
"indexerManager": settings.INDEXER_MANAGER_TYPE,
"proxyDebridStream": settings.PROXY_DEBRID_STREAM,
"stremthruDefaultUrl": settings.STREMTHRU_DEFAULT_URL or "",
},
)

Expand Down
47 changes: 15 additions & 32 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
get_client_ip,
get_aliases,
)
from comet.utils.config import is_proxy_stream_authed, is_proxy_stream_enabled, prepare_debrid_config, should_skip_proxy_stream
from comet.utils.logger import logger
from comet.utils.models import database, rtn, settings, trackers

Expand Down Expand Up @@ -128,30 +129,19 @@ async def stream(request: Request, b64config: str, type: str, id: str):
if type == "series":
log_name = f"{name} S{season:02d}E{episode:02d}"

if (
settings.PROXY_DEBRID_STREAM
and settings.PROXY_DEBRID_STREAM_PASSWORD
== config["debridStreamProxyPassword"]
and config["debridApiKey"] == ""
):
config["debridService"] = (
settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_SERVICE
)
config["debridApiKey"] = settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_APIKEY
prepare_debrid_config(config)

if config["debridApiKey"] == "":
services = ["realdebrid", "alldebrid", "premiumize", "torbox", "debridlink"]
services = ["realdebrid", "alldebrid", "premiumize", "torbox", "debridlink", "stremthru"]
debrid_emoji = "⬇️"
else:
services = [config["debridService"]]
debrid_emoji = "⚡"

results = []
if (
config["debridStreamProxyPassword"] != ""
and settings.PROXY_DEBRID_STREAM
and settings.PROXY_DEBRID_STREAM_PASSWORD
!= config["debridStreamProxyPassword"]
is_proxy_stream_enabled(config)
and not is_proxy_stream_authed(config)
):
results.append(
{
Expand Down Expand Up @@ -479,10 +469,8 @@ async def stream(request: Request, b64config: str, type: str, id: str):

results = []
if (
config["debridStreamProxyPassword"] != ""
and settings.PROXY_DEBRID_STREAM
and settings.PROXY_DEBRID_STREAM_PASSWORD
!= config["debridStreamProxyPassword"]
is_proxy_stream_enabled(config)
and not is_proxy_stream_authed(config)
):
results.append(
{
Expand Down Expand Up @@ -544,13 +532,7 @@ async def playback(request: Request, b64config: str, hash: str, index: str):
if not config:
return FileResponse("comet/assets/invalidconfig.mp4")

if (
settings.PROXY_DEBRID_STREAM
and settings.PROXY_DEBRID_STREAM_PASSWORD == config["debridStreamProxyPassword"]
and config["debridApiKey"] == ""
):
config["debridService"] = settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_SERVICE
config["debridApiKey"] = settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_APIKEY
prepare_debrid_config(config)

async with aiohttp.ClientSession() as session:
# Check for cached download link
Expand Down Expand Up @@ -580,9 +562,8 @@ async def playback(request: Request, b64config: str, hash: str, index: str):
config,
ip
if (
not settings.PROXY_DEBRID_STREAM
or settings.PROXY_DEBRID_STREAM_PASSWORD
!= config["debridStreamProxyPassword"]
not is_proxy_stream_enabled(config)
or not is_proxy_stream_authed(config)
)
else "",
)
Expand All @@ -602,10 +583,12 @@ async def playback(request: Request, b64config: str, hash: str, index: str):
},
)

if should_skip_proxy_stream(config):
return RedirectResponse(download_link, status_code=302)

if (
settings.PROXY_DEBRID_STREAM
and settings.PROXY_DEBRID_STREAM_PASSWORD
== config["debridStreamProxyPassword"]
is_proxy_stream_enabled(config)
and is_proxy_stream_authed(config)
):
if settings.PROXY_DEBRID_STREAM_MAX_CONNECTIONS != -1:
active_ip_connections = await database.fetch_all(
Expand Down
12 changes: 12 additions & 0 deletions comet/debrid/manager.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import aiohttp

from comet.utils.config import should_use_stremthru

from .realdebrid import RealDebrid
from .alldebrid import AllDebrid
from .premiumize import Premiumize
from .torbox import TorBox
from .debridlink import DebridLink
from .stremthru import StremThru


def getDebrid(session: aiohttp.ClientSession, config: dict, ip: str):
debrid_service = config["debridService"]
debrid_api_key = config["debridApiKey"]

if should_use_stremthru(config):
return StremThru(
session=session,
url=config["stremthruUrl"],
debrid_service=debrid_service,
token=debrid_api_key,
)

if debrid_service == "realdebrid":
return RealDebrid(session, debrid_api_key, ip)
elif debrid_service == "alldebrid":
Expand Down
174 changes: 174 additions & 0 deletions comet/debrid/stremthru.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import asyncio
from typing import Optional

import aiohttp
from RTN import parse

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


class StremThru:
def __init__(
self,
session: aiohttp.ClientSession,
url: str,
token: str,
debrid_service: str,
):
if not self.is_supported_store(debrid_service):
raise ValueError(f"unsupported store: {debrid_service}")

if debrid_service == "stremthru":
session.headers["Proxy-Authorization"] = f"Basic {token}"
else:
session.headers["X-StremThru-Store-Name"] = debrid_service
session.headers["X-StremThru-Store-Authorization"] = f"Bearer {token}"

session.headers["User-Agent"] = "comet"

self.session = session
self.base_url = f"{url}/v0/store"
self.name = f"StremThru[{debrid_service}]" if debrid_service else "StremThru"

@staticmethod
def is_supported_store(name: Optional[str]):
return (
name == "stremthru"
or name == "alldebrid"
or name == "debridlink"
or name == "premiumize"
or name == "realdebrid"
or name == "torbox"
)

async def check_premium(self):
try:
user = await self.session.get(f"{self.base_url}/user")
user = await user.json()
return user["data"]["subscription_status"] == "premium"
except Exception as e:
logger.warning(
f"Exception while checking premium status on {self.name}: {e}"
)

return False

async def get_instant(self, magnets: list):
try:
magnet = await self.session.get(
f"{self.base_url}/magnets/check?magnet={','.join(magnets)}"
)
return await magnet.json()
except Exception as e:
logger.warning(
f"Exception while checking hash instant availability on {self.name}: {e}"
)

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool
):
chunk_size = 25
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 = [
response["data"]["items"]
for response in responses
if response and "data" in response
]

files = {}

if type == "series":
for magnets in availability:
for magnet in magnets:
if magnet["status"] != "cached":
continue

for file in magnet["files"]:
filename = file["name"]

if not is_video(filename) or "sample" in filename:
continue

filename_parsed = parse(filename)

if episode not in filename_parsed.episodes:
continue

if kitsu:
if filename_parsed.seasons:
continue
else:
if season not in filename_parsed.seasons:
continue

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

break
else:
for magnets in availability:
for magnet in magnets:
if magnet["status"] != "cached":
continue

for file in magnet["files"]:
filename = file["name"]

if not is_video(filename) or "sample" in filename:
continue

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

break

return files

async def generate_download_link(self, hash: str, index: str):
try:
magnet = await self.session.post(
f"{self.base_url}/magnets",
json={"magnet": f"magnet:?xt=urn:btih:{hash}"},
)
magnet = await magnet.json()

file = next(
(
file
for file in magnet["data"]["files"]
if file["index"] == int(index)
),
None,
)

if not file:
return

link = await self.session.post(
f"{self.base_url}/link/generate",
json={"link": file["link"]},
)
link = await link.json()

return link["data"]["link"]
except Exception as e:
logger.warning(
f"Exception while getting download link from {self.name} for {hash}|{index}: {e}"
)
2 changes: 2 additions & 0 deletions comet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def start_log():
"COMET",
f"Debrid Stream Proxy: {bool(settings.PROXY_DEBRID_STREAM)} - Password: {settings.PROXY_DEBRID_STREAM_PASSWORD} - Max Connections: {settings.PROXY_DEBRID_STREAM_MAX_CONNECTIONS} - Default Debrid Service: {settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_SERVICE} - Default Debrid API Key: {settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_APIKEY}",
)
if settings.STREMTHRU_DEFAULT_URL:
logger.log("COMET", f"Default StremThru URL: {settings.STREMTHRU_DEFAULT_URL}")
logger.log("COMET", f"Title Match Check: {bool(settings.TITLE_MATCH_CHECK)}")
logger.log("COMET", f"Remove Adult Content: {bool(settings.REMOVE_ADULT_CONTENT)}")
logger.log("COMET", f"Custom Header HTML: {bool(settings.CUSTOM_HEADER_HTML)}")
Expand Down
11 changes: 11 additions & 0 deletions comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@
<sl-option value="premiumize">Premiumize</sl-option>
<sl-option value="torbox">TorBox</sl-option>
<sl-option value="debridlink">Debrid-Link</sl-option>
<sl-option value="stremthru">StremThru</sl-option>
</sl-select>
</div>

Expand All @@ -557,6 +558,10 @@
<sl-input id="debridApiKey" placeholder="Enter API key" help-text="If no key is specified, you'll get direct torrents!"></sl-input>
</div>

<div class="form-item">
<sl-input id="stremthruUrl" label="StremThru URL" placeholder="{{stremthruDefaultUrl}}" help-text="URL for StremThru."></sl-input>
</div>

<sl-details summary="Advanced Settings">
<div class="form-item">
<sl-checkbox checked id="removeTrash" help-text="Remove all trash from results (Adult Content, CAM, Clean Audio, PDTV, R5, Screener, Size, Telecine and Telesync)">Remove Trash</sl-checkbox>
Expand All @@ -581,6 +586,8 @@
apiKeyLink.href = "https://torbox.app/settings";
} else if (selectedService === "debridlink") {
apiKeyLink.href = "https://debrid-link.com/webapp/apikey";
} else if (selectedService === "stremthru") {
apiKeyLink.href = "https://github.com/MunifTanjim/stremthru?tab=readme-ov-file#configuration";
}
});
</script>
Expand Down Expand Up @@ -725,6 +732,7 @@
const resultFormat = Array.from(document.getElementById("resultFormat").selectedOptions).map(option => option.value);
const debridService = document.getElementById("debridService").value;
const debridApiKey = document.getElementById("debridApiKey").value;
const stremthruUrl = document.getElementById("stremthruUrl").value;
const debridStreamProxyPassword = document.getElementById("debridStreamProxyPassword").value;
const selectedLanguages = languages.length === defaultLanguages.length && languages.every((val, index) => val === defaultLanguages[index]) ? ["All"] : languages;
const selectedResolutions = resolutions.length === defaultResolutions.length && resolutions.every((val, index) => val === defaultResolutions[index]) ? ["All"] : resolutions;
Expand All @@ -742,6 +750,7 @@
languages: selectedLanguages,
debridService: debridService,
debridApiKey: debridApiKey,
stremthruUrl: stremthruUrl,
debridStreamProxyPassword: debridStreamProxyPassword,
};
}
Expand Down Expand Up @@ -807,6 +816,8 @@
document.getElementById("indexers").value = settings.indexers;
if (settings.languages !== null && settings.languages != "All")
document.getElementById("languages").value = settings.languages;
if (settings.stremthruUrl !== null)
document.getElementById("stremthruUrl").value = settings.stremthruUrl;
if (settings.resolutions !== null && settings.resolutions != "All")
document.getElementById("resolutions").value = settings.resolutions;
if (settings.resultFormat !== null && settings.resultFormat != "All")
Expand Down
Loading

0 comments on commit e66bcaa

Please sign in to comment.