-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.py
225 lines (180 loc) · 8.25 KB
/
item.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import re
class Item:
def __init__(self, api, h_helper, t_id, h_id=0):
self.is_repeating = False
self.is_checklist = False
self.is_checklist_item = False
self.indent = 1
self.parent = t_id
self.h_helper = h_helper
self.api = api
self.todoist_id = t_id
self.habit_id = h_id
self.task = {}
self.type = "todo"
self.repeats_on = {}
self._update_from_todoist()
def _update_from_todoist(self):
_item = self.api.items.get_by_id(self.todoist_id)
self.priority = _item["priority"]
self.parent_id = _item["parent_id"]
self.content = _item["content"]
self.creation_date = _item["date_added"]
self.due_date = _item["due"]
self.collapsed = _item["collapsed"]
self.priority = _item["priority"]
self.completed = _item["checked"] == 1
self.habit_priority = float(self.priority) / 2
if self.habit_priority == 0.5:
self.habit_priority = '0.1'
elif self.habit_priority == 1:
self.habit_priority = '1'
elif self.habit_priority == 1.5:
self.habit_priority = '1.5'
elif self.habit_priority == 2:
self.habit_priority = '2'
if 'date_string' in _item:
if _item["date_string"]:
self.date_string = _item["date_string"].lower()
else:
self.date_string = ""
else:
self.date_string = ""
self.tags = []
try:
self.tags = [
self.h_helper.get_tag_id_by_name("p:" + self.api.projects.get_by_id(_item["project_id"])["name"])]
except AttributeError:
print(f"Old project was found and skipped, {_item['project_id']}")
for label in _item["labels"]:
try:
self.tags += [self.h_helper.get_tag_id_by_name(self.api.labels.get_by_id(label)["name"])]
except AttributeError: # issue #10
print(f"Old label was found, {label:d}")
# self.notes = ""
# for note in _notes:
# self.notes += note["content"]
# self.notes += "\n\n"
self.repeats_on = {}
self._check_for_repeating()
# collapse 4 levels of indentation into 2
if 'indent' in _item:
if _item["indent"] > 1 and _item["parent_id"]:
self.indent = _item["indent"]
self.is_checklist_item = True
parent = self.api.items.get_by_id(_item["parent_id"])
while parent["indent"] > 1:
parent = self.api.items.get_by_id(parent["parent_id"])
self.parent = parent["id"]
self._update_task()
def _update_task(self):
self.task["text"] = self.content
self.task["date"] = self.h_helper.date_t_to_h(self.due_date)
self.task["startDate"] = self.h_helper.date_t_to_h(self.due_date)
self.task["tags"] = self.tags
self.task["completed"] = self.completed
self.task["type"] = self.type
self.task["dateCreated"] = self.creation_date
self.task["repeat"] = self.repeats_on
self.task["priority"] = self.habit_priority
def sync_to_habitrpg(self):
self._update_from_todoist()
if self.habit_id == 0:
self.habit_id = self.h_helper.upload_task(self.task)
return
habit_task = self.h_helper.download_task(self.habit_id)
if habit_task["completed"]:
if not self.task["completed"]: # was uncompleted on todoist?
if not self.type == "daily": # see issue #1
self.h_helper.score_task(self.habit_id, direction="down")
else:
self.task.pop("startDate")
self.h_helper.update_task(self.habit_id, self.task)
return
if not habit_task["completed"]:
if self.task["completed"]: # normal todo was completed
self.h_helper.score_task(self.habit_id)
elif self.is_repeating:
if self.type == "daily":
date_key = "startDate"
else:
date_key = "date"
if self.task["date"] != habit_task[date_key]: # TODO actually compare them with >
self.h_helper.score_task(self.habit_id)
if self.task["type"] == "todo": # dailies renew on their own
self.habit_id = self.h_helper.upload_task(self.task) # make new task and associate it
# update every time
self.h_helper.update_task(self.habit_id, self.task) # just update the task
def delete_from_habitrpg(self):
if self.habit_id == 0:
return
self.h_helper.delete_task(self.habit_id)
self.habit_id = 0
def _check_for_repeating(self):
self.type = "todo"
self.repeats_on = {}
year_strings = ["yearly", "every year"]
month_strings = ["monthly", "every month"]
day_strings = ["daily", "every day", "ev day"]
if not self.date_string:
self.is_repeating = False
return
for word, num in zip(
["first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eighth", "ninth", "tenth"],
["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th"]):
self.date_string = self.date_string.replace(word, num)
self.repeats_on = {}
# build the regex for yearly tasks
ev_yrly = "(yearly|ev(ery)?( year)?)"
ev_mthly = "(monthly|ev(ery)?( month)?)"
ev_daily = "(daily|(ev(ery)?( day)?))"
on = "( on)?"
the_xth = "(( the)? \\d+(st|nd|rd|th)?)"
of = "( of)?"
month = "( (jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?" + \
"|jul(y)?|aug(ust)?|sep(t)?(ember)?|oct(ober)?|nov(ember)?|dec(ember)?))"
monday = "mon(day)?"
tuesday = "tue(s(day)?)?"
wednesday = "wed(nesday)?"
thursday = "thu(r(s(day)?)?)?"
friday = "fri(day)?"
saturday = "sat(urday)?"
sunday = "sun(day)?"
day = "(%s|%s|%s|%s|%s|%s|%s)" % (monday, tuesday, wednesday, thursday, friday, saturday, sunday)
re_or = "|"
decimals = " \d+"
stop = "$"
yrly_re = re.compile(
ev_yrly + on + the_xth + "?" + of + month + the_xth + "?" + re_or + ev_yrly + decimals + stop,
re.IGNORECASE) # https://regex101.com/r/xUnNAT/10
# build the regex for monthly tasks
mthly_re = re.compile(
ev_mthly + "(" + on + the_xth + day + "?" + re_or + day + "?" + the_xth + ")"
) # https://regex101.com/r/UoiLVm/5
daily_re = re.compile(ev_daily + " (" + day + re_or + "weekday|weekend|day" + ")")
# get yearlies
if self.date_string in year_strings or re.match(yrly_re, self.date_string):
self.is_repeating = True
return
# get monthlies
if self.date_string in month_strings or re.match(mthly_re, self.date_string):
self.is_repeating = True
return
# get dailies TODO renew!
if self.date_string in day_strings or re.match(daily_re, self.date_string):
self.is_repeating = True
self.type = "daily"
everyday = self.date_string in day_strings
weekend = re.match(ev_daily + " weekend", self.date_string)
weekday = re.match(ev_daily + " weekday", self.date_string)
self.repeats_on = {
"su": bool(everyday or weekend or re.search(sunday, self.date_string, re.IGNORECASE)),
"s": bool(everyday or weekend or re.search(saturday, self.date_string, re.IGNORECASE)),
"f": bool(everyday or weekday or re.search(friday, self.date_string, re.IGNORECASE)),
"th": bool(everyday or weekday or re.search(thursday, self.date_string, re.IGNORECASE)),
"w": bool(everyday or weekday or re.search(wednesday, self.date_string, re.IGNORECASE)),
"t": bool(everyday or weekday or re.search(tuesday, self.date_string, re.IGNORECASE)),
"m": bool(everyday or weekday or re.search(monday, self.date_string, re.IGNORECASE))
}
return