Skip to content

Instantly share code, notes, and snippets.

@normcyr
Forked from Ironjanowar/reddit-popular.sh
Last active August 1, 2017 15:11
Show Gist options
  • Save normcyr/b049e04cab53e87f17fb6ec10a8f6c83 to your computer and use it in GitHub Desktop.
Save normcyr/b049e04cab53e87f17fb6ec10a8f6c83 to your computer and use it in GitHub Desktop.
Bash script to return the last 8 posts from a subreddit
#!/bin/sh
#
# usage: ./reddit-rss-feeder.sh subreddit-name hot/new/rising/controversial/top/gilded
# default subreddit-name = popular
# default tab = hot
# e.g.: ./reddit-rss-feeder.sh bikewrench new
#
# This script uses rsstail to retrieve latests entries from Reddit Bikecommuting rss feed
# and process them through awk. Each update shows a timestamp separator.
#
# Slightly modified from jvalencia80 (https://gist.github.com/jvalencia80/101f57cf9294e9410f69b95a9a78e6ae) by Ironjanowar (https://gist.github.com/Ironjanowar/527f274017b406f486060a2e89bf97e8) and minor additions by normcyr
#
# It will work with most RSS feeds out there.
#
# rsstail options used here (according to rsstail code, https://github.com/flok99/rsstail):
# -r: print in reverse order (newest first)
# -l: show link
# -H: string HTML
# -u: input URL
# -i: interval check in seconds
# -n: number of posts to return
if [ $# -eq 1 ]; then
subreddit="r/$1"
tab="hot"
elif [ $# -eq 2 ]; then
subreddit="r/$1"
tab="$2"
else
subreddit="r/popular"
fi
echo "Getting /$subreddit/$tab"
rsstail -rlH -u https://www.reddit.com/$subreddit/$tab/.rss -i 300 -n 8 | awk '
BEGIN {
ResetColor = "\033[0m"
BrightColor = "\033[1;33m"
DarkColor = "\033[1;30m"
last_update = systime()
}
/^Title:/ {
title = $0
sub(/Title:/, "", title)
}
/^Link:/ {
link = $0
sub(/Link:/, "", link)
}
title && link {
# timestamp spam protection (30 seconds)
if (systime() - last_update >= 30) {
print DarkColor "---[ " strftime("%H:%M") " ]-------------" ResetColor
print ""
last_update = systime()
}
print BrightColor title ResetColor
print DarkColor link ResetColor
print ""
title = link = ""
}
'
@normcyr
Copy link
Author

normcyr commented Aug 1, 2017

Need to deal with error if subreddit does not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment