From 8db793339bec93c64f4cf6228e0d2e88087daeec Mon Sep 17 00:00:00 2001 From: Goldy <153996346+g0ldyy@users.noreply.github.com> Date: Tue, 4 Mar 2025 11:30:39 +0100 Subject: [PATCH] refactor: improve SQL query formatting and add sample file filtering --- comet/api/stream.py | 38 +++++++++++++++++++++---------- comet/debrid/stremthru.py | 4 ++++ comet/scrapers/manager.py | 10 ++++++--- comet/utils/config.py | 47 --------------------------------------- comet/utils/debrid.py | 10 ++++----- 5 files changed, 42 insertions(+), 67 deletions(-) delete mode 100644 comet/utils/config.py diff --git a/comet/api/stream.py b/comet/api/stream.py index 10b768c..b4316e9 100644 --- a/comet/api/stream.py +++ b/comet/api/stream.py @@ -322,11 +322,21 @@ async def playback( async with aiohttp.ClientSession() as session: cached_link = await database.fetch_one( - f"SELECT download_url FROM download_links_cache WHERE debrid_key = '{config['debridApiKey']}' AND info_hash = '{hash}' AND ((cast(:season as INTEGER) IS NULL AND season IS NULL) OR season = cast(:season as INTEGER)) AND ((cast(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = cast(:episode as INTEGER)) AND timestamp + 3600 >= :current_time", + """ + SELECT download_url + FROM download_links_cache + WHERE debrid_key = :debrid_key + AND info_hash = :info_hash + AND ((CAST(:season as INTEGER) IS NULL AND season IS NULL) OR season = CAST(:season as INTEGER)) + AND ((CAST(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = CAST(:episode as INTEGER)) + AND timestamp + 3600 >= :current_time + """, { - "current_time": time.time(), + "debrid_key": config["debridApiKey"], + "info_hash": hash, "season": season, - "episode": season, + "episode": episode, + "current_time": time.time(), }, ) @@ -335,6 +345,12 @@ async def playback( download_url = cached_link["download_url"] ip = get_client_ip(request) + should_proxy = ( + settings.PROXY_DEBRID_STREAM + and settings.PROXY_DEBRID_STREAM_PASSWORD + == config["debridStreamProxyPassword"] + ) + if download_url is None: debrid = get_debrid( session, @@ -342,7 +358,7 @@ async def playback( None, config["debridService"], config["debridApiKey"], - ip, + ip if not should_proxy else "", ) download_url = await debrid.generate_download_link( hash, index, name, season, episode @@ -350,15 +366,13 @@ async def playback( if not download_url: return FileResponse("comet/assets/uncached.mp4") - query = f""" - INSERT {"OR IGNORE " if settings.DATABASE_TYPE == "sqlite" else ""} - INTO download_links_cache - VALUES (:debrid_key, :info_hash, :season, :episode, :download_url, :timestamp) - {" ON CONFLICT DO NOTHING" if settings.DATABASE_TYPE == "postgresql" else ""} - """ - await database.execute( - query, + f""" + INSERT {"OR IGNORE " if settings.DATABASE_TYPE == "sqlite" else ""} + INTO download_links_cache + VALUES (:debrid_key, :info_hash, :season, :episode, :download_url, :timestamp) + {" ON CONFLICT DO NOTHING" if settings.DATABASE_TYPE == "postgresql" else ""} + """, { "debrid_key": config["debridApiKey"], "info_hash": hash, diff --git a/comet/debrid/stremthru.py b/comet/debrid/stremthru.py index a85a54e..7df83a2 100644 --- a/comet/debrid/stremthru.py +++ b/comet/debrid/stremthru.py @@ -188,6 +188,10 @@ async def generate_download_link( files = [] for file in debrid_files: filename = file["name"] + + if "sample" in filename.lower(): + continue + filename_parsed = parse(filename) if not is_video(filename) or not title_match( diff --git a/comet/scrapers/manager.py b/comet/scrapers/manager.py index 75a06aa..5c38b99 100644 --- a/comet/scrapers/manager.py +++ b/comet/scrapers/manager.py @@ -119,8 +119,8 @@ async def get_cached_torrents(self): SELECT info_hash, file_index, title, seeders, size, tracker, sources, parsed FROM torrents WHERE media_id = :media_id - AND ((season IS NOT NULL AND season = cast(:season as INTEGER)) OR (season IS NULL AND cast(:season as INTEGER) IS NULL)) - AND (episode IS NULL OR episode = cast(:episode as INTEGER)) + AND ((season IS NOT NULL AND season = CAST(:season as INTEGER)) OR (season IS NULL AND CAST(:season as INTEGER) IS NULL)) + AND (episode IS NULL OR episode = CAST(:episode as INTEGER)) AND timestamp + :cache_ttl >= :current_time """, { @@ -185,7 +185,11 @@ async def filter(self, torrents: list): remove_adult_content = self.remove_adult_content for torrent in torrents: - parsed = parse(torrent["title"]) + torrent_title = torrent["title"] + if "sample" in torrent_title.lower(): + continue + + parsed = parse(torrent_title) if remove_adult_content and parsed.adult: continue diff --git a/comet/utils/config.py b/comet/utils/config.py deleted file mode 100644 index c38c7b4..0000000 --- a/comet/utils/config.py +++ /dev/null @@ -1,47 +0,0 @@ -from typing import Any - -from comet.debrid.stremthru import StremThru -from comet.utils.models import settings - - -def is_proxy_stream_enabled(config: dict[str, Any]): - return ( - bool(settings.PROXY_DEBRID_STREAM) and config["debridStreamProxyPassword"] != "" - ) - - -def is_proxy_stream_authed(config: dict[str, Any]): - return settings.PROXY_DEBRID_STREAM_PASSWORD == config["debridStreamProxyPassword"] - - -def should_use_stremthru(config: dict[str, Any]): - return config["stremthruUrl"] and StremThru.is_supported_store( - config["debridService"] - ) - - -def should_skip_proxy_stream(config: dict[str, Any]): - return ( - config["stremthruUrl"] - and config["debridService"] == "stremthru" - and ":" not in config["debridApiKey"] - ) - - -def should_use_fallback_debrid_config(config: dict[str, Any]): - if is_proxy_stream_authed(config) and config["debridApiKey"] == "": - return True - - return False - - -def prepare_debrid_config(config: dict[str, Any]): - if should_use_fallback_debrid_config(config): - config["debridService"] = settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_SERVICE - config["debridApiKey"] = settings.PROXY_DEBRID_STREAM_DEBRID_DEFAULT_APIKEY - - if not config["stremthruUrl"] and ( - config["debridService"] == "stremthru" - or config["debridService"] in settings.STREMTHRU_AUTO_ENABLED_DEBRID_SERVICES - ): - config["stremthruUrl"] = settings.STREMTHRU_DEFAULT_URL diff --git a/comet/utils/debrid.py b/comet/utils/debrid.py index c1c9a30..e7f07fc 100644 --- a/comet/utils/debrid.py +++ b/comet/utils/debrid.py @@ -123,7 +123,7 @@ async def get_cached_availability( base_query = f""" SELECT info_hash, file_index, title, size, parsed FROM debrid_availability - WHERE info_hash IN (SELECT cast(value as TEXT) FROM {"json_array_elements_text" if settings.DATABASE_TYPE == "postgresql" else "json_each"}(:info_hashes)) + WHERE info_hash IN (SELECT CAST(value as TEXT) FROM {"json_array_elements_text" if settings.DATABASE_TYPE == "postgresql" else "json_each"}(:info_hashes)) AND debrid_service = :debrid_service AND timestamp + :cache_ttl >= :current_time """ @@ -141,8 +141,8 @@ async def get_cached_availability( query = ( base_query + """ - AND ((cast(:season as INTEGER) IS NULL AND season IS NULL) OR season = cast(:season as INTEGER)) - AND ((cast(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = cast(:episode as INTEGER)) + AND ((CAST(:season as INTEGER) IS NULL AND season IS NULL) OR season = CAST(:season as INTEGER)) + AND ((CAST(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = CAST(:episode as INTEGER)) """ ) results = await database.fetch_all(query, params) @@ -164,8 +164,8 @@ async def get_cached_availability( query = ( base_query + """ - AND ((cast(:season as INTEGER) IS NULL AND season IS NULL) OR season = cast(:season as INTEGER)) - AND ((cast(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = cast(:episode as INTEGER)) + AND ((CAST(:season as INTEGER) IS NULL AND season IS NULL) OR season = CAST(:season as INTEGER)) + AND ((CAST(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = CAST(:episode as INTEGER)) """ ) results = await database.fetch_all(query, params)