„ical2csv.py“ ändern
Bugfixing AttributeError none for summary, description and location; remove some useless print() statements
This commit is contained in:
parent
c1fe0dcec7
commit
1db23f9ea7
42
ical2csv.py
42
ical2csv.py
@ -27,7 +27,6 @@ file_extension = str(sys.argv[1])[-3:]
|
|||||||
headers = ('Summary', 'UID', 'Description', 'Location', 'Start Time', 'End Time', 'URL')
|
headers = ('Summary', 'UID', 'Description', 'Location', 'Start Time', 'End Time', 'URL')
|
||||||
|
|
||||||
class CalendarEvent:
|
class CalendarEvent:
|
||||||
"""Calendar event class"""
|
|
||||||
summary = ''
|
summary = ''
|
||||||
uid = ''
|
uid = ''
|
||||||
description = ''
|
description = ''
|
||||||
@ -63,7 +62,7 @@ def removehtml(html):
|
|||||||
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)
|
||||||
f = open(sys.argv[1], 'rb')
|
f = open(sys.argv[1], 'rb')
|
||||||
gcal = Calendar.from_ical(f.read())
|
gcal = Calendar.from_ical(f.read())
|
||||||
revents = recurring_ical_events.of(gcal).between(istart,istop)
|
revents = recurring_ical_events.of(gcal).between(istart,istop)
|
||||||
@ -73,11 +72,14 @@ def open_cal():
|
|||||||
event = CalendarEvent("event")
|
event = CalendarEvent("event")
|
||||||
v=(dir(component).count('get')) # Only proces data if object is a valid event
|
v=(dir(component).count('get')) # Only proces data if object is a valid event
|
||||||
if (v != 0):
|
if (v != 0):
|
||||||
if component.get('TRANSP') == 'TRANSPARENT': continue #skip all day events and the like
|
if component.get('TRANSP') == 'TRANSPARENT':
|
||||||
if component.get('SUMMARY') == None: continue #skip blank items
|
continue #skip all day events and the like
|
||||||
|
if component.get('SUMMARY') is None or component.get('SUMMARY') == "":
|
||||||
|
continue #skip blank items
|
||||||
event.summary = component.get('SUMMARY')
|
event.summary = component.get('SUMMARY')
|
||||||
event.uid = component.get('UID')
|
event.uid = component.get('UID')
|
||||||
if component.get('DESCRIPTION') == None: continue #skip blank items
|
if component.get('DESCRIPTION') is None or component.get('DESCRIPTION') == "":
|
||||||
|
continue #skip blank items
|
||||||
event.description = component.get('DESCRIPTION')
|
event.description = component.get('DESCRIPTION')
|
||||||
event.location = component.get('LOCATION')
|
event.location = component.get('LOCATION')
|
||||||
if hasattr(component.get('dtstart'), 'dt'):
|
if hasattr(component.get('dtstart'), 'dt'):
|
||||||
@ -109,33 +111,25 @@ def csv_write(icsfile):
|
|||||||
spent=0
|
spent=0
|
||||||
evcount=0
|
evcount=0
|
||||||
evskip=0
|
evskip=0
|
||||||
sys.stdout.write("Processing events : ")
|
|
||||||
try:
|
try:
|
||||||
with open(csvfile, 'w') as myfile:
|
with open(csvfile, 'w') as myfile:
|
||||||
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
|
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
|
||||||
wr.writerow(headers)
|
wr.writerow(headers)
|
||||||
for event in sortedevents:
|
for event in sortedevents:
|
||||||
values = (event.summary.encode('utf-8').decode(), event.uid, removehtml(event.description.encode('utf-8').decode()), event.location.encode('utf-8').decode(), event.start, event.end, event.url)
|
values = (event.summary.encode('utf-8').decode(),
|
||||||
|
event.uid,
|
||||||
|
removehtml(event.description.encode('utf-8').decode()),
|
||||||
|
event.location.encode('utf-8').decode() if event.location is not None else "",
|
||||||
|
event.start,
|
||||||
|
event.end,
|
||||||
|
event.url)
|
||||||
wr.writerow(values)
|
wr.writerow(values)
|
||||||
sys.stdout.write(".")
|
|
||||||
sys.stdout.flush()
|
|
||||||
evcount+=1
|
evcount+=1
|
||||||
print("\n\nWrote " + str(evcount) + " events to ", csvfile, "\n")
|
print("Wrote " + str(evcount) + " events to ", csvfile)
|
||||||
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):
|
|
||||||
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")
|
|
||||||
|
|
||||||
now=datetime.datetime.now()
|
now=datetime.datetime.now()
|
||||||
istart=datetime.datetime.fromtimestamp(0) # Start of UNIX epoch (1970-01-01T00:00:00)
|
istart=datetime.datetime.fromtimestamp(0) # Start of UNIX epoch (1970-01-01T00:00:00)
|
||||||
istop=now+datetime.timedelta(seconds=157680000) # Stop 5 years in the future, if no enddate is given, to make sure reucurring events don't go on forever ...
|
istop=now+datetime.timedelta(seconds=157680000) # Stop 5 years in the future, if no enddate is given, to make sure reucurring events don't go on forever ...
|
||||||
@ -146,9 +140,7 @@ if len(sys.argv) > 3:
|
|||||||
if sys.argv[3] != '':
|
if sys.argv[3] != '':
|
||||||
istop=parse(sys.argv[3])
|
istop=parse(sys.argv[3])
|
||||||
|
|
||||||
print("Opening ics file\n")
|
open_cal() # Open ics file and do initial parsing of events
|
||||||
open_cal() # Open ics file and do initial parsing of events
|
|
||||||
print("Sorting events\n")
|
|
||||||
sortedevents=sorted(events, key=lambda obj: obj.start) # Make sure events are in chronological order
|
sortedevents=sorted(events, key=lambda obj: obj.start) # Make sure events are in chronological order
|
||||||
csv_write(filename)
|
csv_write(filename)
|
||||||
#debug_event(event)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user