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)
@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