From 4a1717452add492f7f0c84aeb24fadd267446fa1 Mon Sep 17 00:00:00 2001 From: Gregory Newman Date: Thu, 24 Oct 2019 09:16:31 -0500 Subject: [PATCH 1/2] Python 3 compatibility --- ical2csv.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ical2csv.py b/ical2csv.py index 314b4ab..f2829b6 100644 --- a/ical2csv.py +++ b/ical2csv.py @@ -27,7 +27,7 @@ event = CalendarEvent("event") def open_cal(): if os.path.isfile(filename): if file_extension == 'ics': - print "Extracting events from file:", filename, "\n" + print("Extracting events from file:", filename, "\n") f = open(sys.argv[1], 'rb') gcal = Calendar.from_ical(f.read()) @@ -42,12 +42,12 @@ def open_cal(): event.url = component.get('URL') f.close() else: - print "You entered ", filename, ". " - print file_extension.upper(), " is not a valid file format. Looking for an ICS file." + print("You entered ", filename, ". ") + print(file_extension.upper(), " is not a valid file format. Looking for an ICS file.") exit(0) else: - print "I can't find the file ", filename, "." - print "Please enter an ics file located in the same folder as this script." + print("I can't find the file ", filename, ".") + print("Please enter an ics file located in the same folder as this script.") exit(0) @@ -60,21 +60,21 @@ def csv_write(icsfile): wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(headers) wr.writerow(values) - print "Wrote to ", csvfile, "\n" + print("Wrote to ", csvfile, "\n") except IOError: - print "Could not open file! Please close Excel!" + print("Could not open file! Please close Excel!") exit(0) def debug_event(class_name): - 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" + 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") open_cal() csv_write(filename) From 58c92578cec17bdfb6174279c2302387e6a4750f Mon Sep 17 00:00:00 2001 From: Gregory Newman Date: Thu, 24 Oct 2019 10:49:10 -0500 Subject: [PATCH 2/2] Convert all events, skip blanks, fix .dt crash --- ical2csv.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ical2csv.py b/ical2csv.py index f2829b6..b681787 100644 --- a/ical2csv.py +++ b/ical2csv.py @@ -21,7 +21,7 @@ class CalendarEvent: def __init__(self, name): self.name = name -event = CalendarEvent("event") +events = [] def open_cal(): @@ -31,15 +31,21 @@ def open_cal(): f = open(sys.argv[1], 'rb') gcal = Calendar.from_ical(f.read()) - # TODO: Let this parse multiple events in one ics file for component in gcal.walk(): + event = CalendarEvent("event") + if component.get('SUMMARY') == None: continue #skip blank items event.summary = component.get('SUMMARY') event.uid = component.get('UID') event.description = component.get('DESCRIPTION') event.location = component.get('LOCATION') - event.start = component.get('dtstart').dt - event.end = component.get('dtend').dt + if hasattr(component.get('dtstart'), 'dt'): + event.start = component.get('dtstart').dt + if hasattr(component.get('dtend'), 'dt'): + event.end = component.get('dtend').dt + + event.url = component.get('URL') + events.append(event) f.close() else: print("You entered ", filename, ". ") @@ -51,15 +57,15 @@ def open_cal(): exit(0) -# TODO: make a create and an append method for use with multiple events in one file def csv_write(icsfile): csvfile = icsfile[:-3] + "csv" try: - with open(csvfile, 'wb') as myfile: - values = (event.summary, event.uid, event.description, event.location, event.start, event.end, event.url) + with open(csvfile, 'w') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(headers) - wr.writerow(values) + for event in events: + values = (event.summary, event.uid, event.description, event.location, event.start, event.end, event.url) + wr.writerow(values) print("Wrote to ", csvfile, "\n") except IOError: print("Could not open file! Please close Excel!") @@ -78,4 +84,4 @@ def debug_event(class_name): open_cal() csv_write(filename) -debug_event(event) +#debug_event(event)