Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(debrid): add support for stremthru #166

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,6 +20,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 @@ -49,6 +49,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
54 changes: 21 additions & 33 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 @@ -421,6 +411,7 @@ async def stream(
season,
episode,
kitsu,
video_id=full_id,
)

ranked_files = set()
Expand Down Expand Up @@ -480,10 +471,8 @@ async def stream(

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)
Copy link
Contributor Author

@MunifTanjim MunifTanjim Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StremThru will generate a link to itself, that can proxy stream to debrid direct links. So Comet just needs to redirect to that link.


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