Skip to content

Commit 8db7933

Browse files
committed
refactor: improve SQL query formatting and add sample file filtering
1 parent f6970fa commit 8db7933

File tree

5 files changed

+42
-67
lines changed

5 files changed

+42
-67
lines changed

comet/api/stream.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,21 @@ async def playback(
322322

323323
async with aiohttp.ClientSession() as session:
324324
cached_link = await database.fetch_one(
325-
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",
325+
"""
326+
SELECT download_url
327+
FROM download_links_cache
328+
WHERE debrid_key = :debrid_key
329+
AND info_hash = :info_hash
330+
AND ((CAST(:season as INTEGER) IS NULL AND season IS NULL) OR season = CAST(:season as INTEGER))
331+
AND ((CAST(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = CAST(:episode as INTEGER))
332+
AND timestamp + 3600 >= :current_time
333+
""",
326334
{
327-
"current_time": time.time(),
335+
"debrid_key": config["debridApiKey"],
336+
"info_hash": hash,
328337
"season": season,
329-
"episode": season,
338+
"episode": episode,
339+
"current_time": time.time(),
330340
},
331341
)
332342

@@ -335,30 +345,34 @@ async def playback(
335345
download_url = cached_link["download_url"]
336346

337347
ip = get_client_ip(request)
348+
should_proxy = (
349+
settings.PROXY_DEBRID_STREAM
350+
and settings.PROXY_DEBRID_STREAM_PASSWORD
351+
== config["debridStreamProxyPassword"]
352+
)
353+
338354
if download_url is None:
339355
debrid = get_debrid(
340356
session,
341357
None,
342358
None,
343359
config["debridService"],
344360
config["debridApiKey"],
345-
ip,
361+
ip if not should_proxy else "",
346362
)
347363
download_url = await debrid.generate_download_link(
348364
hash, index, name, season, episode
349365
)
350366
if not download_url:
351367
return FileResponse("comet/assets/uncached.mp4")
352368

353-
query = f"""
354-
INSERT {"OR IGNORE " if settings.DATABASE_TYPE == "sqlite" else ""}
355-
INTO download_links_cache
356-
VALUES (:debrid_key, :info_hash, :season, :episode, :download_url, :timestamp)
357-
{" ON CONFLICT DO NOTHING" if settings.DATABASE_TYPE == "postgresql" else ""}
358-
"""
359-
360369
await database.execute(
361-
query,
370+
f"""
371+
INSERT {"OR IGNORE " if settings.DATABASE_TYPE == "sqlite" else ""}
372+
INTO download_links_cache
373+
VALUES (:debrid_key, :info_hash, :season, :episode, :download_url, :timestamp)
374+
{" ON CONFLICT DO NOTHING" if settings.DATABASE_TYPE == "postgresql" else ""}
375+
""",
362376
{
363377
"debrid_key": config["debridApiKey"],
364378
"info_hash": hash,

comet/debrid/stremthru.py

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ async def generate_download_link(
188188
files = []
189189
for file in debrid_files:
190190
filename = file["name"]
191+
192+
if "sample" in filename.lower():
193+
continue
194+
191195
filename_parsed = parse(filename)
192196

193197
if not is_video(filename) or not title_match(

comet/scrapers/manager.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ async def get_cached_torrents(self):
119119
SELECT info_hash, file_index, title, seeders, size, tracker, sources, parsed
120120
FROM torrents
121121
WHERE media_id = :media_id
122-
AND ((season IS NOT NULL AND season = cast(:season as INTEGER)) OR (season IS NULL AND cast(:season as INTEGER) IS NULL))
123-
AND (episode IS NULL OR episode = cast(:episode as INTEGER))
122+
AND ((season IS NOT NULL AND season = CAST(:season as INTEGER)) OR (season IS NULL AND CAST(:season as INTEGER) IS NULL))
123+
AND (episode IS NULL OR episode = CAST(:episode as INTEGER))
124124
AND timestamp + :cache_ttl >= :current_time
125125
""",
126126
{
@@ -185,7 +185,11 @@ async def filter(self, torrents: list):
185185
remove_adult_content = self.remove_adult_content
186186

187187
for torrent in torrents:
188-
parsed = parse(torrent["title"])
188+
torrent_title = torrent["title"]
189+
if "sample" in torrent_title.lower():
190+
continue
191+
192+
parsed = parse(torrent_title)
189193

190194
if remove_adult_content and parsed.adult:
191195
continue

comet/utils/config.py

-47
This file was deleted.

comet/utils/debrid.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async def get_cached_availability(
123123
base_query = f"""
124124
SELECT info_hash, file_index, title, size, parsed
125125
FROM debrid_availability
126-
WHERE info_hash IN (SELECT cast(value as TEXT) FROM {"json_array_elements_text" if settings.DATABASE_TYPE == "postgresql" else "json_each"}(:info_hashes))
126+
WHERE info_hash IN (SELECT CAST(value as TEXT) FROM {"json_array_elements_text" if settings.DATABASE_TYPE == "postgresql" else "json_each"}(:info_hashes))
127127
AND debrid_service = :debrid_service
128128
AND timestamp + :cache_ttl >= :current_time
129129
"""
@@ -141,8 +141,8 @@ async def get_cached_availability(
141141
query = (
142142
base_query
143143
+ """
144-
AND ((cast(:season as INTEGER) IS NULL AND season IS NULL) OR season = cast(:season as INTEGER))
145-
AND ((cast(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = cast(:episode as INTEGER))
144+
AND ((CAST(:season as INTEGER) IS NULL AND season IS NULL) OR season = CAST(:season as INTEGER))
145+
AND ((CAST(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = CAST(:episode as INTEGER))
146146
"""
147147
)
148148
results = await database.fetch_all(query, params)
@@ -164,8 +164,8 @@ async def get_cached_availability(
164164
query = (
165165
base_query
166166
+ """
167-
AND ((cast(:season as INTEGER) IS NULL AND season IS NULL) OR season = cast(:season as INTEGER))
168-
AND ((cast(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = cast(:episode as INTEGER))
167+
AND ((CAST(:season as INTEGER) IS NULL AND season IS NULL) OR season = CAST(:season as INTEGER))
168+
AND ((CAST(:episode as INTEGER) IS NULL AND episode IS NULL) OR episode = CAST(:episode as INTEGER))
169169
"""
170170
)
171171
results = await database.fetch_all(query, params)

0 commit comments

Comments
 (0)