Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 19 additions & 3 deletions comet/services/orchestration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from comet.core.scrape import ScrapeContext
from comet.scrapers.manager import scraper_manager
from comet.scrapers.models import ScrapeRequest
from comet.services.filtering import filter_worker
from comet.services.filtering import TitleMatcher, filter_worker
from comet.services.ranking import rank_worker
from comet.services.torrent_manager import torrent_update_queue
from comet.utils.languages import select_indexer_titles
Expand Down Expand Up @@ -196,15 +196,16 @@ async def _fetch_cached_rows(self, media_id: str):

async def get_cached_torrents(self):
rows = []
primary_info_hashes = set()
cache_row_groups = await asyncio.gather(
*(
self._fetch_cached_rows(cache_media_id)
for cache_media_id in self.cache_media_ids
)
)
for cache_media_id, cache_rows in zip(self.cache_media_ids, cache_row_groups):
if cache_rows and cache_media_id == self.media_only_id:
self.primary_cached = True
if cache_media_id == self.media_only_id:
primary_info_hashes.update(row["info_hash"] for row in cache_rows)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
rows.extend(cache_rows)

if rows:
Expand Down Expand Up @@ -237,6 +238,14 @@ def row_priority(row):

rows = list(best_rows.values())

title_matcher = TitleMatcher(
self.title,
self.year,
self.year_end,
self.media_type,
self.aliases,
)

for row in rows:
parsed_data = load_cached_parsed(row["parsed_json"])
if parsed_data is None:
Expand All @@ -246,6 +255,11 @@ def row_priority(row):
continue
ensure_multi_language(parsed_data)

if parsed_data.parsed_title and not title_matcher.matches(
row["title"], parsed_data.parsed_title, parsed_data.year
):
continue
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

target_season = self.search_season
if (
target_season is not None
Expand Down Expand Up @@ -277,6 +291,8 @@ def row_priority(row):
"parsed": parsed_data,
"updatedAt": row["updated_at"],
}
if info_hash in primary_info_hashes:
self.primary_cached = True

def _append_cache_file_infos(self, file_infos: list[dict], torrent: dict):
parsed = torrent["parsed"]
Expand Down
70 changes: 70 additions & 0 deletions tests/test_cached_title_revalidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import unittest
from unittest.mock import patch

from RTN import parse

from comet.services.orchestration import TorrentManager


class CachedTitleRevalidationTests(unittest.IsolatedAsyncioTestCase):
@staticmethod
def _row(title: str, info_hash: str) -> dict:
return {
"info_hash": info_hash,
"file_index": 0,
"title": title,
"seeders": 1,
"size": 1_000,
"tracker": "cache",
"sources_json": "[]",
"parsed_json": parse(title).model_dump_json(),
"episode": None,
"updated_at": 1,
}

@staticmethod
def _manager() -> TorrentManager:
return TorrentManager(
media_type="movie",
media_full_id="tt2250912",
media_only_id="tt2250912",
title="Spider-Man: Homecoming",
year=2017,
year_end=None,
season=None,
episode=None,
aliases={},
remove_adult_content=False,
)

async def test_mismatched_cached_title_is_rejected(self):
manager = self._manager()
wrong_hash = "a" * 40
wrong = self._row(
"Spider-Man.Into.the.Spider-Verse.2018.2160p.REMUX.HEVC.DV.mkv",
wrong_hash,
)

with patch.object(manager, "_fetch_cached_rows", return_value=[wrong]):
await manager.get_cached_torrents()

self.assertNotIn(wrong_hash, manager.torrents)
self.assertFalse(manager.primary_cached)

async def test_matching_cached_title_still_counts_as_primary_cache(self):
manager = self._manager()
right_hash = "b" * 40
right = self._row(
"Spider-Man.Homecoming.2017.2160p.BluRay.REMUX.HEVC.mkv",
right_hash,
)

with patch.object(manager, "_fetch_cached_rows", return_value=[right]):
await manager.get_cached_torrents()

self.assertIn(right_hash, manager.torrents)
self.assertTrue(manager.primary_cached)


if __name__ == "__main__":
unittest.main()