From aeae095e3eb088c19d3bb6a218b3641ab8be870d Mon Sep 17 00:00:00 2001 From: Martin Moeller Date: Tue, 17 Nov 2020 09:25:48 +0100 Subject: [PATCH 1/6] Remove seconds from worklog (ical2txt.py) --- ical2txt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ical2txt.py b/ical2txt.py index 9cba6a3..71da0d2 100755 --- a/ical2txt.py +++ b/ical2txt.py @@ -119,7 +119,7 @@ def txt_write(icsfile): hours = divmod(ds, 3600)[0] minutes = divmod(ds,3600)[1]/60 description=removehtml(event.description.encode('utf-8').decode()) - values = event.start.strftime("%H:%M:%S") + " - " + event.end.strftime("%H:%M:%S") + " (" + '{:02.0f}'.format(hours) + ":" + '{:02.0f}'.format(minutes) + ") " + event.summary.encode('utf-8').decode() + values = event.start.strftime("%H:%M") + " - " + event.end.strftime("%H:%M") + " (" + '{:02.0f}'.format(hours) + ":" + '{:02.0f}'.format(minutes) + ") " + event.summary.encode('utf-8').decode() if event.location != '': values = values + " [" + event.location + "]" # Only include location if there is one # Remove Google Meet and Skype Meeting part of description From d6030fb5ed1353d86d3429b0f441956d3dd0d4df Mon Sep 17 00:00:00 2001 From: Martin Moeller Date: Sun, 28 Mar 2021 21:36:17 +0200 Subject: [PATCH 2/6] Comprehensive update of ical2txt.py with better argument checking and now handling recurring events, as long as recurring_ical_events can figure them out. --- README.md | 4 ++++ ical2txt.py | 63 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index f53f20b..2a35f17 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/ical2txt.py b/ical2txt.py index 71da0d2..f5d7625 100755 --- a/ical2txt.py +++ b/ical2txt.py @@ -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) From aea006661b6af82713571d64f729ad86d4d2f80c Mon Sep 17 00:00:00 2001 From: Martin Moeller Date: Sun, 28 Mar 2021 21:59:24 +0200 Subject: [PATCH 3/6] Comprehensive update of ical2txt.py with better argument checking and now handling recurring events, as long as recurring_ical_events can figure them out. Implemented safe stop 5 years in the future as default. --- ical2txt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ical2txt.py b/ical2txt.py index f5d7625..315e62d 100755 --- a/ical2txt.py +++ b/ical2txt.py @@ -68,7 +68,7 @@ def open_cal(): # for component in gcal.walk(): for component in revents: event = CalendarEvent("event") - v=(dir(component).count('get')) + v=(dir(component).count('get')) # Only proces data if object is a valid event 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 @@ -155,15 +155,15 @@ def debug_event(class_name): print(class_name.end) print(class_name.url, "\n") +now=datetime.datetime.now() 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 ... +#istop=datetime.datetime.fromtimestamp(4102441200) # The year 2100. Hopefully this will not be in use by then ... +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 ... 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() From da5ba848aacbac8d38e46a979b4cb5ff09d7975e Mon Sep 17 00:00:00 2001 From: Martin Moeller Date: Sun, 28 Mar 2021 22:08:32 +0200 Subject: [PATCH 4/6] Updated documentation. Included recurring-ical-events and default end time --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 2a35f17..e950760 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Download the script or clone the project and get it from there. * ***icalendar*** (pip3 install icalendar) * [**Homepage**](http://icalendar.readthedocs.org/) * [**Code**](http://github.com/collective/icalendar) +* ***recurring-ical-events*** (pip3 install recurring-ical-events) ## Usage of ical2txt @@ -43,9 +44,13 @@ 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` +*NEW AS OF 2021-03-28:* Recurring events are now actually processed in ical2txt.py. If no end date is given 5 years from now is chosen. + ## Contributing 1. Fork it! From 98e65a752df67a2c000822224cdee0c1bfb0cfb4 Mon Sep 17 00:00:00 2001 From: Martin Moeller Date: Sun, 28 Mar 2021 22:10:49 +0200 Subject: [PATCH 5/6] Bold in stead of italics for NEW section --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e950760..123fb41 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Note: You can limit output to a certain time period. Useful for week logs and th `./ical2txt.py myexport.ics 2021-01-01T00:00:00 2021-01-31T23:59:59` -*NEW AS OF 2021-03-28:* Recurring events are now actually processed in ical2txt.py. If no end date is given 5 years from now is chosen. +**NEW AS OF 2021-03-28:** Recurring events are now actually processed in ical2txt.py. If no end date is given 5 years from now is chosen. ## Contributing From eb6fdf7a19726c071d929bceb98bf5cd3c296087 Mon Sep 17 00:00:00 2001 From: Martin Moeller Date: Sun, 28 Mar 2021 23:05:02 +0200 Subject: [PATCH 6/6] Actually print progress dots live in stead of at the end. --- ical2txt.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ical2txt.py b/ical2txt.py index 315e62d..68b2e0e 100755 --- a/ical2txt.py +++ b/ical2txt.py @@ -101,7 +101,7 @@ def txt_write(icsfile): spent=0 evcount=0 evskip=0 - print("Processing events :", end=" ") + sys.stdout.write("Processing events : ") try: with open(txtfile, 'w') as myfile: for event in sortedevents: @@ -133,12 +133,18 @@ def txt_write(icsfile): if description != '': values = values + "\n" + description + "\n" myfile.write(values+"\n") - print("", end=".") + sys.stdout.write(".") + sys.stdout.flush() evcount+=1 else: - print("", end="S") + sys.stdout.write("S") + sys.stdout.flush() evskip+=1 + th=divmod(spent, 3600)[0] + tm=divmod(spent, 3600)[1]/60 + myfile.write("\nTime Total: " + '{:02.0f}'.format(th) + ":" + '{:02.0f}'.format(tm) + "\n") + print("\n\nWrote " + str(evcount) + " events to ", txtfile, " and skipped ", str(evskip), " events\n") except IOError: print("Could not open file!") @@ -157,7 +163,6 @@ def debug_event(class_name): now=datetime.datetime.now() 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 ... 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 ... if len(sys.argv) > 3: @@ -166,7 +171,7 @@ if len(sys.argv) > 3: if sys.argv[3] != '': istop=parse(sys.argv[3]) -open_cal() -sortedevents=sorted(events, key=lambda obj: obj.start) -txt_write(filename) +open_cal() # Open ics file and do initial parsing of events +sortedevents=sorted(events, key=lambda obj: obj.start) # Make sure events are in chronological order +txt_write(filename) # Write the matching events to the textfile. With recurring_ical_events, scoping is already done. #debug_event(event)