diff --git a/plex_tmdb/services/xdcc.py b/plex_tmdb/services/xdcc.py index e66dd91..c6ea3f7 100644 --- a/plex_tmdb/services/xdcc.py +++ b/plex_tmdb/services/xdcc.py @@ -15,6 +15,7 @@ API_URL = "https://xdcc.info/api/v1/search" CACHE_TTL_SECONDS = 15 * 60 +MAX_PAGES = 50 # 50 pages x 200 results = 10,000 packs per episode _cache: Dict[str, Any] = {} _cache_lock = threading.Lock() @@ -146,8 +147,13 @@ def search_missing_episode( all_results.extend(page_results) total = payload.get("total") if isinstance(payload, dict) else None - if not page_results or ( - isinstance(total, int) and len(all_results) >= total + # Stop on an empty or short page (the last page), once the reported + # total is reached, or at a hard page cap in case the API ignores + # the page parameter and keeps returning full pages. + if ( + len(raw_results) < page_limit + or (isinstance(total, int) and len(all_results) >= total) + or page >= MAX_PAGES ): break diff --git a/requirements.txt b/requirements.txt index ce30ca0..3737a97 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,21 +10,21 @@ Flask-SQLAlchemy==3.0.5 PlexAPI==4.15.4 # HTTP requests and API calls -requests==2.33.0 +requests==2.34.2 urllib3==2.8.0 # Image processing (for poster/backdrop handling) -Pillow==12.3.0 +Pillow==13.0.0 # Date and time handling python-dateutil==2.8.2 # Configuration and environment -python-dotenv==1.2.2 +python-dotenv==1.2.4 # Console output and logging colorama==0.4.6 -tqdm==4.67.1 +tqdm==4.70.1 # JSON handling and validation jsonschema==4.19.2 @@ -39,7 +39,7 @@ pytz==2023.3 rich==13.7.0 # Security and cryptography -cryptography==50.0.0 +cryptography==50.0.2 # File path handling (Python < 3.4 compatibility) pathlib2==2.3.7.post1 @@ -51,10 +51,10 @@ configparser==6.0.0 alive-progress==3.1.5 # Enhanced logging (optional) -loguru==0.7.2 +loguru==0.7.3 # Development and testing dependencies (optional) -pytest==9.0.3 +pytest==9.2.0 pytest-cov==4.1.0 black==26.10.1 flake8==6.1.0 \ No newline at end of file diff --git a/tests/test_xdcc.py b/tests/test_xdcc.py index b35d7be..8b88c9f 100644 --- a/tests/test_xdcc.py +++ b/tests/test_xdcc.py @@ -25,3 +25,49 @@ def json(self): assert results[0]["pack_num"] == 42 assert results[0]["xdcc_command"] == "/msg ExampleBot xdcc send #42" mock_get.assert_called_once() + + +def _pack(n): + return { + "id": n, + "pack_num": n, + "filename": f"Show.S01E02.part{n}.mkv", + "bot": "ExampleBot", + "network": "Rizon", + } + + +class _PagedResponse: + status_code = 200 + + def __init__(self, results, total=None): + self._payload = {"results": results} + if total is not None: + self._payload["total"] = total + + def json(self): + return self._payload + + +@patch("plex_tmdb.services.xdcc.time.sleep") +@patch("plex_tmdb.services.xdcc._session.get") +def test_search_stops_on_short_page_without_total(mock_get, _sleep): + """No "total" in the payload: a page smaller than the limit ends paging.""" + mock_get.side_effect = [ + _PagedResponse([_pack(1), _pack(2)]), + _PagedResponse([_pack(3)]), + ] + results = search_missing_episode("ShortPage", 1, 2, limit=2) + assert [r["pack_num"] for r in results] == [1, 2, 3] + assert mock_get.call_count == 2 + + +@patch("plex_tmdb.services.xdcc.time.sleep") +@patch("plex_tmdb.services.xdcc._session.get") +def test_search_stops_at_page_cap_when_api_repeats_full_pages(mock_get, _sleep): + """An API that ignores "page" and always returns a full page must not loop forever.""" + from plex_tmdb.services.xdcc import MAX_PAGES + + mock_get.return_value = _PagedResponse([_pack(1)]) + search_missing_episode("EndlessPages", 1, 2, limit=1) + assert mock_get.call_count == MAX_PAGES