Skip to content

Commit 3e11756

Browse files
committed
chore: final tweaks for mastodon checker
1 parent 13bb05e commit 3e11756

File tree

1 file changed

+58
-45
lines changed

1 file changed

+58
-45
lines changed
Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,75 @@
11
"""
2-
Mastodon API-backed Maigret checker (example).
2+
Mastodon API-style checker (example).
33
4-
This checker calls the resolver via its module so tests can patch the resolver
5-
function (patching mastodon_api_resolver.resolve_mastodon_api will affect us).
4+
Uses the helper resolve_mastodon_api() (in mastodon_api_resolver.py)
5+
which probes instances using the Mastodon accounts lookup endpoint.
6+
This checker reports a hit when the resolver returns {"status": "found"}.
67
"""
8+
from typing import Dict, Any, Optional
79
import os
8-
from typing import Any, Dict
910

10-
# import the resolver module (importing the module object allows tests to patch
11-
# the function on the module, which `from ... import func` would not respect).
12-
from . import mastodon_api_resolver
11+
# import the resolver module (not the function) so tests that patch the
12+
# function on the module will affect calls performed here.
13+
from . import mastodon_api_resolver as resolver
1314

1415
DEFAULT_RANK = 120
1516

16-
def _empty_result(raw: Any = None) -> Dict:
17-
return {
18-
"http_status": None,
19-
"ids_usernames": {},
20-
"is_similar": False,
21-
"parsing_enabled": False,
22-
"rank": DEFAULT_RANK,
23-
"url": None,
24-
"raw": raw,
25-
}
26-
27-
def _found_result(username: str, url: str, raw: Any = None) -> Dict:
28-
short = username.lstrip("@").split("@")[0]
29-
return {
30-
"http_status": 200,
31-
"ids_usernames": {short: "username"},
32-
"is_similar": False,
33-
"parsing_enabled": True,
34-
"rank": DEFAULT_RANK,
35-
"url": url,
36-
"raw": raw,
37-
}
3817

39-
def check(username: str, settings=None, logger=None) -> Dict:
18+
def check(username: str, settings: Optional[object] = None, logger: Optional[object] = None, timeout: int = 6) -> Dict[str, Any]:
4019
"""
41-
Check a Mastodon account via the API resolver.
20+
Maigret-style checker for Mastodon-like handles.
21+
22+
Args:
23+
username: input username (may be '@name', 'name@instance' or 'name')
24+
settings, logger: optional compatibility parameters (not used here)
25+
timeout: passed to resolver
4226
43-
- username: may be "@alice", "alice", or "alice@instance"
44-
- settings, logger: accepted for compatibility (not used here)
27+
Returns:
28+
dict with keys at least: http_status, ids_usernames, parsing_enabled, rank, url, raw
4529
"""
30+
queried = username or ""
31+
queried_stripped = queried.lstrip("@")
32+
33+
# allow overriding the instance to probe via env var
4634
instance_hint = os.getenv("MAIGRET_MASTODON_INSTANCE")
4735

48-
# Call the resolver via module so tests can patch it:
49-
resolved = mastodon_api_resolver.resolve_mastodon_api(username, instance_hint=instance_hint)
36+
try:
37+
# call the resolver through the module so test patching works:
38+
resolved = resolver.resolve_mastodon_api(queried, instance_hint=instance_hint, timeout=timeout)
39+
except Exception as exc:
40+
# Do not raise during checks — treat as not found; log if logger is present
41+
if logger:
42+
try:
43+
logger.debug("mastodon resolver exception: %s", exc)
44+
except Exception:
45+
pass
46+
resolved = {"status": "not_found"}
5047

51-
# Defensive handling
52-
if not isinstance(resolved, dict):
53-
return _empty_result(raw=resolved)
48+
# Default not-found result
49+
result: Dict[str, Any] = {
50+
"http_status": None,
51+
"ids_usernames": {},
52+
"is_similar": False,
53+
"parsing_enabled": False,
54+
"rank": DEFAULT_RANK,
55+
"url": None,
56+
"raw": resolved,
57+
}
5458

55-
status = resolved.get("status")
56-
if isinstance(status, str) and status.lower() == "found":
57-
url = resolved.get("url")
58-
if not url and isinstance(resolved.get("data"), dict):
59-
url = resolved["data"].get("url")
60-
return _found_result(username, url, raw=resolved)
59+
if resolved.get("status") == "found":
60+
# Extract canonical username (drop leading '@' and any instance part)
61+
canon = queried_stripped.split("@", 1)[0]
62+
result.update(
63+
{
64+
"http_status": 200,
65+
"ids_usernames": {canon: "username"},
66+
"is_similar": False,
67+
# this checker provides a found profile URL / data so parsing_enabled = True
68+
"parsing_enabled": True,
69+
"rank": DEFAULT_RANK,
70+
"url": resolved.get("url"),
71+
"raw": resolved,
72+
}
73+
)
6174

62-
return _empty_result(raw=resolved)
75+
return result

0 commit comments

Comments
 (0)