Coninuouly print news from RSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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