example usage:
$ yget 3
1. Cisco Acquires Splunk
<https://news.ycombinator.com/item?id=37596497>
591 points by siddharthb_ | 325 comments
2. Matrix 2.0: The Future of Matrix
<https://news.ycombinator.com/item?id=37599510>
151 points by jrepinc | 63 comments
3. Nippon Television has just acquired Studio Ghibli
<https://news.ycombinator.com/item?id=37596788>
454 points by sohkamyung | 146 comments
#!/bin/sh
# yget: get top stories on hn without a browser
# https://news.ycombinator.com/item?id=37601452
# this is a small shell script i wrote to try out the firebase API that
# prints HN posts one at a time into your terminal. it depends on curl
# and jq, but is otherwise fully portable.
yget () {
URI_FMT='https://hacker-news.firebaseio.com/v0'
URL_FMT='https://news.ycombinator.com/item?id='
USER=${USER:-$(whoami || id -un)}
YGET_DIR="${YGET_DIR:-/tmp/yget.$USER}"
mkdir -p "$YGET_DIR"
topstories="$YGET_DIR"/topstories.json
curl -s "$URI_FMT/topstories.json" > "$topstories"
if [ -n "$1" ]; then
if test "$1" -ge 1 2>/dev/null && test "$1" -le 500 2>/dev/null; then
post_left="$1"
elif test "$1" -lt 1 2>/dev/null || test "$1" -gt 500 2>/dev/null; then
printf "out of range!\n"
exit 1
else
printf "invalid input!\n"
exit 1
fi
else
post_left="4"
fi
post_count="$post_left"
while [ "$post_left" -gt 0 ]; do
rank=$((post_count - post_left))
num=$((rank + 1))
id=$(jq -r .["$rank"] "$topstories")
story="$YGET_DIR"/"$id".json
curl -s "$URI_FMT/item/$id.json" > "$story"
title=$(jq -r '.title' "$story")
score=$(jq -r '.score' "$story")
user=$(jq -r '.by' "$story")
comments=$(jq -r '.descendants' "$story")
printf "\n%3d. %s \n" "$num" "$title"
printf " <%s> \n" "$URL_FMT$id"
printf " %s points by %s | %s comments \n" "$score" "$user" "$comments"
post_left=$((post_left - 1))
done
}
yget "$1"