Last active
August 29, 2015 14:11
-
-
Save llucax/35a3dbd04f056c861486 to your computer and use it in GitHub Desktop.
OwnCloud iCal Calendar Importer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# Works only on Python2 because depends on caldav that only works on Python2 | |
# (but this script should be compatible with Python3) | |
# https://pypi.python.org/pypi/caldav | |
# pip install caldav | |
from __future__ import print_function | |
import sys | |
import caldav | |
import argparse | |
parser = argparse.ArgumentParser( | |
description='Send ICS calendar files to an owncloud CalDAV server') | |
parser.add_argument('server', | |
help="server URL where to send the calendar files") | |
parser.add_argument('files', nargs='+', metavar="file", | |
help="file to send to the server") | |
parser.add_argument('-v', '--verbose', action='store_true', help="be verbose") | |
parser.add_argument('-n', '--dry-run', action='store_true', | |
help="just show the plan, don't add any events to the calendar") | |
parser.add_argument('-q', '--quiet', action='store_true', help="be quiet") | |
parser.add_argument('-c', '--calendar', metavar="NAME", | |
help="put the calendar files to the %(metavar)s calendar " | |
"(default: first found)") | |
parser.add_argument('-u', '--username', | |
help="log in to server as %(metavar)s (default: no login)") | |
parser.add_argument('-p', '--password', | |
help="log in to server using %(metavar)s (default: no password)") | |
parser.add_argument('-X', '--proxy-url', metavar="URL", | |
help="use %(metavar)s as proxy (default: no proxy is used)") | |
parser.add_argument('-P', '--primary-path', metavar="PATH", | |
default='/remote.php/caldav/', | |
help="use %(metavar)s as the path to OwnCloud Primary CalDAV address " | |
" in the server (default: %(default)s)") | |
args = parser.parse_args() | |
def vprint(fmt, *fargs, **kwargs): | |
if args.verbose: | |
print(fmt.format(*fargs), **kwargs) | |
def qerror(fmt, *fargs, **kwargs): | |
if not args.quiet: | |
print(fmt.format(*fargs), file=sys.stderr, **kwargs) | |
url = args.server + args.primary_path | |
vprint("Retrieving calendars from {} (username={}, proxy={})", | |
url, args.username, args.proxy_url) | |
client = caldav.DAVClient(url, args.proxy_url, args.username, args.password) | |
principal = caldav.Principal(client) | |
calendars = principal.calendars() | |
if len(calendars) == 0: | |
qerror("No calendars found in {}", url) | |
sys.exit(3) | |
if args.calendar is None: | |
calendar = calendars[0] | |
else: | |
for calendar in calendars: | |
if calendar.url.endswith('/' + args.calendar): | |
break | |
else: | |
qerror("No '{}' calendar found", args.calendar) | |
sys.exit(4) | |
vprint("Using calendar {}", calendar) | |
error = False | |
for fname in args.files: | |
vprint("Reading calendar from '{}'", fname) | |
try: | |
vcal = open(fname).read() | |
except (IOError, OSError) as e: | |
qerror("Error opening/reading file '{}', skipping...", fname) | |
error = True | |
continue | |
vprint("\tAdding to calendar") | |
if not args.dry_run: | |
event = calendar.add_event(vcal) | |
vprint("\tEvent {} created", event) | |
if error: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment