Hackernews comment to AI summary

A recurring theme is that I have a lot of terminal helper scripts for shortcuts.

Inspired by and modified from Summarizing Hacker News discussion themes with Claude and LLM. I modified the bash script that fetches the HN title and all comments on a post and summarizes their themes and opinions, quoting users and keeping the summary short.

A post may be popular, with hundreds of comments, and I don’t want to read them all. I want only the short-form themes expressed.

Why This Approach?

This script works best if you already have a code editor with an integrated AI chat (VS Code, Zed, etc.). Run the script, open the .md file in your editor, and the AI reads the embedded instructions and generates a summary.

Usage:

$ hn-summary
Please provide a valid Hacker News URL or item ID.

Usage: hn-summary [hn_url_or_id]
Description: Fetches Hacker News comments and saves them as markdown with auto-generated filename
Examples:
  hn-summary https://news.ycombinator.com/item?id=12345
  hn-summary 12345
$ hn-summary https://news.ycombinator.com/item?id=27995270
Saved to: /home/user/Downloads/hn-summary/never-gonna-give-you-up-by-rick-astley-reaches-one-billion-views-video.md

Then I open the .md file in a code editor with an AI chat window and have it summarize according to the instructions in the file.


Name:

~/bin/hn-summary

Source:

#!/bin/bash

# Function to slugify a string for use as a filename
slugify() {
    echo "$1" | sed 's/[^a-zA-Z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-\|-$//g' | tr '[:upper:]' '[:lower:]'
}

# Extract ID from URL or use direct ID
if [[ $1 =~ ^https?://news\.ycombinator\.com/item\?id=([0-9]+) ]]; then
    id="${BASH_REMATCH[1]}"
elif [[ $1 =~ ^[0-9]+$ ]]; then
    id="$1"
else
    echo "Please provide a valid Hacker News URL or item ID."
    echo
    echo "Usage: $(basename $0) [hn_url_or_id]"
    echo "Description: Fetches Hacker News comments and saves them as markdown with auto-generated filename"
    echo "Examples:"
    echo "  $(basename $0) https://news.ycombinator.com/item?id=12345"
    echo "  $(basename $0) 12345"
    exit 1
fi

# Make the curl request to hn.algolia.com and store JSON response
json_response=$(curl -s "https://hn.algolia.com/api/v1/items/$id")

# Extract title for filename
title=$(echo "$json_response" | jq -r '.title // "untitled"')

# Create output directory
output_dir="$HOME/Downloads/hn-summary"
mkdir -p "$output_dir"

# Generate filename
filename=$(slugify "$title").md
filepath="$output_dir/$filename"

# Format comments
#  uses `#` for comment parent/children indentation
comments=$(echo "$json_response" | \
  jq -r '("# Article: " + (.title // "No title")), ("## URL: " + (.url // "No URL")), "",
        (def walk(d): . as $item | (("#" * (d+1) + " " + .author + ": " + .text), (.children[]? | walk(d+1))); walk(0))')

# disable command output
# set +x

# Create the content with prompt and comments
content="Summarize the themes of the opinions expressed here.
  For each theme, output a markdown header.
  Under the header, include a sentence or two summarizing the point or quotes of the theme.
  Include direct \"quotations\" where appropriate nested as bullet points last.
  You MUST quote directly from users when crediting them, with double quotes.
  Fix HTML entities. Output markdown. Go short.

$comments"

# Save to file
echo "$content" > "$filepath"

# Output the saved file path
echo "Saved to: $filepath"

#Ai   #Prompt   #Scripts   #Bash   #Hackernews