read-reddit-thread
Purpose
Fetch a Reddit post or comment and its surrounding thread context using Reddit's public JSON API. No API key required.
Inputs
- A
reddit.comorold.reddit.comURL (post or comment)
Workflow
- Clean the URL: strip query params, remove trailing slashes, normalize
old.reddit.com→reddit.com. - Append
.jsonto the URL and fetch with aUser-Agentheader (Reddit blocks requests without one — useBuzz/1.0). - Parse the response:
- Post URL (
/r/sub/comments/id/slug): Returns[listing, comments]. The first listing contains the post, the second contains the comment tree. - Comment URL (
/r/sub/comments/id/slug/comment_id): Same structure, but the comment tree is focused on the linked comment and its ancestors.
- Post URL (
- Extract the post:
data.children[0].datafrom the first listing. Pulltitle,selftext,author,score,num_comments,created_utc,subreddit_name_prefixed. - Walk the comment tree from the second listing. For each comment, extract
author,body,score,created_utc. Recurse intorepliesto get the full nested thread. Flatten into a list preserving depth (indent or number by depth level). - Return the full thread — post first, then all comments in tree order.
Example request
curl -sS "https://www.reddit.com/r/commandline/comments/abc123/my_post_title.json" \
-H "User-Agent: Buzz/1.0"
Return
Post: "Post title here" by u/author (r/subreddit, 142 points, 37 comments)
---
Post body text here...
---
Comments:
[1] u/commenter1 (52 pts):
Top-level comment text...
[1.1] u/replier (23 pts):
Reply to commenter1...
[1.1.1] u/deeper (8 pts):
Deeper reply...
[2] u/commenter2 (31 pts):
Another top-level comment...
Use depth numbering (1, 1.1, 1.1.1) to show the thread structure.
Failure handling
- Reddit returns 403/429 → report rate limit, suggest retrying later.
- URL doesn't match Reddit format → report invalid URL.
- Post is deleted/removed → report that the post is unavailable.
- If the response is very large (>100 comments), still return all of them — the caller needs the full context.