-
Notifications
You must be signed in to change notification settings - Fork 17
/
nextcloud-gotify-activity.sh
116 lines (82 loc) · 2.38 KB
/
nextcloud-gotify-activity.sh
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
#!/bin/bash
# By Georgiy Sitnikov.
# Nextcloud Domain without prefix
NextCloudDomain="cloud.domain.net"
# Nextcloud Activity RSS Token only
NextcloudActivityRssToken=""
### OR
# Nextcloud Activity RSS Full URL copied from the Nextcloud
NextCloudActivityRssUrl="https://domain/index.php/apps/activity/rss.php?token=123abc"
# Gotify URL
GotifyDomain="domain.net/gotify"
# Gotify Application Token
GotifyApplicationToken="1234"
LOCKFILE=/tmp/nextcloud-gotify-activity.lock
### END OF CONFIGURATION ###
CentralConfigFile="/etc/nextcloud-scripts-config.conf"
if [[ -f "$CentralConfigFile" ]]; then
. $CentralConfigFile
fi
curlConfiguration="-fsS -m 10 --retry 5"
if [[ -z "$NextCloudDomain" || -z "$NextcloudActivityRssToken" ]]; then
# Take RSS Full URL if token and Domain are not defined
ncRSS="$NextCloudActivityRssUrl"
else
# Build URL based on Token and Domain
ncRSS="https://$NextCloudDomain/index.php/apps/activity/rss.php?token=$NextcloudActivityRssToken"
fi
if [[ ! -f "$LOCKFILE" ]]; then
echo "0" > $LOCKFILE
fi
lastSeen=$(tail -n 1 $LOCKFILE)
# RSS XML Reader as per https://www.linuxjournal.com/content/parsing-rss-news-feed-bash-script
xmlgetnext () {
local IFS='>'
read -d '<' TAG VALUE
}
curl $curlConfiguration "$ncRSS" | sed '1!G;h;$!d' | while xmlgetnext ; do
case $TAG in
'/item')
guid=''
title=''
link=''
pubDate=''
description=''
;;
'guid isPermaLink="false"')
guid="$VALUE"
;;
'title')
title="$VALUE"
;;
'link')
link="$VALUE"
;;
'pubDate')
# convert pubDate format for <time datetime="">
datetime=$( date --date "$VALUE" --iso-8601=minutes )
pubDate=$( date --date "$VALUE" '+%d.%m.%Y %X' )
;;
'description')
# convert '<' and '>' to '<' and '>'
description=$( echo "$VALUE" | sed -e 's/</</g' -e 's/>/>/g' )
;;
'item')
if [[ "$guid" -gt "$lastSeen" ]]; then
if [[ ! -z "$description" ]]; then
message="$description"$'\n'
fi
if [[ ! -z "$link" ]]; then
link="$link"$'\n'
fi
# Create Gotify message Body
message="$description$link""Posted on $pubDate"$'\n'"Nextcloud activity id $guid"
# Send notification to Gotify
curl $curlConfiguration -X POST "https://$GotifyDomain/message?token=$GotifyApplicationToken" -F "title=\"$title\"" -F "message=\"$message\"" >> /dev/null
# Remember last seen message ID from the Nextcloud
echo $guid > $LOCKFILE
fi
;;
esac
done
exit 0