Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions plex_tmdb/services/xdcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the Pillow pin compatible with Python 3.10

For non-Docker installs on Python 3.10, this pin makes pip install -r requirements.txt fail: Pillow 13 requires Python >=3.11, while the previous 12.3.0 supports Python 3.10. The README still advertises Python 3.7+ and setup.py includes a Python 3.10 classifier, so keep a compatible pin or update the supported-version documentation.

Useful? React with 👍 / 👎.


# 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
Expand All @@ -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
Expand All @@ -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
46 changes: 46 additions & 0 deletions tests/test_xdcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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