watch_rss.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. """Watch an RSS 2.0 or Atom feed; print new items to stdout, silent on empty.
  3. Usage (via cron with --no-agent):
  4. hermes cron create my-feed \\
  5. --schedule "*/15 * * * *" --no-agent \\
  6. --script "$HERMES_HOME/skills/devops/watchers/scripts/watch_rss.py" \\
  7. --script-args "--name hn --url https://news.ycombinator.com/rss"
  8. First run records a baseline (emits nothing). Subsequent runs emit only
  9. items whose <guid> / <id> isn't in the watermark.
  10. """
  11. from __future__ import annotations
  12. import argparse
  13. import sys
  14. import urllib.error
  15. import urllib.request
  16. from pathlib import Path
  17. from xml.etree import ElementTree as ET
  18. sys.path.insert(0, str(Path(__file__).parent))
  19. from _watermark import Watermark, format_items_as_markdown # type: ignore
  20. def _strip_ns(tag: str) -> str:
  21. return tag.split("}", 1)[1] if "}" in tag else tag
  22. def _parse_feed(xml_bytes: bytes):
  23. """Return a list of {id, title, url, summary} dicts.
  24. Handles both RSS 2.0 ``<item>`` and Atom ``<entry>``.
  25. """
  26. try:
  27. root = ET.fromstring(xml_bytes)
  28. except ET.ParseError as e:
  29. print(f"watch_rss: invalid XML: {e}", file=sys.stderr)
  30. sys.exit(2)
  31. entries = []
  32. for item in root.iter():
  33. tag = _strip_ns(item.tag)
  34. if tag not in {"item", "entry"}:
  35. continue
  36. # ElementTree Elements without children are *falsy* — use `is not None`.
  37. children = {_strip_ns(c.tag): c for c in item}
  38. guid_el = children.get("guid")
  39. if guid_el is None:
  40. guid_el = children.get("id")
  41. link_el = children.get("link")
  42. if link_el is not None:
  43. href = link_el.attrib.get("href") or (link_el.text or "").strip()
  44. else:
  45. href = ""
  46. guid = (guid_el.text or "").strip() if guid_el is not None else ""
  47. guid = guid or href
  48. if not guid:
  49. continue
  50. title_el = children.get("title")
  51. title = (title_el.text or "").strip() if title_el is not None else ""
  52. summ_el = children.get("description")
  53. if summ_el is None:
  54. summ_el = children.get("summary")
  55. summary = (summ_el.text or "").strip() if summ_el is not None else ""
  56. entries.append(
  57. {"id": guid, "title": title, "url": href, "summary": summary}
  58. )
  59. return entries
  60. def main() -> int:
  61. p = argparse.ArgumentParser(description="Watch an RSS/Atom feed.")
  62. p.add_argument("--name", required=True, help="Watcher name (used for state file)")
  63. p.add_argument("--url", required=True, help="Feed URL")
  64. p.add_argument("--max", type=int, default=10,
  65. help="Max new items to emit per tick (default: 10)")
  66. p.add_argument("--with-summary", action="store_true",
  67. help="Include <description>/<summary> snippet under each item")
  68. p.add_argument("--timeout", type=float, default=20.0,
  69. help="HTTP timeout in seconds (default: 20)")
  70. args = p.parse_args()
  71. try:
  72. req = urllib.request.Request(args.url, headers={"User-Agent": "Hermes-Watcher/1.0"})
  73. with urllib.request.urlopen(req, timeout=args.timeout) as resp:
  74. xml_bytes = resp.read()
  75. except urllib.error.HTTPError as e:
  76. print(f"watch_rss: HTTP {e.code} from {args.url}", file=sys.stderr)
  77. return 2
  78. except (urllib.error.URLError, TimeoutError, OSError) as e:
  79. print(f"watch_rss: network error: {e}", file=sys.stderr)
  80. return 2
  81. entries = _parse_feed(xml_bytes)
  82. wm = Watermark.load(args.name)
  83. new_items = wm.filter_new(entries, id_key="id")
  84. wm.save()
  85. # Cap emitted items (watermark still records all seen IDs so we don't
  86. # re-emit them next tick).
  87. if args.max > 0:
  88. new_items = new_items[: args.max]
  89. body_key = "summary" if args.with_summary else None
  90. output = format_items_as_markdown(new_items, body_key=body_key)
  91. if output:
  92. sys.stdout.write(output)
  93. # Empty stdout on no-new — cron treats that as silent.
  94. return 0
  95. if __name__ == "__main__":
  96. sys.exit(main())