Created
August 18, 2016 14:46
-
-
Save andir/dc29d470f5df6206628d4d090094c869 to your computer and use it in GitHub Desktop.
convert taskwarrior tasks to ical entries when they have a scheduled date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os.path | |
from ics import Calendar, Event | |
import subprocess | |
import json | |
def read_tasks(): | |
output = subprocess.check_output(['task', 'export']) | |
for task in json.loads(output.decode('utf-8')): | |
if task['status'] == 'completed': | |
continue | |
if 'scheduled' in task: | |
yield task | |
if __name__ == "__main__": | |
c = Calendar() | |
for task in read_tasks(): | |
e = Event() | |
e.name = task['description'] | |
e.begin = task['scheduled'] | |
c.events.append(e) | |
with open(os.path.expanduser('~/.local/share/calendar/tasks/tasks.ics'), 'w') as f: | |
f.writelines(c) |
this is great indeed many thanks to @andir (and @kylehovey for the comment). However, now in the line 21 i get an error (arrow.parser.ParserError: Could not match input to any of ['YYYY-MM-DDTHH:mm'] on '20210730T220000Z'
). Is it because ics
changed its format (or timewarrior?)
I changed manually the date to 20210730T220000Z
to2021-07-30T00:00
and it worked. But I think it does not correspond to my timeline
Ok. That's the solution that worked for me with datetime package (with my corresponding timezone: + 2 hours). I also collected the due
field instead of the scheduled
field:
#!/usr/bin/env python3
import datetime
from datetime import timedelta
import os.path
from ics import Calendar, Event
import subprocess
import json
def read_tasks():
output = subprocess.check_output(['task', 'export'])
for task in json.loads(output.decode('utf-8')):
if task['status'] == 'completed':
continue
if 'due' in task:
yield task
if __name__ == "__main__":
c = Calendar()
for task in read_tasks():
print(task)
dt = datetime.datetime.strptime(task["due"], "%Y%m%dT%H%M%S%fZ") + timedelta(hours=2)
dt = dt.strftime('%Y-%m-%dT%H:%M')
task["due"] = dt
e = Event()
e.name = task['description']
e.begin = task['due']
c.events.add(e)
with open(os.path.expanduser('~/.local/share/calendar/tasks/tasks.ics'), 'w') as f:
f.writelines(c)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Thanks for doing this.
ics
has changed their API, by the way. Instead ofc.events.append(e)
on line 22, it should bec.events.add(e)
.