forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicalview.py
More file actions
executable file
·93 lines (65 loc) · 2.96 KB
/
icalview.py
File metadata and controls
executable file
·93 lines (65 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# https://gist.github.com/ParagDoke/2388d7311f11cab4fa5f5538fa694d0e
# The only examples are the tests:
# /usr/lib/python3/dist-packages/icalendar/tests/
import sys
from icalendar import Calendar
import datetime
import argparse
def read_ics_file(infile):
"""Parse an ics file into a dictionary.
"""
cal = Calendar.from_ical(open(infile, 'rb').read())
# Get the local timezone
localtz = datetime.datetime.now().astimezone().tzinfo
event = {}
for component in cal.walk():
if component.name != 'VEVENT':
continue
if 'SUMMARY' in event:
print("Yikes, more than one event!")
event['SUMMARY'] = component.get('summary')
event['DESCRIPTION'] = component.get('description')
event['LOCATION'] = component.get('location')
event['DTSTART'] = component.get('DTSTART').dt
event['DTEND'] = component.get('DTEND').dt
# I don't know what the dtstamp or exdate are. No docs.
# dtstamp = component.get('DTSTAMP')
# exdate = component.get('exdate')
# Rewrite into current timezone
event['LOCALSTART'] = event['DTSTART'].astimezone(localtz)
event['LOCALEND'] = event['DTEND'].astimezone(localtz)
# dtstamp.dt = dtstamp.dt.astimezone(localtz)
return event
def print_event(event):
"""Print in an informal but readable way."""
print(f"START: {event['LOCALSTART'].strftime('%a, %d %b %Y %I:%M %Z')}")
if event['DTSTART'].tzinfo != event['LOCALSTART'].tzinfo:
print(f" ({event['DTSTART'].strftime('%a, %d %b %Y %I:%M %Z')})")
print(f" END: {event['LOCALEND'].strftime('%a, %d %b %Y %I:%M %Z')}")
if event['DTEND'].tzinfo != event['LOCALEND'].tzinfo:
print(f" ({event['DTEND'].strftime('%a, %d %b %Y %I:%M %Z')})")
print("SUMMARY", event['SUMMARY'])
print("LOCATION", event['LOCATION'])
# Description for some events is really long.
if len(event['DESCRIPTION']) > 300:
print("DESCRIPTION", event['DESCRIPTION'][:300], "...")
else:
print("DESCRIPTION", event['DESCRIPTION'])
def remind_for_event(ev):
"""Create a line that can be added to a file for /usr/bin/remind."""
desc = ev['DESCRIPTION'].replace('\n\n', '\n').replace('\n', ' ||| ')
print(f"REM {ev['LOCALSTART'].strftime('%d %m %Y')} +1 MSG {ev['SUMMARY']} ||| LOCATION {ev['LOCATION']} ||| DESCRIPTION: ||| {desc}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Parse ical files")
parser.add_argument('-f', action="store", dest="format", default="text",
help='Output format: text (default), remind')
parser.add_argument('files', nargs='+', help="Input ical files")
args = parser.parse_args(sys.argv[1:])
# print("args", args)
events = [ read_ics_file(f) for f in args.files ]
for ev in events:
if args.format == "remind":
remind_for_event(ev)
else:
print_event(ev)