Files
devplacepy/examples/devrant/feed_watch.py
T
retoor c151325916 feat: add plug-and-play devRant API client examples with Python and JavaScript
Add complete set of example scripts for the devRant REST protocol under
examples/devrant/, including reusable client libraries, a rant poster,
a live feed watcher with keyword-based auto-upvote, and an end-to-end
smoke test that exercises every endpoint. Both Python (stdlib-only) and
JavaScript (Node 18+ global fetch) implementations are provided.
2026-06-14 07:48:37 +00:00

40 lines
1.1 KiB
Python

# retoor <retoor@molodetz.nl>
import os
import time
from client import from_env
POLL_SECONDS = int(os.environ.get("DEVRANT_POLL_SECONDS", "30"))
UPVOTE_KEYWORDS = [
word.strip().lower()
for word in os.environ.get("DEVRANT_UPVOTE_KEYWORDS", "").split(",")
if word.strip()
]
def main() -> None:
api = from_env()
if api.username and api.password:
api.login()
seen: set[int] = set()
print(f"watching feed every {POLL_SECONDS}s (ctrl-c to stop)")
while True:
for rant in reversed(api.rants(sort="recent", limit=20)):
rant_id = rant["id"]
if rant_id in seen:
continue
seen.add(rant_id)
text = rant["text"].replace("\n", " ")[:80]
print(f"[{rant_id}] {rant['user_username']}: {text}")
if UPVOTE_KEYWORDS and api.auth:
lowered = rant["text"].lower()
if any(word in lowered for word in UPVOTE_KEYWORDS):
api.vote_rant(rant_id, 1)
print(f" upvoted {rant_id}")
time.sleep(POLL_SECONDS)
if __name__ == "__main__":
main()