Hacker News connector — top stories from the HN Firebase API.
No authentication required. All API calls are in module-level functions
for easy mocking in tests.
Classes
HackerNewsConnector
Bases: BaseConnector
Fetch the current top stories from Hacker News.
Source code in src/openjarvis/connectors/hackernews.py
| def __init__(self) -> None:
self._status = SyncStatus()
|
Functions
sync
sync(*, since: Optional[datetime] = None, cursor: Optional[str] = None) -> Iterator[Document]
Yield Documents for the top 5 Hacker News stories.
Source code in src/openjarvis/connectors/hackernews.py
| def sync(
self, *, since: Optional[datetime] = None, cursor: Optional[str] = None
) -> Iterator[Document]:
"""Yield Documents for the top 5 Hacker News stories."""
top_ids = _hn_top_story_ids()
for story_id in top_ids[:5]:
item = _hn_item(story_id)
if item is None:
continue
title = item.get("title", "")
score = item.get("score", 0)
descendants = item.get("descendants", 0)
url = item.get("url", "")
by = item.get("by", "")
ts = datetime.now()
if item.get("time"):
ts = datetime.fromtimestamp(item["time"])
yield Document(
doc_id=f"hn-{story_id}",
source="hackernews",
doc_type="story",
content=f"Score: {score}, Comments: {descendants}",
title=title,
author=by,
timestamp=ts,
url=url or None,
metadata={
"story_id": story_id,
"score": score,
"descendants": descendants,
},
)
self._status.state = "idle"
self._status.last_sync = datetime.now()
|