Merge pull request #2 from bozoslivehere/master
Some fixes, improvements
This commit is contained in:
commit
626131176c
54
ical2csv.py
54
ical2csv.py
@ -21,61 +21,67 @@ class CalendarEvent:
|
|||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
event = CalendarEvent("event")
|
events = []
|
||||||
|
|
||||||
|
|
||||||
def open_cal():
|
def open_cal():
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
if file_extension == 'ics':
|
if file_extension == 'ics':
|
||||||
print "Extracting events from file:", filename, "\n"
|
print("Extracting events from file:", filename, "\n")
|
||||||
f = open(sys.argv[1], 'rb')
|
f = open(sys.argv[1], 'rb')
|
||||||
gcal = Calendar.from_ical(f.read())
|
gcal = Calendar.from_ical(f.read())
|
||||||
|
|
||||||
# TODO: Let this parse multiple events in one ics file
|
|
||||||
for component in gcal.walk():
|
for component in gcal.walk():
|
||||||
|
event = CalendarEvent("event")
|
||||||
|
if component.get('SUMMARY') == None: continue #skip blank items
|
||||||
event.summary = component.get('SUMMARY')
|
event.summary = component.get('SUMMARY')
|
||||||
event.uid = component.get('UID')
|
event.uid = component.get('UID')
|
||||||
event.description = component.get('DESCRIPTION')
|
event.description = component.get('DESCRIPTION')
|
||||||
event.location = component.get('LOCATION')
|
event.location = component.get('LOCATION')
|
||||||
event.start = component.get('dtstart').dt
|
if hasattr(component.get('dtstart'), 'dt'):
|
||||||
event.end = component.get('dtend').dt
|
event.start = component.get('dtstart').dt
|
||||||
|
if hasattr(component.get('dtend'), 'dt'):
|
||||||
|
event.end = component.get('dtend').dt
|
||||||
|
|
||||||
|
|
||||||
event.url = component.get('URL')
|
event.url = component.get('URL')
|
||||||
|
events.append(event)
|
||||||
f.close()
|
f.close()
|
||||||
else:
|
else:
|
||||||
print "You entered ", filename, ". "
|
print("You entered ", filename, ". ")
|
||||||
print file_extension.upper(), " is not a valid file format. Looking for an ICS file."
|
print(file_extension.upper(), " is not a valid file format. Looking for an ICS file.")
|
||||||
exit(0)
|
exit(0)
|
||||||
else:
|
else:
|
||||||
print "I can't find the file ", filename, "."
|
print("I can't find the file ", filename, ".")
|
||||||
print "Please enter an ics file located in the same folder as this script."
|
print("Please enter an ics file located in the same folder as this script.")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
# TODO: make a create and an append method for use with multiple events in one file
|
|
||||||
def csv_write(icsfile):
|
def csv_write(icsfile):
|
||||||
csvfile = icsfile[:-3] + "csv"
|
csvfile = icsfile[:-3] + "csv"
|
||||||
try:
|
try:
|
||||||
with open(csvfile, 'wb') as myfile:
|
with open(csvfile, 'w') as myfile:
|
||||||
values = (event.summary, event.uid, event.description, event.location, event.start, event.end, event.url)
|
|
||||||
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
|
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
|
||||||
wr.writerow(headers)
|
wr.writerow(headers)
|
||||||
wr.writerow(values)
|
for event in events:
|
||||||
print "Wrote to ", csvfile, "\n"
|
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:
|
except IOError:
|
||||||
print "Could not open file! Please close Excel!"
|
print("Could not open file! Please close Excel!")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
def debug_event(class_name):
|
def debug_event(class_name):
|
||||||
print "Contents of ", class_name.name, ":"
|
print("Contents of ", class_name.name, ":")
|
||||||
print class_name.summary
|
print(class_name.summary)
|
||||||
print class_name.uid
|
print(class_name.uid)
|
||||||
print class_name.description
|
print(class_name.description)
|
||||||
print class_name.location
|
print(class_name.location)
|
||||||
print class_name.start
|
print(class_name.start)
|
||||||
print class_name.end
|
print(class_name.end)
|
||||||
print class_name.url, "\n"
|
print(class_name.url, "\n")
|
||||||
|
|
||||||
open_cal()
|
open_cal()
|
||||||
csv_write(filename)
|
csv_write(filename)
|
||||||
debug_event(event)
|
#debug_event(event)
|
||||||
|
Loading…
Reference in New Issue
Block a user