From 719a1d5aca58bed239b66a9c3212ebd274c9dca7 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Mon, 27 Apr 2026 11:55:55 -0700 Subject: [PATCH] fix: address code review issues in reddit_comment thread detection helpers --- app/services/platforms/reddit_comment.py | 7 ++++++- tests/services/platforms/test_reddit_comment.py | 5 +---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/services/platforms/reddit_comment.py b/app/services/platforms/reddit_comment.py index 7eb1828..ec6f2e3 100644 --- a/app/services/platforms/reddit_comment.py +++ b/app/services/platforms/reddit_comment.py @@ -53,7 +53,7 @@ def _extract_thread_id_from_url(url: str) -> str: https://www.reddit.com/r//comments/// Raises ValueError if the ID cannot be found. """ - match = re.search(r"/comments/([a-z0-9]+)/", url) + match = re.search(r"/comments/([a-zA-Z0-9]+)/", url) if not match: raise ValueError(f"Cannot extract thread id from {url!r}") return match.group(1) @@ -69,9 +69,14 @@ def _find_sticky( Uses the Reddit public JSON API (no auth required). Returns the post ID (e.g. "abc123") of the first match, or None. """ + # TODO: use session_file for authenticated requests on private subs url = f"https://www.reddit.com/r/{sub}/hot.json?limit=10" response = httpx.get(url, headers={"User-Agent": "magpie/1.0"}) + response.raise_for_status() payload = response.json() + if "data" not in payload: + logger.warning("Unexpected Reddit API response: %r", payload) + return None children = payload.get("data", {}).get("children", []) pattern_lower = title_pattern.lower() for child in children: diff --git a/tests/services/platforms/test_reddit_comment.py b/tests/services/platforms/test_reddit_comment.py index 6ef490a..0e835c6 100644 --- a/tests/services/platforms/test_reddit_comment.py +++ b/tests/services/platforms/test_reddit_comment.py @@ -51,11 +51,8 @@ def test_parse_occurrence_first_sunday(): def test_parse_occurrence_unknown_raises(): - try: + with pytest.raises(ValueError): parse_occurrence("fourth_wednesday") - assert False, "Expected ValueError" - except ValueError: - pass # --- _extract_thread_id_from_url ---