-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc-atom-notice-wiki
executable file
·127 lines (93 loc) · 2.95 KB
/
irc-atom-notice-wiki
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
#! /usr/bin/env bash
# © 2022 Unvanquished Developers
# MIT License https://spdx.org/licenses/MIT.html
export shell_opts="${shell_opts:--u -e -o pipefail}"
set ${shell_opts}
export slug='wiki'
export atom_url='https://wiki.unvanquished.net/api.php?hidebots=1&days=1&limit=10&action=feedrecentchanges&feedformat=atom'
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "${script_dir}"
script_name="$(basename "${BASH_SOURCE[0]}")"
. 'irc-atom-notice.conf'
case "${1:-}" in
'-h'|'--help')
printf '%s <interval-in-seconds>\n' "${script_name}"
exit
;;
[0-9][0-9]*)
interval="${1}"
;;
esac
export interval
colorString () {
set ${shell_opts}
local color="${1}"; shift
local string="${1}"; shift
printf '\x03%s%s\x0f' "${color}" "${string}"
}
export -f colorString
processEntry () {
set ${shell_opts}
local entry="${1}"
local date
local url
local nick
local title
printf '%s\n' "${entry}" \
| tr '\t' '\n' \
| while read date
do
read url
read nick
read title
title="$(printf '%s' "${title}" | html2text -nobs -utf8 -width 999 | head -n1)"
local tag="${slug}"
local channel='#unvanquished-dev'
local c_tag="$(colorString '08,15' "${tag}")"
local c_nick="$(colorString '10' "${nick}")"
local c_title="$(colorString '03' "${title}")"
local message="[${c_tag}] ${c_nick} edited “${c_title}”: <${url}>"
# MediaWiki already sets the Z UTC suffix.
local commit_epoch="$(date --date="${date}" '+%s')"
local current_epoch="$(date '+%s')"
local entry_age="$((${current_epoch} - ${commit_epoch}))"
local purgatory_time="$((${interval} / 60))"
if [ "${purgatory_time}" -lt 5 ]
then
purgatory_time=5
if [ "${purgatory_time}" -gt "${interval}" ]
then
purgatory_time=1
fi
fi
local allowed_age="$((${interval} + ${purgatory_time}))"
if [ "${entry_age}" -le "${allowed_age}" ]
then
log_date="$(date --rfc-3339=s --utc --date="@${commit_epoch}")"
cat >> "${log_file}" <<-EOF
"${log_date}: ${MSGCMD:-./irc-msg} "${channel}" "${message}"
EOF
${MSGCMD:-./irc-msg} "${channel}" "${message}"
fi
done
}
export -f processEntry
mkdir -p "${cache_dir}"
before_tsv="${cache_dir}/${slug}-before.tsv"
current_tsv="${cache_dir}/${slug}-current.tsv"
current_atom="${cache_dir}/${slug}-current.atom"
mkdir -p "${log_dir}"
export log_file="${log_dir}/${slug}.log"
touch "${log_file}"
touch "${current_tsv}"
mv "${current_tsv}" "${before_tsv}"
curl -s "${atom_url}" > "${current_atom}"
# The TSV file may not be produced if the Atom feed doesn't have any entry.
# The MediaWiki Atom feed doesn't have a published date field, only an updated date one. We track Wiki editions anyway.
xsltproc --nonet --output "${current_tsv}" 'atom-tsv-updated.xslt' "${current_atom}"
touch "${current_tsv}"
sort "${current_tsv}" -o "${current_tsv}"
comm -1 -3 "${before_tsv}" "${current_tsv}" \
| tr '\n' '\0' \
| xargs -0 -r -n1 -P1 \
/usr/bin/env bash ${shell_opts} -c 'processEntry "${@}"' processEntry