Skip to content

Instantly share code, notes, and snippets.

@typebrook
Last active January 4, 2023 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typebrook/a4de3249d9b0c0aadc22320497373140 to your computer and use it in GitHub Desktop.
Save typebrook/a4de3249d9b0c0aadc22320497373140 to your computer and use it in GitHub Desktop.
Coninuouly print news from RSS
#! /bin/sh
RSS_URL=http://feeds.bbci.co.uk/news/rss.xml
#RSS_URL=http://feeds.bbci.co.uk/news/rss.xml
PRINT_INTERVAL=${PRING_INTERVAL:-30}
FETCH_INTERVAL=${FETCH_INTERVAL:-600}
RSS=$(mktemp)
LOCK=$(mktemp)
latest_news_time=$(date --date -1day +%s)
print_news_from_rss_file() {
xq -c '.rss.channel.item[]' $RSS | \
tac | \
while read -r record; do
date="$(jq -r .pubDate <<<"$record")"
epoch=$(date -d "$date" +%s)
if [[ $epoch -gt $latest_news_time ]]; then
latest_news_time=$epoch
echo $latest_news_time >$LOCK
else
continue
fi
# Print headline
echo -en '\e[1;30;41m'
jq -r .title <<<"$record"
echo -en '\e[0m\e[K'
# Print publish date
echo -en '\e[0;3m'
echo " $date"
echo -en '\e[0m'
printf '\n'
# Print summary
jq -r .description <<<"$record" | grep -Eo "^[^<]+"
# Print link
echo -en '\e[0;3;34m'
jq -r .link <<<"$record"
echo -en '\e[m'
printf '\n\n'
sleep $PRINT_INTERVAL
done
}
while true; do
if [ -s $RSS ]; then
RSS_CTIME=$(stat -c %Y $RSS)
else
RSS_CTIME=0
fi
time_from_last_fetch=$(( $(date +%s) - $RSS_CTIME ))
if [[ $time_from_last_fetch -le $FETCH_INTERVAL ]]; then
sleep $(( $FETCH_INTERVAL - $time_from_last_fetch ))
fi
curl -s $RSS_URL >$RSS
print_news_from_rss_file
latest_news_time=$(cat $LOCK)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment