-
Notifications
You must be signed in to change notification settings - Fork 0
/
sakra2
executable file
·179 lines (145 loc) · 3.54 KB
/
sakra2
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh -e
# shellcheck disable=SC1003
help() { echo 'sakra2 [-cv] [FEEDGROUP[/FEEDID]]...' >&2 ; }
TAB=$(printf '\t')
export SAKRAHOME="${SAKRAHOME:-${HOME:?}}"
case "$SAKRAHOME" in -*) exit 1 ;; esac
SAKRADIR="${SAKRADIR:-${SAKRAHOME:?}/.sakra2}"
case "$SAKRADIR" in -*) exit 1 ;; esac
mkdir -p "$SAKRADIR" "$SAKRAHOME"
cd "$SAKRADIR"
unset -v catchup NV
V=0
while getopts cvh o; do
case "$o" in
c) catchup=1 ;;
v) export V=$((V+1)) ;;
h) help; exit 0 ;;
*) help; exit 1 ;;
esac
done
shift $((OPTIND-1))
case "$V" in
0) export NV=1; unset -v V;;
1) ;;
2) export VV=1;;
3) export VV=1 VVV=1;;
esac
lock() {
if command -v flock > /dev/null 2>&1; then
exec 9> "$1"
flock 9
else
while ! mkdir "$1"; do
sleep 1
done
trap 'trap - EXIT HUP QUIT TERM INT ABRT; rmdir "'"$1"'"' EXIT HUP QUIT TERM INT ABRT
fi
}
# this runs in the same context as a user's rc script
defaultrc() {
set -C
[ "$LINK" ] || [ "$ENCLOSURE" ] || exit 0
target=${ENCLOSURE:-$LINK}
# add file extensions when applicable
if [ "$ENCLOSURE" ]; then
path=${target##*/}; path=${path%%[?#]*};
ext=${path##*.}
else
ext=html
fi
# carefully assemble filename, sub / for \
name="${FEEDNAME:-$AUTHOR}"
title="${TITLE:-${TIMESTAMP:-$(date +%c)}}"
filename=$(tr '/' '\\') <<-EOF
${name:+$name - }${title}${ext:+.$ext}
EOF
mkdir -p "$SAKRAHOME/$FEEDGROUP"
curl -LSsf${VVV:+v} -- "$target" > "$SAKRAHOME/$FEEDGROUP/$filename"
}
handle_feed() {
while IFS= read -r item; do
full_item="$item"
if [ "$VVV" ]; then
echo "$full_item" >&2
fi
unset -v TIMESTAMP TITLE LINK CONTENT CONTENT_TYPE GUID AUTHOR ENCLOSURE old
for field in TIMESTAMP TITLE LINK CONTENT CONTENT_TYPE GUID AUTHOR ENCLOSURE; do
eval export "$field="'${item%%"$TAB"*}'
# skip old entries
if [ "$field" = GUID ]; then
if [ "$GUID" ] && grep -Fxqe "$GUID" < "$STATE"; then
old=1; break;
elif grep -Fxqe "$full_item" < "$STATE"; then
old=1; break;
fi
fi
case "$item" in *"$TAB"*) ;; *) break ;; esac # no more fields = we're done
item=${item#*"$TAB"} # remove $field from $item
done
if [ "$old" ]; then continue; fi
if [ "$catchup" ]; then
printf '%s\n' "$GUID" >> "$STATE"
continue
fi
if [ -x "$RC" ]; then
"$RC" || continue
elif [ -f "$RC" ]; then
echo "$RC exists but isn't executable" >&2; return 1;
else
(defaultrc) || continue
fi
printf '%s\n' "$GUID" >> "$STATE" # mark item as old
done
}
iterate_feeds() {
while read -r feed FEEDNAME; do
[ "$feed" ] || continue # skip empty lines
export FEEDNAME
if [ "$FEEDNAME" ]; then
FEEDID="$FEEDNAME"
else
FEEDID=$(tr -d /) <<-EOF
${feed#*:https://}
EOF
fi
if [ "$1" ] && [ "$FEEDID" != "$1" ]; then
continue
fi
mkdir -p ./state/"$FEEDGROUP"
export STATE="./state/$FEEDGROUP/$FEEDID"
: >> "$STATE"
lock "$STATE.lock"
if [ "$VV" ]; then
echo Working on feed "$FEEDGROUP/${FEEDID:-$feed}" ... >&2
fi
{ curl -LSsf${VVV:+v} -- "$feed" | sfeed | handle_feed; } || :
done
}
iterate_feedgroups() {
for feeds; do
if [ "$V" ]; then
echo Working on feedgroup "$feeds" ... >&2
fi
# implements FEEDGROUP/FEEDID syntax
unset -v FEEDID
case "$feeds" in
*/*) FEEDID=${feeds#*/}; feeds=${feeds%%/*}.feeds ;;
*.feeds) ;;
*) feeds="$feeds.feeds"
esac
[ -f "$feeds" ] || continue
export FEEDGROUP="${feeds%.feeds}"
export RC="./${FEEDGROUP}rc"
if [ "$FEEDID" ]; then
iterate_feeds "$FEEDID" < "$feeds"
else
iterate_feeds < "$feeds"
fi
done
}
if [ "$*" ]; then
iterate_feedgroups "$@"
else
iterate_feedgroups *.feeds
fi