Comprehensive update of ical2txt.py with better argument checking and now handling recurring events, as long as recurring_ical_events can figure them out.

This commit is contained in:
Martin Moeller 2021-03-28 21:36:17 +02:00
parent aeae095e3e
commit d6030fb5ed
2 changed files with 43 additions and 24 deletions

View File

@ -42,6 +42,10 @@ Call the script and pass in the location of the ics file.
Ex: `python ical2txt.py event.ics` / `python3 ical2txt.py event.ics`
Note: You can limit output to a certain time period. Useful for week logs and the like:
`./ical2txt.py myexport.ics 20210101 20211231`
`./ical2txt.py myexport.ics 2021-01-01T00:00:00 2021-01-31T23:59:59`
## Contributing
1. Fork it!

View File

@ -3,13 +3,21 @@
import sys
import os.path
from icalendar import Calendar
import csv
import recurring_ical_events
from bs4 import BeautifulSoup
import warnings
from dateutil.parser import parse
import datetime
warnings.filterwarnings("ignore", category=UserWarning, module='bs4') # We don't want warnings about URL's. We just what the URL printed, if there.
if len(sys.argv) <= 1:
print("Please call this script with an ics-file as parameter.\n")
print("Even better, call it with start and end dates:\n")
print(sys.argv[0] + " myexport.ics 20210101 20210201")
print(sys.argv[0] + " myexport.ics 2021-01-01T00:00:00 2021-01-31T23:59:59\n")
exit(1)
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:]
@ -49,31 +57,33 @@ def removehtml(html):
return text
def open_cal():
if os.path.isfile(filename):
if file_extension == 'ics':
print("Extracting events from file:", filename, "\n")
f = open(sys.argv[1], 'rb')
gcal = Calendar.from_ical(f.read())
revents = recurring_ical_events.of(gcal).between(istart,istop)
for component in gcal.walk():
# for component in gcal.walk():
for component in revents:
event = CalendarEvent("event")
if component.get('TRANSP') == 'TRANSPARENT': continue #skip event that have not been accepted
if component.get('SUMMARY') == None: continue #skip blank items
event.summary = component.get('SUMMARY')
event.uid = component.get('UID')
if component.get('DESCRIPTION') == None: continue #skip blank items
event.description = component.get('DESCRIPTION')
event.location = component.get('LOCATION')
if hasattr(component.get('dtstart'), 'dt'):
event.start = component.get('dtstart').dt
if hasattr(component.get('dtend'), 'dt'):
event.end = component.get('dtend').dt
v=(dir(component).count('get'))
if (v != 0):
if component.get('TRANSP') == 'TRANSPARENT': continue #skip all day events and the like
if component.get('SUMMARY') == None: continue #skip blank items
event.summary = component.get('SUMMARY')
event.uid = component.get('UID')
if component.get('DESCRIPTION') == None: continue #skip blank items
event.description = component.get('DESCRIPTION')
event.location = component.get('LOCATION')
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)
event.url = component.get('URL')
events.append(event)
f.close()
else:
print("You entered ", filename, ". ")
@ -91,12 +101,6 @@ def txt_write(icsfile):
spent=0
evcount=0
evskip=0
istart=0
istop=4102441200.0 # The year 2100. Hopefully this will not be in use by then ...
if sys.argv[2] != '':
istart=parse(sys.argv[2]).timestamp()
if sys.argv[3] != '':
istop=parse(sys.argv[3]).timestamp()
print("Processing events :", end=" ")
try:
with open(txtfile, 'w') as myfile:
@ -108,7 +112,7 @@ def txt_write(icsfile):
tm=divmod(spent, 3600)[1]/60
myfile.write("\nTime Total: " + '{:02.0f}'.format(th) + ":" + '{:02.0f}'.format(tm) + "\n")
spent=0
if event.start.timestamp() > istart and event.start.timestamp() < istop:
if event.start.timestamp() > istart.timestamp() and event.start.timestamp() < istop.timestamp():
if prevdate != event.start.strftime("%Y-%m-%d"): # Make a header for each day
prevdate = event.start.strftime("%Y-%m-%d")
myfile.write("\nWorklog, " + prevdate + "\n===================\n")
@ -151,6 +155,17 @@ def debug_event(class_name):
print(class_name.end)
print(class_name.url, "\n")
istart=datetime.datetime.fromtimestamp(0) # Start of UNIX epoch (1970-01-01T00:00:00)
istop=datetime.datetime.fromtimestamp(4102441200) # The year 2100. Hopefully this will not be in use by then ...
if len(sys.argv) > 3:
if sys.argv[2] != '':
# istart=parse(sys.argv[2]).timestamp()
istart=parse(sys.argv[2])
if sys.argv[3] != '':
# istop=parse(sys.argv[3]).timestamp()
istop=parse(sys.argv[3])
open_cal()
sortedevents=sorted(events, key=lambda obj: obj.start)
txt_write(filename)