Skip to content

Instantly share code, notes, and snippets.

@andir
Created August 18, 2016 14:46
Show Gist options
  • Save andir/dc29d470f5df6206628d4d090094c869 to your computer and use it in GitHub Desktop.
Save andir/dc29d470f5df6206628d4d090094c869 to your computer and use it in GitHub Desktop.
convert taskwarrior tasks to ical entries when they have a scheduled date
#!/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)
@valhovey
Copy link

valhovey commented Sep 7, 2018

This is great! Thanks for doing this. ics has changed their API, by the way. Instead of c.events.append(e) on line 22, it should be c.events.add(e).

@bjce
Copy link

bjce commented Jul 10, 2021

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

@bjce
Copy link

bjce commented Jul 10, 2021

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