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:
parent
aeae095e3e
commit
d6030fb5ed
@ -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`
|
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
|
## Contributing
|
||||||
|
|
||||||
1. Fork it!
|
1. Fork it!
|
||||||
|
39
ical2txt.py
39
ical2txt.py
@ -3,13 +3,21 @@
|
|||||||
import sys
|
import sys
|
||||||
import os.path
|
import os.path
|
||||||
from icalendar import Calendar
|
from icalendar import Calendar
|
||||||
import csv
|
import recurring_ical_events
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import warnings
|
import warnings
|
||||||
from dateutil.parser import parse
|
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.
|
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]
|
filename = sys.argv[1]
|
||||||
# TODO: use regex to get file extension (chars after last period), in case it's not exactly 3 chars.
|
# 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:]
|
file_extension = str(sys.argv[1])[-3:]
|
||||||
@ -49,17 +57,20 @@ def removehtml(html):
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
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())
|
||||||
|
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")
|
event = CalendarEvent("event")
|
||||||
if component.get('TRANSP') == 'TRANSPARENT': continue #skip event that have not been accepted
|
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
|
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')
|
||||||
@ -71,7 +82,6 @@ def open_cal():
|
|||||||
if hasattr(component.get('dtend'), 'dt'):
|
if hasattr(component.get('dtend'), 'dt'):
|
||||||
event.end = component.get('dtend').dt
|
event.end = component.get('dtend').dt
|
||||||
|
|
||||||
|
|
||||||
event.url = component.get('URL')
|
event.url = component.get('URL')
|
||||||
events.append(event)
|
events.append(event)
|
||||||
f.close()
|
f.close()
|
||||||
@ -91,12 +101,6 @@ def txt_write(icsfile):
|
|||||||
spent=0
|
spent=0
|
||||||
evcount=0
|
evcount=0
|
||||||
evskip=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=" ")
|
print("Processing events :", end=" ")
|
||||||
try:
|
try:
|
||||||
with open(txtfile, 'w') as myfile:
|
with open(txtfile, 'w') as myfile:
|
||||||
@ -108,7 +112,7 @@ def txt_write(icsfile):
|
|||||||
tm=divmod(spent, 3600)[1]/60
|
tm=divmod(spent, 3600)[1]/60
|
||||||
myfile.write("\nTime Total: " + '{:02.0f}'.format(th) + ":" + '{:02.0f}'.format(tm) + "\n")
|
myfile.write("\nTime Total: " + '{:02.0f}'.format(th) + ":" + '{:02.0f}'.format(tm) + "\n")
|
||||||
spent=0
|
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
|
if prevdate != event.start.strftime("%Y-%m-%d"): # Make a header for each day
|
||||||
prevdate = event.start.strftime("%Y-%m-%d")
|
prevdate = event.start.strftime("%Y-%m-%d")
|
||||||
myfile.write("\nWorklog, " + prevdate + "\n===================\n")
|
myfile.write("\nWorklog, " + prevdate + "\n===================\n")
|
||||||
@ -151,6 +155,17 @@ def debug_event(class_name):
|
|||||||
print(class_name.end)
|
print(class_name.end)
|
||||||
print(class_name.url, "\n")
|
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()
|
open_cal()
|
||||||
sortedevents=sorted(events, key=lambda obj: obj.start)
|
sortedevents=sorted(events, key=lambda obj: obj.start)
|
||||||
txt_write(filename)
|
txt_write(filename)
|
||||||
|
Loading…
Reference in New Issue
Block a user