-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·148 lines (124 loc) · 4.56 KB
/
main.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/env python3
import json
import argparse
import sys
import codecs
from datetime import datetime
from typing import Dict, List
def parse_timestamp(timestamp: str) -> str:
# format as 19810329T020000
# incoming as 2017-03-22T15:06:28.338Z
# first throw away .XXXZ
parsed = datetime.strptime(timestamp[:timestamp.rfind('.')], '%Y-%m-%dT%H:%M:%S')
return parsed.strftime("%Y%m%dT%H%M%SZ")
def handle_list(todo_list: Dict, flatten_subtasks: bool, subtasks_as_description: bool, inherit_completed: bool,
ignore_completed: bool) -> List[str]:
str_list = []
for task in todo_list["tasks"]:
if ignore_completed and task["completed"]:
continue;
description = ""
# add subtasks to description if needed
if subtasks_as_description and len(task["subtasks"]) > 0:
for subtask in task["subtasks"]:
if subtask["completed"]:
status = "[x]"
else:
status = "[ ]"
description += f"{status} {subtask['title']}\n"
# parse notes
if len(task["notes"]) > 0:
if subtasks_as_description and len(task["subtasks"]) > 0:
description += "\n"
description += "".join([note["content"] for note in task["notes"]])
description += "\n"
# replace newlines in desc with literal '\n'
desc_literal = description.replace('\n', '\\n')
parsed_str = f"""
BEGIN:VTODO
DTSTAMP:{parse_timestamp(task["createdAt"])}
SEQUENCE:0
SUMMARY:{task["title"]}
CREATED:{parse_timestamp(task["createdAt"])}
UID:{task["id"]}
DESCRIPTION:{desc_literal}
"""
# add completed info
if task["completed"]:
parsed_str += f"""STATUS:COMPLETED
COMPLETED:{parse_timestamp(task["completedAt"])}
PERCENT-COMPLETE:100
LAST-MODIFIED:{parse_timestamp(task["completedAt"])}
"""
# add priority based on starred flag
if task["starred"]:
parsed_str += "PRIORITY:1\n"
# due date
if task["dueDate"]:
parsed_str += f"DUE:{parse_timestamp(task['dueDate'])}\n"
parsed_str += "END:VTODO"
if flatten_subtasks and len(task["subtasks"]) > 0:
# add all subtasks as their own tasks
for idx, subtask in enumerate(task["subtasks"]):
subtask_id = f"{task['id']}-{idx}"
subtask_title = f"{task['title']} - {subtask['title']}"
parsed_str += f"""
BEGIN:VTODO
DTSTAMP:{parse_timestamp(subtask["createdAt"])}
SEQUENCE:0
SUMMARY:{subtask_title}
CREATED:{parse_timestamp(subtask["createdAt"])}
UID:{subtask_id}
"""
if subtask["completed"]:
parsed_str += f"""STATUS:COMPLETED
COMPLETED:{parse_timestamp(subtask["completedAt"])}
PERCENT-COMPLETE:100
LAST-MODIFIED:{parse_timestamp(subtask["completedAt"])}
"""
else:
if task["completed"] and inherit_completed:
parsed_str += f"""STATUS:COMPLETED
COMPLETED:{parse_timestamp(task["completedAt"])}
PERCENT-COMPLETE:100
LAST-MODIFIED:{parse_timestamp(task["completedAt"])}
"""
parsed_str += "END:VTODO"
str_list.append(parsed_str)
return str_list
def write_ics(name: str, str_list: List[str]) -> None:
ics_str = """BEGIN:VCALENDAR
PRODID:wasmitnetzen/wunder2ics
VERSION:2.0"""
ics_str += ''.join(str_list)
ics_str += "\nEND:VCALENDAR"
with open(f"output/{name}.ics", "w") as f:
# RFC 5545 3.1. Content Lines requires CLRF line endings
f.write(ics_str.replace('\n', '\r\n'))
def main(file, flatten_subtasks: bool, subtasks_as_description: bool, inherit_completed: bool,
ignore_completed: bool) -> None:
with codecs.open(file, 'r', 'utf-8-sig') as json_file:
data = json.load(json_file)
for todo_list in data:
parsed = handle_list(todo_list, flatten_subtasks, subtasks_as_description, inherit_completed,
ignore_completed)
write_ics(todo_list["title"], parsed)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Parse Wunderlist export.')
parser.add_argument("--subtasks-as-description", help="Put subtasks into description of parent task",
action="store_true")
parser.add_argument("--flatten-subtasks",
help="Put subtasks into their own task, with textual reference of parent task",
action="store_true")
parser.add_argument("--inherit-completed",
help="If a parent of a subtask is done, it will be considered as done as well",
action="store_true")
parser.add_argument("--ignore-completed",
help="Omit completed tasks",
action="store_true")
parser.add_argument('file', metavar='f', help='The Tasks.json from the Wunderlist export')
args = parser.parse_args()
if args.inherit_completed and not args.flatten_subtasks:
print("Warning: --inherit-completed is ignored without enabling --flatten-subtasks.", file=sys.stderr)
sys.exit(main(args.file, args.flatten_subtasks, args.subtasks_as_description, args.inherit_completed,
args.ignore_completed))