blob: fd88d1baac7ba357e51c3560d8e8253582bdbb46 (
plain)
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
|
#!/bin/sh
# Copyright 2016 Jan Verbeek (blyxxyz) <ring@openmailbox.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# This script keeps track of URLs in RSS feeds and opens new ones.
# New feeds can be added with ':spawn -u /path/to/userscripts/rss add' or
# ':spawn -u /path/to/userscripts/rss <url>'.
# New items can be opened with ':spawn -u /path/to/userscripts/rss'.
# The script doesn't really parse XML, and searches for things that look like
# item links. It might open things that aren't real links, and it might miss
# real links.
config_dir="$HOME/.qute-rss"
add_feed () {
touch "feeds"
if grep -Fq "$1" "feeds"; then
notice "$1 is saved already."
else
printf '%s\n' "$1" >> "feeds"
fi
}
# Show an error message and exit
fail () {
echo "message-error '$*'" > "$QUTE_FIFO"
exit 1
}
# Get a sorted list of item URLs from a RSS feed
get_items () {
$curl "$@" | grep "$text_only" -zo -e '<guid[^<>]*>[^<>]*</guid>' \
-e '<link[^<>]*>[^<>]*</link>' \
-e '<link[^<>]*href="[^"]*"' |
grep "$text_only" -o 'http[^<>"]*' | sort | uniq
}
# Show an info message
notice () {
echo "message-info '$*'" > "$QUTE_FIFO"
}
# Update a database of a feed and open new URLs
read_items () {
cd read_urls || return 1
feed_file="$(echo "$1" | tr -d /)"
feed_temp_file="$(mktemp "$feed_file.tmp.XXXXXXXXXX")"
feed_new_items="$(mktemp "$feed_file.new.XXXXXXXXXX")"
get_items "$1" > "$feed_temp_file"
if [ ! -s "$feed_temp_file" ]; then
notice "No items found for $1."
rm "$feed_temp_file" "$feed_new_items"
elif [ ! -f "$feed_file" ]; then
notice "$1 is a new feed. All items will be marked as read."
mv "$feed_temp_file" "$feed_file"
rm "$feed_new_items"
else
sort -o "$feed_file" "$feed_file"
comm -2 -3 "$feed_temp_file" "$feed_file" | tee "$feed_new_items"
cat "$feed_new_items" >> "$feed_file"
sort -o "$feed_file" "$feed_file"
rm "$feed_temp_file" "$feed_new_items"
fi | while read -r item; do
echo "open -t $item" > "$QUTE_FIFO"
done
}
if [ ! -d "$config_dir/read_urls" ]; then
notice "Creating configuration directory."
mkdir -p "$config_dir/read_urls"
fi
cd "$config_dir" || exit 1
if [ $# != 0 ]; then
for arg in "$@"; do
if [ "$arg" = "add" ]; then
add_feed "$QUTE_URL"
else
add_feed "$arg"
fi
done
exit
fi
if [ ! -f "feeds" ]; then
fail "Add feeds by running ':spawn -u rss add' or ':spawn -u rss <url>'."
fi
if curl --version >&-; then
curl="curl -sL"
elif wget --version >&-; then
curl="wget -qO -"
else
fail "Either curl or wget is needed to run this script."
fi
# Detect GNU grep so we can force it to treat everything as text
if < /dev/null grep --help 2>&1 | grep -q -- -a; then
text_only="-a"
fi
while read -r feed_url; do
read_items "$feed_url" &
done < "$config_dir/feeds"
wait
|