Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created August 18, 2026 11:24
Show Gist options
  • Select an option

  • Save skorotkiewicz/483463d6d7495a7fa00667e2b223ec3a to your computer and use it in GitHub Desktop.

Select an option

Save skorotkiewicz/483463d6d7495a7fa00667e2b223ec3a to your computer and use it in GitHub Desktop.
Fetch Hacker News threads by ID and recursively find my comments.
#!/usr/bin/env python3
import html
import json
import os
import re
import ssl
import sys
from urllib.request import Request, urlopen
USERNAMES = {"modinfo"}
CA_FILE = "/etc/ssl/certs/ca-certificates.crt"
SSL_CONTEXT = ssl.create_default_context(
cafile=CA_FILE if os.path.exists(CA_FILE) else None
)
def fetch(ident):
request = Request(
f"https://hn.algolia.com/api/v1/items/{ident}",
headers={"User-Agent": "hn.py/1.0"},
)
with urlopen(request, timeout=30, context=SSL_CONTEXT) as response:
return json.load(response)
def clean(text):
text = html.unescape(re.sub(r"<[^>]+>", " ", text or ""))
return re.sub(r"\s+", " ", text).strip()
def print_user_comments(comments):
for comment in comments:
if comment.get("author") in USERNAMES:
print(
"USER COMMENT",
comment["author"],
comment.get("id"),
":",
clean(comment.get("text")),
)
print_user_comments(comment.get("children", []))
def main(ids):
if not ids or any(not ident.isdigit() for ident in ids):
raise SystemExit(f"Usage: {sys.argv[0]} <hn_id> [hn_id ...]")
for ident in ids:
item = fetch(ident)
print(f"\n=== {ident} ===")
print("title:", item.get("title"))
print("author:", item.get("author"))
print("points:", item.get("points"))
print("url:", item.get("url"))
print("top comments:", len(item.get("children", [])))
print_user_comments(item.get("children", []))
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment