fix: address code review issues in reddit_comment thread detection helpers
This commit is contained in:
parent
9d955b2c50
commit
719a1d5aca
2 changed files with 7 additions and 5 deletions
|
|
@ -53,7 +53,7 @@ def _extract_thread_id_from_url(url: str) -> str:
|
||||||
https://www.reddit.com/r/<sub>/comments/<id>/<title>/
|
https://www.reddit.com/r/<sub>/comments/<id>/<title>/
|
||||||
Raises ValueError if the ID cannot be found.
|
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:
|
if not match:
|
||||||
raise ValueError(f"Cannot extract thread id from {url!r}")
|
raise ValueError(f"Cannot extract thread id from {url!r}")
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
|
|
@ -69,9 +69,14 @@ def _find_sticky(
|
||||||
Uses the Reddit public JSON API (no auth required).
|
Uses the Reddit public JSON API (no auth required).
|
||||||
Returns the post ID (e.g. "abc123") of the first match, or None.
|
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"
|
url = f"https://www.reddit.com/r/{sub}/hot.json?limit=10"
|
||||||
response = httpx.get(url, headers={"User-Agent": "magpie/1.0"})
|
response = httpx.get(url, headers={"User-Agent": "magpie/1.0"})
|
||||||
|
response.raise_for_status()
|
||||||
payload = response.json()
|
payload = response.json()
|
||||||
|
if "data" not in payload:
|
||||||
|
logger.warning("Unexpected Reddit API response: %r", payload)
|
||||||
|
return None
|
||||||
children = payload.get("data", {}).get("children", [])
|
children = payload.get("data", {}).get("children", [])
|
||||||
pattern_lower = title_pattern.lower()
|
pattern_lower = title_pattern.lower()
|
||||||
for child in children:
|
for child in children:
|
||||||
|
|
|
||||||
|
|
@ -51,11 +51,8 @@ def test_parse_occurrence_first_sunday():
|
||||||
|
|
||||||
|
|
||||||
def test_parse_occurrence_unknown_raises():
|
def test_parse_occurrence_unknown_raises():
|
||||||
try:
|
with pytest.raises(ValueError):
|
||||||
parse_occurrence("fourth_wednesday")
|
parse_occurrence("fourth_wednesday")
|
||||||
assert False, "Expected ValueError"
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# --- _extract_thread_id_from_url ---
|
# --- _extract_thread_id_from_url ---
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue