Skip to content

Commit

Permalink
Merge pull request #166 from MunifTanjim/feat-stremthru
Browse files Browse the repository at this point in the history
feat(debrid): add support for stremthru
  • Loading branch information
g0ldyy authored Feb 7, 2025
2 parents cca5315 + 24611a2 commit 778a59e
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 43 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
3 changes: 2 additions & 1 deletion 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 All @@ -58,7 +59,7 @@ async def manifest(b64config: str = None):
if not config:
config = {"debridService": None}

debrid_extension = get_debrid_extension(config["debridService"])
debrid_extension = get_debrid_extension(config["debridService"], config["debridApiKey"])

return {
"id": settings.ADDON_ID,
Expand Down
58 changes: 23 additions & 35 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
get_aliases,
add_torrent_to_cache,
)
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 @@ -135,30 +136,19 @@ async def stream(
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 @@ -245,7 +235,7 @@ async def stream(
)
else:
the_stream["infoHash"] = hash
index = data["index"]
index = str(data["index"])
the_stream["fileIdx"] = (
1 if "|" in index else int(index)
) # 1 because for Premiumize it's impossible to get the file index
Expand Down Expand Up @@ -421,6 +411,7 @@ async def stream(
season,
episode,
kitsu,
video_id=full_id,
)

ranked_files = set()
Expand Down Expand Up @@ -474,16 +465,14 @@ async def stream(

logger.info(f"Results have been cached for {log_name}")

debrid_extension = get_debrid_extension(config["debridService"])
debrid_extension = get_debrid_extension(config["debridService"], config["debridApiKey"])

balanced_hashes = get_balanced_hashes(sorted_ranked_files, config)

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 All @@ -496,13 +485,17 @@ async def stream(
for resolution in balanced_hashes:
for hash in balanced_hashes[resolution]:
data = sorted_ranked_files[hash]["data"]
index = data['index']
if index == -1:
index = data['title']
url = f"{request.url.scheme}://{request.url.netloc}/{b64config}/playback/{hash}/{index}"
results.append(
{
"name": f"[{debrid_extension}⚡] Comet {data['resolution']}",
"description": format_title(data, config),
"torrentTitle": data["torrent_title"],
"torrentSize": data["torrent_size"],
"url": f"{request.url.scheme}://{request.url.netloc}/{b64config}/playback/{hash}/{data['index']}",
"url": url,
"behaviorHints": {
"filename": data["raw_title"],
"bingeGroup": "comet|" + hash,
Expand Down Expand Up @@ -545,13 +538,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(raise_for_status=True) as session:
# Check for cached download link
Expand Down Expand Up @@ -581,9 +568,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 @@ -603,10 +589,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
2 changes: 1 addition & 1 deletion comet/debrid/alldebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def get_instant(self, chunk: list):
)

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool, **kwargs
):
chunk_size = 500
chunks = [
Expand Down
2 changes: 1 addition & 1 deletion comet/debrid/debridlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def get_instant(self, chunk: list):
return responses

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool, **kwargs
):
chunk_size = 10
chunks = [
Expand Down
13 changes: 13 additions & 0 deletions comet/debrid/manager.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
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,
ip=ip,
)

if debrid_service == "realdebrid":
return RealDebrid(session, debrid_api_key, ip)
elif debrid_service == "alldebrid":
Expand Down
2 changes: 1 addition & 1 deletion comet/debrid/premiumize.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def get_instant(self, chunk: list):
)

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool, **kwargs
):
chunk_size = 100
chunks = [
Expand Down
2 changes: 1 addition & 1 deletion comet/debrid/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def get_instant(self, chunk: list):
)

async def get_files(
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool
self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool, **kwargs
):
chunk_size = 100
chunks = [
Expand Down
Loading

0 comments on commit 778a59e

Please sign in to comment.