~strahinja/dotfiles

dotfiles/bin/pagecheck -rwxr-xr-x 1.7 KiB
66fad124Страхиња Радић interesting.bit: New bit 2 days 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
71
72
#!/bin/sh

CACHEDIR=${HOME}/.cache/pagecheck
CACHEFILE=${CACHEDIR}/pagecheck.cache
PROG="${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"
DISP=$(ps ax -o cmd | grep Xorg | grep -v grep | sed 's/.*\(:[0-9]\+\).*/\1/')

trap "rm -f \"${TMPFILE}\"" HUP PIPE INT QUIT TERM EXIT
mkdir -p "$CACHEDIR"
touch "$CACHEFILE"

DATA=$(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')
if [ -n "${DATA}" ]; then
	if ! grep -F -e "${DATA}" "${CACHEFILE}" >/dev/null 2>&1; then
		printf '%s\\nURL: %s\\n' "${DATA}" "${URL}" > "${TMPFILE}"
		sed -e's/\\n/\n/g' "${TMPFILE}" | \
			ufold -w ${WRAPWIDTH} -s > "${NEWFILE}"
		print_new | neomutt -s "[${PROG}] New update" "${EMAIL}"
		BODY=$(head -n1 ${NEWFILE})
		[ -n "${DISP}" ] && \
			/usr/bin/env DISPLAY=${DISP} /usr/bin/notify-send \
				-i "${ICON}" "${TITLE}" "${BODY}"
		print_new
		cat "${TMPFILE}" >> "${CACHEFILE}"
	fi
fi