forked from bigpresh/ical-to-google-calendar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathics-sync.sh
executable file
·286 lines (258 loc) · 11.6 KB
/
ics-sync.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
#------------------------------------------#
# iCal2GoogleCalendar #
#------------------------------------------#
# #
# Synchronize an ics calendar to Google #
# Calendar (one way only) #
# #
# Yvan Godard #
# #
# Version 3.0 -- february, 21 2022 #
# Under Licence #
# Creative Commons 4.0 BY NC SA #
# #
# http://goo.gl/AQjEnM #
# #
#------------------------------------------#
# Variables initialisation
VERSION="iCal2GoogleCalendar v3.0 2022, Yvan Godard [[email protected]]"
help="no"
SCRIPT_NAME=$(basename $0)
SCRIPT_DIR=$(dirname $(realpath $0))
SCRIPT_PARENT_DIR=$(dirname ${SCRIPT_DIR%/})
PYTHON_ICS_CLEANER=${SCRIPT_DIR}/ical-to-gcal.py
URL_CALENDAR=""
LOCAL_FILE=""
NETRC_CONFIG=""
CALENDAR_GCAL=""
PATH_ICS=""
CURL_USER=""
CURL_PASS=""
LOG=/var/log/ics_sync.log
LOG_ACTIVE=0
LOG_TEMP=$(mktemp /tmp/tmp_log_ics_sync.XXXXX)
ERROR=0
EMAIL_REPORT="nomail"
EMAIL_LEVEL=0
WITH_USER=0
POETRY_PATH=/usr/local/bin/poetry
SYNCICS2GCAL_PATH=${SCRIPT_PARENT_DIR%/}/sync_ics2gcal
help () {
echo -e "$VERSION\n"
echo -e "This tool is designed to synchronize an iCal-format (.ics) calendar to a a Google Calendar with it, via the Google Calendar API."
echo -e "For that, it downloads the ics file, parse it with a cleaner script to avoid incompatibily with Google (with a python Script),"
echo -e "and another Python tool parse the cleaned file, and updates a Google Calendar with it, via the Google Calendar API (only one way)."
echo -e "This tool includes some third-party scripts and tools:"
echo -e "\t- ical-to-gcal.pl: original script by David Precious ([email protected]) - https://github.com/bigpresh/ical-to-google-calendar,"
echo -e "\t- sync-ics2gcal: sync-ics2gcal to be installed first in a venv, have a look here https://github.com/b4tman/sync_ics2gcal."
echo -e "\t sync-ics2gcal have to be completely installed, with service-account.json."
echo -e "\nDisclamer:"
echo -e "This tool is provide without any support and guarantee."
echo -e "\nSynopsis:"
echo -e "./$SCRIPT_NAME [-h] | -c <URL of iCal-format (.ics) file> -n <Calendar temp name>"
echo -e " -C <GoogleCalendar ID> -p <temp path to save ics files>"
echo -e " [-u <user to access to ics file>] [-P <password to access to ics file>]"
echo -e " [-e <email report option>] [-E <email address>] [-j <log file>]"
echo -e " [-g <sync-ics2gcal path>]"
echo -e "\n\t-h: prints this help then exit"
echo -e "\nMandatory options:"
echo -e "\t-c <URL of iCal-format (.ics) file>: the URL of iCal-format (.ics) source calendar (e.g.: 'http://my.server.com/path/to/icsfile.ics')"
echo -e "\t-n <calendar temp name>: the temp file name of this calendar"
echo -e "\t-C <GoogleCalendar ID>: ID of the GoogleCalendar you want to sync to (how to find it : http://goo.gl/oobl2v). This calendar must be created first."
echo -e "\t-p <temp path to save ics files>: the path to save ics files 'in transit'."
echo -e "\nOptional options:"
echo -e "\t-u <user to access to ics file>: the user to use to connect to the URL of iCal-format (.ics) file (if authentification is needed)."
echo -e "\t-P <password to access to ics file>: the password of user to use to connect to the URL of iCal-format (.ics) file (if authentification is needed),"
echo -e "\t must be filled if '- u' parameter is used. Asked if not filled."
echo -e "\t-g <sync-ics2gcal path>: path of sync-ics2gcal installation directory (default: '${SYNCICS2GCAL_PATH}')"
echo -e "\t-e <email report option>: settings for sending a report by email, must be 'onerror', 'forcemail' or 'nomail' (default: '${EMAIL_REPORT}')"
echo -e "\t-E <email address>: email address to send the report (must be filled if '-e forcemail' or '-e onerror' options is used)"
echo -e "\t-j <log file>: enables logging instead of standard output. Specify an argument for the full path to the log file"
echo -e "\t (e.g.: '${LOG}') or use 'default' (${LOG})"
exit 0
}
error () {
echo -e "\n*** Error ***"
echo -e ${1}
echo -e "\n"${VERSION}
alldone 1
}
alldone () {
# Redirect standard outpout
exec 1>&6 6>&-
# Logging if needed
[ ${LOG_ACTIVE} -eq 1 ] && cat ${LOG_TEMP} >> ${LOG}
# Print current log to standard outpout
[ ${LOG_ACTIVE} -ne 1 ] && cat ${LOG_TEMP}
[ ${EMAIL_LEVEL} -ne 0 ] && [ ${1} -ne 0 ] && cat ${LOG_TEMP} | mail -s "[ERROR: ${SCRIPT_NAME}] Processing URL: ${URL_CALENDAR}" "${EMAIL_ADDRESS}" && rm ${LOG_TEMP} && exit ${1}
[ ${EMAIL_LEVEL} -eq 2 ] && [ ${1} -eq 0 ] && cat ${LOG_TEMP} | mail -s "[OK: ${SCRIPT_NAME}] Processing URL: ${URL_CALENDAR}" "${EMAIL_ADDRESS}" && rm ${LOG_TEMP} && exit ${1}
[ ${EMAIL_LEVEL} -eq 2 ] && [ $URL_OK -ne 0 ] && cat ${LOG_TEMP} | mail -s "[POSSIBLE ERROR: ${SCRIPT_NAME}] Processing URL: ${URL_CALENDAR}" "${EMAIL_ADDRESS}" && rm ${LOG_TEMP} && exit ${1}
rm ${LOG_TEMP}
exit ${1}
}
optsCount=0
while getopts "hc:n:C:p:u:P:e:E:j:o:" OPTION
do
case "$OPTION" in
h) help="yes"
;;
c) URL_CALENDAR=${OPTARG}
let optsCount=$optsCount+1
;;
n) LOCAL_FILE=${OPTARG}
let optsCount=$optsCount+1
;;
C) CALENDAR_GCAL=${OPTARG}
let optsCount=$optsCount+1
;;
p) PATH_ICS=${OPTARG}
let optsCount=$optsCount+1
;;
u) CURL_USER=${OPTARG}
WITH_USER=1
;;
P) CURL_PASS=${OPTARG}
;;
g) SYNCICS2GCAL_PATH=${OPTARG}
;;
e) EMAIL_REPORT=${OPTARG}
;;
E) EMAIL_ADDRESS=${OPTARG}
;;
j) [ ${OPTARG} != "default" ] && LOG=${OPTARG}
LOG_ACTIVE=1
;;
esac
done
if [[ ${optsCount} != "4" ]]
then
help
alldone 1
fi
if [[ ${help} = "yes" ]]
then
help
fi
if [[ ${WITH_USER} = "1" ]] && [[ ${CURL_PASS} = "" ]]
then
echo "Enter password associated to user '${CURL_USER}' needed to access to '${URL_CALENDAR}'?"
read -s CURL_PASS
fi
# Redirect standard outpout to temp file
exec 6>&1
exec >> ${LOG_TEMP}
# Start temp log file
echo -e "\n****************************** `date` ******************************\n"
echo -e "\nProcessing URL: ${URL_CALENDAR}\n"
# Test of sending email parameter and check the consistency of the parameter email address
if [[ ${EMAIL_REPORT} = "forcemail" ]]
then
EMAIL_LEVEL=2
if [[ -z $EMAIL_ADDRESS ]]
then
echo -e "You use option '-e ${EMAIL_REPORT}' but you have not entered any email info.\n\t-> We continue the process without sending email."
EMAIL_LEVEL=0
else
echo "${EMAIL_ADDRESS}" | grep '^[a-zA-Z0-9._-]*@[a-zA-Z0-9._-]*\.[a-zA-Z0-9._-]*$' > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo -e "This address '${EMAIL_ADDRESS}' does not seem valid.\n\t-> We continue the process without sending email."
EMAIL_LEVEL=0
fi
fi
elif [[ ${EMAIL_REPORT} = "onerror" ]]
then
EMAIL_LEVEL=1
if [[ -z $EMAIL_ADDRESS ]]
then
echo -e "You use option '-e ${EMAIL_REPORT}' but you have not entered any email info.\n\t-> We continue the process without sending email."
EMAIL_LEVEL=0
else
echo "${EMAIL_ADDRESS}" | grep '^[a-zA-Z0-9._-]*@[a-zA-Z0-9._-]*\.[a-zA-Z0-9._-]*$' > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo -e "This address '${EMAIL_ADDRESS}' does not seem valid.\n\t-> We continue the process without sending email."
EMAIL_LEVEL=0
fi
fi
elif [[ ${EMAIL_REPORT} != "nomail" ]]
then
echo -e "\nOption '-e ${EMAIL_REPORT}' is not valid (must be: 'onerror', 'forcemail' or 'nomail').\n\t-> We continue the process without sending email."
EMAIL_LEVEL=0
elif [[ ${EMAIL_REPORT} = "nomail" ]]
then
EMAIL_LEVEL=0
fi
## Testons si l'URL est correcte
if [[ ${WITH_USER} = "0" ]]
then
curl -L -k -s --head ${URL_CALENDAR} | head -n 1 | grep "HTTP/1.1 200 OK" > /dev/null
URL_OK=$?
elif [[ ${WITH_USER} = "1" ]]
then
curl -L -k -s -u ${CURL_USER}:${CURL_PASS} --head ${URL_CALENDAR} | head -n 1 | grep "HTTP/1.1 200 OK" > /dev/null
URL_OK=$?
fi
[ $URL_OK -eq 0 ] && echo "URL of ics calendar seems to be OK: ${URL_CALENDAR}."
[ $URL_OK -ne 0 ] && echo "Possible problem to access to ics calendar: ${URL_CALENDAR}."
# Test if temp path exists
[ ! -d ${PATH_ICS} ] && error "The temp path '${PATH_ICS}' is not correct."
OWNER_PATH_ICS=$(stat -c %U ${PATH_ICS})
GWNER_PATH_ICS=$(stat -c %G ${PATH_ICS})
# Removing old files
[ -e ${PATH_ICS}/${LOCAL_FILE}.ics ] && rm ${PATH_ICS}/${LOCAL_FILE}.ics
[ -e ${PATH_ICS}/${LOCAL_FILE}.gcal.ics ] && rm ${PATH_ICS}/${LOCAL_FILE}.gcal.ics
# Downloading ics file
if [[ ${WITH_USER} = "0" ]]
then
curl -L -k --silent ${URL_CALENDAR} -o ${PATH_ICS}/${LOCAL_FILE}.ics && chown -R ${OWNER_PATH_ICS}:${GWNER_PATH_ICS} ${PATH_ICS}
elif [[ ${WITH_USER} = "1" ]]
then
curl -L -k --silent -u ${CURL_USER}:${CURL_PASS} ${URL_CALENDAR} -o ${PATH_ICS}/${LOCAL_FILE}.ics && chown -R ${OWNER_PATH_ICS}:${GWNER_PATH_ICS} ${PATH_ICS}
fi
## Test if file seems to be correct (contains BEGIN:VCALENDAR)
if [ -e ${PATH_ICS%/}/${LOCAL_FILE}.ics ]
then
cat ${PATH_ICS%/}/${LOCAL_FILE}.ics | head -n 10 | grep "BEGIN:VCALENDAR" > /dev/null
[ $? -ne 0 ] && error "File '${PATH_ICS}/${LOCAL_FILE}.ics' doesn't seem to be a correct calendar file."
else
error "File ${PATH_ICS}/${LOCAL_FILE}.ics doesn't exist"
fi
# Processing by ${PYTHON_ICS_CLEANER}
echo -e "\nProcessing command: '${PYTHON_ICS_CLEANER} ${PATH_ICS}/${LOCAL_FILE}.ics'."
echo -e "\n***********"
${PYTHON_ICS_CLEANER} ${PATH_ICS%/}/${LOCAL_FILE}.ics 2>&1
[ $? -ne 0 ] && error "File processing on '${PATH_ICS}/${LOCAL_FILE}.ics' was not completed successfully by '${PYTHON_ICS_CLEANER}'."
echo -e "***********\n"
echo -e "File processing on '${PATH_ICS}/${LOCAL_FILE}.ics' was completed successfully by '${PYTHON_ICS_CLEANER}'.\n"
## Pour le moment on lance via Poetry
# Test if sync-ics2gcal is installed
#SYNCICS2GCAL_BIN=${SYNCICS2GCAL_PATH%/}/venv/bin/sync-ics2gcal
#echo "SCRIPT_PARENT_DIR : ${SCRIPT_PARENT_DIR}"
#echo "SYNCICS2GCAL_BIN : ${SYNCICS2GCAL_BIN}"
#[[ ! -x ${SYNCICS2GCAL_BIN} ]] && error "sync-ics2gcal seems not to be installed. Install this tool first.\nHave a look to https://github.com/b4tman/sync_ics2gcal."
SYNCICS2GCAL_BIN="${POETRY_PATH} run sync-ics2gcal"
# Processing by ${SYNCICS2GCAL_BIN}
cd ${SYNCICS2GCAL_PATH%}
# Deleting olf sync-ics2gcal conf
SYNCICS2GCAL_CONF=${SYNCICS2GCAL_PATH%/}/config.yml
[ -e ${SYNCICS2GCAL_CONF} ] && rm ${SYNCICS2GCAL_CONF}
# Moving ICS file
#echo "CP : ${PATH_ICS%/}/${LOCAL_FILE}.gcal.ics ${SYNCICS2GCAL_PATH%/}/${LOCAL_FILE}.gcal.ics"
cp ${PATH_ICS%/}/${LOCAL_FILE}.gcal.ics ${SYNCICS2GCAL_PATH%/}/${LOCAL_FILE}.gcal.ics
ICS_CLEAN_FILE=${SYNCICS2GCAL_PATH%/}/${LOCAL_FILE}.gcal.ics
# Creating new sync-ics2gcal conf
echo "start_from: now" > ${SYNCICS2GCAL_CONF}
echo "service_account: service-account.json" >> ${SYNCICS2GCAL_CONF}
echo "calendar:" >> ${SYNCICS2GCAL_CONF}
echo " google_id: ${CALENDAR_GCAL}" >> ${SYNCICS2GCAL_CONF}
echo " source: ${LOCAL_FILE}.gcal.ics" >> ${SYNCICS2GCAL_CONF}
echo "Processing commmand: sync-ics2gcal"
echo -e "\n***********"
${SYNCICS2GCAL_BIN}
rm ${PATH_ICS}/${LOCAL_FILE}*
[ -e ${ICS_CLEAN_FILE} ] && rm ${ICS_CLEAN_FILE}
[ -e ${SYNCICS2GCAL_CONF} ] && rm ${SYNCICS2GCAL_CONF}
alldone 0