2020-11-02 04:46:00 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2016-02-04 23:01:46 +01:00
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
from icalendar import Calendar
|
|
|
|
import csv
|
|
|
|
|
|
|
|
filename = sys.argv[1]
|
|
|
|
# TODO: use regex to get file extension (chars after last period), in case it's not exactly 3 chars.
|
|
|
|
file_extension = str(sys.argv[1])[-3:]
|
|
|
|
headers = ('Summary', 'UID', 'Description', 'Location', 'Start Time', 'End Time', 'URL')
|
|
|
|
|
|
|
|
class CalendarEvent:
|
|
|
|
"""Calendar event class"""
|
|
|
|
summary = ''
|
|
|
|
uid = ''
|
|
|
|
description = ''
|
|
|
|
location = ''
|
|
|
|
start = ''
|
|
|
|
end = ''
|
|
|
|
url = ''
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
2019-10-24 17:49:10 +02:00
|
|
|
events = []
|
2016-02-04 23:01:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
def open_cal():
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
if file_extension == 'ics':
|
2019-10-24 16:16:31 +02:00
|
|
|
print("Extracting events from file:", filename, "\n")
|
2016-02-04 23:01:46 +01:00
|
|
|
f = open(sys.argv[1], 'rb')
|
|
|
|
gcal = Calendar.from_ical(f.read())
|
|
|
|
|
|
|
|
for component in gcal.walk():
|
2019-10-24 17:49:10 +02:00
|
|
|
event = CalendarEvent("event")
|
2020-11-02 04:46:00 +01:00
|
|
|
if component.get('TRANSP') == 'TRANSPARENT': continue #skip event that have not been accepted
|
2019-10-24 17:49:10 +02:00
|
|
|
if component.get('SUMMARY') == None: continue #skip blank items
|
2016-02-04 23:01:46 +01:00
|
|
|
event.summary = component.get('SUMMARY')
|
|
|
|
event.uid = component.get('UID')
|
2020-03-26 04:34:56 +01:00
|
|
|
if component.get('DESCRIPTION') == None: continue #skip blank items
|
2016-02-04 23:01:46 +01:00
|
|
|
event.description = component.get('DESCRIPTION')
|
|
|
|
event.location = component.get('LOCATION')
|
2019-10-24 17:49:10 +02:00
|
|
|
if hasattr(component.get('dtstart'), 'dt'):
|
|
|
|
event.start = component.get('dtstart').dt
|
|
|
|
if hasattr(component.get('dtend'), 'dt'):
|
|
|
|
event.end = component.get('dtend').dt
|
|
|
|
|
|
|
|
|
2016-02-04 23:01:46 +01:00
|
|
|
event.url = component.get('URL')
|
2019-10-24 17:49:10 +02:00
|
|
|
events.append(event)
|
2016-02-04 23:01:46 +01:00
|
|
|
f.close()
|
|
|
|
else:
|
2019-10-24 16:16:31 +02:00
|
|
|
print("You entered ", filename, ". ")
|
|
|
|
print(file_extension.upper(), " is not a valid file format. Looking for an ICS file.")
|
2016-02-04 23:01:46 +01:00
|
|
|
exit(0)
|
|
|
|
else:
|
2019-10-24 16:16:31 +02:00
|
|
|
print("I can't find the file ", filename, ".")
|
|
|
|
print("Please enter an ics file located in the same folder as this script.")
|
2016-02-04 23:01:46 +01:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
def csv_write(icsfile):
|
|
|
|
csvfile = icsfile[:-3] + "csv"
|
|
|
|
try:
|
2019-10-24 17:49:10 +02:00
|
|
|
with open(csvfile, 'w') as myfile:
|
2016-02-04 23:01:46 +01:00
|
|
|
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
|
|
|
|
wr.writerow(headers)
|
2020-11-02 04:46:00 +01:00
|
|
|
for event in sortedevents:
|
|
|
|
values = (event.summary.encode('utf8').decode(), event.uid, event.description.encode('uft8').decode(), event.location, event.start, event.end, event.url)
|
2019-10-24 17:49:10 +02:00
|
|
|
wr.writerow(values)
|
2019-10-24 16:16:31 +02:00
|
|
|
print("Wrote to ", csvfile, "\n")
|
2016-02-04 23:01:46 +01:00
|
|
|
except IOError:
|
2019-10-24 16:16:31 +02:00
|
|
|
print("Could not open file! Please close Excel!")
|
2016-02-04 23:01:46 +01:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
def debug_event(class_name):
|
2019-10-24 16:16:31 +02:00
|
|
|
print("Contents of ", class_name.name, ":")
|
|
|
|
print(class_name.summary)
|
|
|
|
print(class_name.uid)
|
|
|
|
print(class_name.description)
|
|
|
|
print(class_name.location)
|
|
|
|
print(class_name.start)
|
|
|
|
print(class_name.end)
|
|
|
|
print(class_name.url, "\n")
|
2016-02-04 23:01:46 +01:00
|
|
|
|
|
|
|
open_cal()
|
2020-11-02 04:46:00 +01:00
|
|
|
sortedevents=sorted(events, key=lambda obj: obj.start) # Needed to sort events. They are not fully chronological in a Google Calendard export ...
|
2016-02-04 23:01:46 +01:00
|
|
|
csv_write(filename)
|
2019-10-24 17:49:10 +02:00
|
|
|
#debug_event(event)
|