Skip to content

Instantly share code, notes, and snippets.

@remyd1
Last active January 25, 2021 17:42
Show Gist options
  • Save remyd1/a353b07219884c5878cc27d04aab9d96 to your computer and use it in GitHub Desktop.
Save remyd1/a353b07219884c5878cc27d04aab9d96 to your computer and use it in GitHub Desktop.
caldav_api bash with curl
#!/bin/bash
#set -x
source .client_config
usage="$0 report [START DATE (YYMMDD)] [END DATE (YYMMDD)] \n
or \n $0 propfind \n
or \n $0 get [ics] \n"
if [[ $# -lt 1 ]]; then
echo -e $usage
exit 1
fi
if [[ $1 == "propfind" ]]; then
if [[ -n $user ]]; then
request="curl --silent -k --user $user -X PROPFIND "
else
request="curl --silent -X PROPFIND "
fi
request=$request" --header 'Depth: 1'
--header 'Prefer: return-minimal'
--header 'Content-Type: application/xml; charset=utf-8'
--url $caldav_url"
elif [[ $1 == "get" ]]; then
ics=$2
url=$caldav_url/$ics
if [[ -n $user ]]; then
request="curl --silent -k --user $user -X GET "
else
request="curl --silent -X GET "
fi
request=$request" --header 'Depth: 1'
--header 'Prefer: return-minimal'
--header 'Content-Type: application/xml; charset=utf-8'
--data-binary '<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">'
--url $url"
elif [[ $1 == "report" ]]; then
#START=$2"T000000Z"
if [[ $2 != "basic" ]]; then
req_type_basic=false
START=$2
else
req_type_basic=true
fi
if [[ -n $user ]]; then
request="curl --silent -k --user $user -X REPORT "
else
request="curl --silent -X REPORT "
fi
request=$request" --header 'Depth: 1'"
request=$request" --header 'Prefer: return-minimal'"
request=$request" --header 'Content-Type: application/xml; charset=utf-8'"
#request=$request" --header 'Content-Length: 0'"
request=$request" --data-binary '<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"
if [[ $req_type_basic == true ]]; then
prop=' <D:prop>
<D:getetag />
<C:calendar-data />
</D:prop>'
filter=' <C:filter>
<C:comp-filter name="VCALENDAR" />
</C:filter>'
else
prop='
<D:prop>
<D:getetag/>
<C:calendar-data>
<C:comp name="VCALENDAR">
<C:prop name="VERSION"/>
<C:comp name="VEVENT">
<C:prop name="SUMMARY"/>
<C:prop name="UID"/>
<C:prop name="DTSTART"/>
<C:prop name="DTEND"/>
<C:prop name="DURATION"/>
<C:prop name="RRULE"/>
<C:prop name="RDATE"/>
<C:prop name="EXRULE"/>
<C:prop name="EXDATE"/>
<C:prop name="RECURRENCE-ID"/>'
prop=$prop'
</C:comp>
<C:comp name="VTIMEZONE" />
</C:comp>
</C:calendar-data>
</D:prop>'
filter='
<C:filter>
<C:comp-filter name="VCALENDAR">
<C:comp-filter name="VEVENT">
<C:time-range start="'$START
if [[ -n $3 ]]; then
#END=$3"T000000Z"
END=$3
filter=$filter'" end="'$END'"/>'
else
filter=$filter'" />'
fi
filter=$filter"
</C:comp-filter>
</C:comp-filter>
</C:filter>"
fi
request=$request$prop$filter
request=$request"</C:calendar-query>'"
request=$request" --url $caldav_url"
fi
echo "##########################################"
echo "Request"
echo "##########################################"
echo $request
echo "##########################################"
echo "Results"
echo "##########################################"
eval $request
#!/bin/bash
### This shell script is used to create new events in caldav on our owncloud server after a machine booking on our grr server
usage="$0 machineNumber user starttime endtime"
if [ "$#" -ne 4 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ];then
echo $usage
exit 1
else
random1=`</dev/urandom tr -dc '1-9a-z' | head -c8; echo ""`
random2=`</dev/urandom tr -dc '1-9a-z' | head -c4; echo ""`
random3=`</dev/urandom tr -dc '1-9a-z' | head -c4; echo ""`
random4=`</dev/urandom tr -dc '1-9a-z' | head -c4; echo ""`
random5=`</dev/urandom tr -dc '1-9a-z' | head -c12; echo ""`
#UID=`</dev/urandom tr -dc '1-9a-z' | head -c10; echo ""`
# ics example 8cbf7d9e-6g68-43b9-zb3c-073a8e6b8f46.ics
UID=$random1-$random2-$random3-$random4-$random5
ics=$UID.ics
NOW=`date +%Y%m%dT%H%M%S`
template=template.ics
cp template.ics $ics
sed -i "s|reservation machine|reservation machine $1 par $2|g" $ics
sed -i "s|20160225T083000|$3|g" $ics
sed -i "s|20160225T090000|$4|g" $ics
sed -i "s|99g999gggg|$UID|g" $ics
sed -i "s|20160224T172807|$NOW|g" $ics
sed -i "s|20160224T172807|$NOW|g" $ics
user=username:secret
url=http://owncloud.domain.tld/owncloud/remote.php/caldav/calendars/foo
curl -k --user $user -X PUT -H "Content-Type: text/calendar; charset=utf-8" --data-binary @./$ics --url $url/$ics
fi
@jkoz
Copy link

jkoz commented Dec 3, 2020

Nice work, What is your template.ics looks like?

@remyd1
Copy link
Author

remyd1 commented Dec 9, 2020

Hi @jkoz

Sure, here is the template I am using:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
BEGIN:VTIMEZONE
TZID:Europe/Paris
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20160224T172807Z
LAST-MODIFIED:20160224T172807Z
DTSTAMP:20160224T172807Z
UID:99g999gggg
SUMMARY:reservation machine
DESCRIPTION:to_replace
DTSTART;TZID=Europe/Paris:20160225T083000
DTEND;TZID=Europe/Paris:20160225T090000
X-LIC-ERROR;X-LIC-ERRORTYPE=VALUE-PARSE-ERROR:No value for CATEGORIES prope
 rty. Removing entire property:
CLASS:PUBLIC
X-MOZ-GENERATION:1
END:VEVENT
END:VCALENDAR

I hope it would be helpful,

Best regards,
Rémy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment