Skip to content

Commit

Permalink
refactor: improve SQL query formatting and add sample file filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Mar 4, 2025
1 parent f6970fa commit 8db7933
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 67 deletions.
38 changes: 26 additions & 12 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
)

Expand All @@ -335,30 +345,34 @@ 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,
None,
None,
config["debridService"],
config["debridApiKey"],
ip,
ip if not should_proxy else "",
)
download_url = await debrid.generate_download_link(
hash, index, name, season, episode
)
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,
Expand Down
4 changes: 4 additions & 0 deletions comet/debrid/stremthru.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions comet/scrapers/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
""",
{
Expand Down Expand Up @@ -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
Expand Down
47 changes: 0 additions & 47 deletions comet/utils/config.py

This file was deleted.

10 changes: 5 additions & 5 deletions comet/utils/debrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8db7933

Please sign in to comment.