summaryrefslogtreecommitdiff
path: root/misc/userscripts/rss
blob: 3c52d1f276d17116ab583e79e6fc39f780faaa73 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh

# Copyright 2016 Jan Verbeek (blyxxyz) <ring@openmailbox.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

# 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