about summary refs log tree commit diff stats
path: root/runsfeed
blob: 6f6b161afe325e9432ba94705ee9834ec1f404db (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
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
179
180
181
182
#!/usr/bin/env bash
# Run sfeed
# shellcheck disable=2016
# set -euo pipefail

main() {
	NOFETCH=false
	[ "x$1" = "x-n" ] && {
		NOFETCH=true
		shift
	}
	[ -n "$1" ] && SFEED_OUTPUT="$1" || SFEED_OUTPUT="$HOME/.sfeed"
	export SFEED_OUTPUT
	export NOFETCH
	export SFEED_CONFIG="$HOME/.sfeed"
	# SFEED_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/sfeed"
	export SFEED_DATA="$HOME/.sfeed"
	# SFEED_DATA="${XDG_DATA_HOME:-$HOME/.local/share}/sfeed"
	# SFEED_OUTPUT=/var/www/sfeed
	export sfeedrc="$SFEED_CONFIG/sfeedrc"
	export sfeedpath="$SFEED_DATA/feeds"
	test -d "$(dirname "$sfeedrc")" || mkdir -p "$(dirname "$sfeedrc")"
	test -d "$sfeedpath" || mkdir -p "$sfeedpath"
	touch /tmp/runsfeed.ok

	# if ! $NOFETCH; then
	# logok "Finding Invidious host" \
	# get_invidious_url "https://api.invidious.io/instances.json?sort_by=health"
	# fi
	logok "Removing unsubscribed feeds" \
		remove_unsubs "$sfeedrc"
	logok -n "Updating feeds" \
		update "$sfeedrc"
	logok "Updating urls" \
		update_urls "$sfeedrc"
	logok "Generating HTML" '{
		html "$sfeedpath"/* >/tmp/sfeed-index.html &&
			mv /tmp/sfeed-index.html "$SFEED_OUTPUT/index.html";
		html -v LIMIT=-1 "$sfeedpath"/* >/tmp/sfeed-feeds.html &&
			mv /tmp/sfeed-feeds.html "$SFEED_OUTPUT/feeds.html";
	}'
	logok "Generating RSS" '{
		atom "$sfeedpath" >/tmp/feeds.xml &&
			mv /tmp/feeds.xml "$SFEED_OUTPUT/feeds.xml";
		atom "$sfeedpath" 7 >/tmp/feeds-short.xml &&
			mv /tmp/feeds-short.xml "$SFEED_OUTPUT/feeds-short.xml";
	}'
	logok "Generating OPML" '{
		opml "$sfeedrc" >/tmp/feeds.opml &&
			mv /tmp/feeds.opml "$SFEED_OUTPUT/feeds.opml";
	}'
	logok "Generating twtxt" '{
		twtxt "$sfeedpath" >/tmp/feeds.txt &&
			mv /tmp/feeds.txt "$SFEED_OUTPUT/feeds.txt";
	}'
	logok "Generating ass" '{
		ass "$sfeedpath" >/tmp/feed.ass &&
			mv /tmp/feed.ass "$SFEED_OUTPUT/feed.ass";
	}'
	logok "Archiving old feeds" \
		'archive 60 "$sfeedpath"/*'
	logok "Weeding" '{
		echo
		mkdir -p "$SFEED_OUTPUT/weeds";
		weed > /tmp/weeds.html &&
			mv /tmp/weeds.html "$SFEED_OUTPUT/weeds/index.html";
	}'
	robotstxt >"$SFEED_OUTPUT/robots.txt"
	if [ -f /tmp/runsfeed.ok ]; then
		echo >&2 'Done.'
	else
		echo >&2 'Done (some errors).'
	fi
}

runcmd() {
	cmd="$(command -v "$1" || echo "./$1")"
	shift
	"$cmd" "$@"
}

remove_unsubs() {
	runcmd sfeed_unsubscribe.sh "$@"
}

update_urls() {
	runcmd sfeed_update_urls.sh "$@"
}

get_invidious_url() {
	"${NOFETCH:-false}" && return
	curl -sL "$1" |
		jq -r .[][1].uri |
		grep -v onion |
		head -n1 | tee /tmp/invidious.host
}

logok() {
	newline=''
	if [ "x$1" = x-n ]; then
		newline='\n'
		shift
	fi
	printf "%s...$newline" "$1" >&2
	shift
	if output="$(eval "$@" | tee /dev/stderr)"; then
		printf '%s\n' "${output:-ok}"
		return 0
	else
		rm /tmp/runsfeed.ok
		printf 'ERROR\n'
		return 1
	fi
}

update() {
	runcmd sfeed_update_xargs "$@"
}

aggregate() {
	curd="$PWD"
	cd "$1" || return 1
	if [ -n "$2" ]; then
		old="$(($(date +%s) - ($2 * 24 * 3600)))"
	else
		old=0
	fi
	awk -v old="$old" \
		'BEGIN{FS="\t";OFS="\t";} int($1)>=old{$2="["FILENAME"] "$2;print}' \
		* | sort -k1,1rn
	cd "$curd" || return
}

ass() {
	aggregate "$1" "$2" | runcmd sfeed_ass.awk
}

twtxt() {
	aggregate "$1" "$2" | sfeed_twtxt
}

opml() {
	sfeed_opml_export "$@"
}

html() {
	runcmd sfeed_html.awk "$@"
}

atom() ( # atom DIRECTORY [DAYS]
	aggregate "$1" "$2" | sfeed_atom
)

weed() (
	# Generate a page for weeds.
	runcmd sfeed_update_xargs weedrc >&2
	. weedrc
	aggregate "$sfeedpath" | runcmd sfeed_weed.awk
)

archive() ( # sfeed_archive AGE FEED ...
	age="$1"
	shift
	# shellcheck disable=2031
	old="$sfeedpath/../sfeed.old.d"
	mkdir -p "$old"
	for feed; do
		awk -v old="$(($(date +%s) - (age * 24 * 3600)))" \
			-F '\t' 'int($1) > old' <"$feed" >"$feed.new"
		mv "$feed" "$old/${feed##*/}" 2>/dev/null &&
			mv "$feed.new" "$feed" 2>/dev/null
	done
)

robotstxt() {
	echo "User-Agent: *"
	echo "Disallow: /"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	main "$@"
fi