scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

feed (1375B)


      1 #!/bin/sh
      2 
      3 # RSS feed manager
      4 
      5 # Requires: sfeed, sfeed_plain (get), dmenu, open-url (menu)
      6 
      7 # Usage: feed [-m menu] [get|menu|clear|show]
      8 
      9 dir=$HOME/box/sfeed
     10 feeddir=$dir/urls
     11 destdir=$dir/new
     12 readdir=$dir/last
     13 menu="dmenu -l 20 -i"
     14 urlopener=open-url
     15 
     16 usage() {
     17 	echo "Usage: feed [get|menu|clear|show]"
     18 }
     19 
     20 getnew() {
     21 	for f in "$feeddir"/*; do
     22 		read -r url < "$f"
     23 		name=$(basename "$f")
     24 		d="$destdir/$name"
     25 		r="$readdir/$name"
     26 
     27 		[ -f "$r" ] && read -r lr < "$r" || lr=0
     28 
     29 		# Get new feed items
     30 		tmp=$(mktemp)
     31 		curl -s "$url" | sfeed | \
     32 		awk -v lr="$lr" '$1 > lr {print $0}' | \
     33 		tee "$tmp" | sfeed_plain >> "$d"
     34 
     35 		# Update last time stamp
     36 		awk -v lr="$lr" '$1 > lr {lr=$1} END {print lr}' <"$tmp" >"$r"
     37 	done
     38 }
     39 
     40 show() {
     41 	for f in "$destdir"/*; do
     42 		ff=$(basename "$f")
     43 		if [ -s "$f" ]; then
     44 			sort -r "$f" | while read -r line; do
     45 				printf '%20s    %s\n' "$ff" "$line"
     46 			done
     47 		fi
     48 	done
     49 }
     50 
     51 selectmenu() {
     52 	$menu | awk '{print $NF}' | xargs $urlopener
     53 }
     54 
     55 while getopts "m:" opt; do
     56 	case "$opt" in
     57 		m)
     58 			menu="$OPTARG"
     59 			;;
     60 		*)
     61 			usage
     62 			exit 1
     63 			;;
     64 	esac
     65 done
     66 
     67 shift $((OPTIND - 1))
     68 
     69 if [ -z "$1" ]; then
     70 	usage
     71 	exit 1
     72 fi
     73 
     74 case "$1" in
     75 	get)
     76 		getnew
     77 		countnew=$(cat "$destdir"/* | wc -l)
     78 		echo "$countnew new feed items"
     79 		;;
     80 	menu)
     81 		show | selectmenu
     82 		;;
     83 	clear)
     84 		rm -r "$destdir"/*
     85 		;;
     86 	show)
     87 		show
     88 		;;
     89 	*)
     90 		usage
     91 		exit 1
     92 		;;
     93 esac