~strahinja/dotfiles

ref: 183952d86e838d2392af864eadc53d494f232d95 dotfiles/bin/pagecheck -rwxr-xr-x 1.5 KiB
183952d8Страхиња Радић Replaced sed calls with ufold 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/sh

CACHEDIR=$HOME/.cache/pagecheck
CACHEFILE=$CACHEDIR/pagecheck.cache
PROG=$(basename $0)
NEWFILE=$CACHEDIR/pagecheck.new
WRAPWIDTH=80

print_new()
{
	sed -e"s/\\\\e/\x1b/g" <<EOT
================================================================================
Modified: $(stat -c %y $NEWFILE)
================================================================================

$(cat $NEWFILE)

================================================================================
EOT
}

case "$1" in
	-s|--seen)
		rm $NEWFILE 2>/dev/null
		exit 1
		;;

	-h|--help)
		echo "Usage: $PROG [[-h|--help] | [-s|--seen] | [regexp]" \
			"[url] [email]]"
		exit 1
		;;
esac

if [ $# -lt 2 ]; then
	if [ -f "$NEWFILE" ]; then
		print_new
	else
		echo "$PROG: Nothing new"
	fi
	exit 0
fi

TMPFILE=$(mktemp --tmpdir -q pagecheck.XXXXX)
REGEXP=$1
URL=$2
EMAIL=$3
ICON=dialog-information
TITLE="$PROG: New update"

mkdir -p $CACHEDIR
touch $CACHEFILE
curl -s $URL | grep $REGEXP | sed -e's/<p>/\\n\\n/g' | sed -ze's/\n/\\n/g' | \
	sed -e's/<[^>]\+>//g' -e's/\s\+/ /g' -e's/^\s\+//g' > $TMPFILE
if [ $(( $(wc -c $TMPFILE | cut -d' ' -f1) )) -gt 0 ]; then
	if ! grep -F -f $TMPFILE $CACHEFILE &>/dev/null; then
		echo "\\nURL: $URL\\n" >> $TMPFILE
		ufold -w $WRAPWIDTH $TMPFILE > $NEWFILE
		#cat $TMPFILE | sed -e's/\\n/\n/g' | \
		#	sed "s/.\\{$WRAPWIDTH\\}/&\\n/g" > $NEWFILE
		print_new | neomutt -s "[$PROG] New update" $EMAIL
		BODY=$(print_new)
		notify-send -i $ICON "$TITLE" "$BODY"
		print_new
		cat $TMPFILE >> $CACHEFILE
	fi
fi

rm $TMPFILE