-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion_to_habitica.py
197 lines (175 loc) · 7.98 KB
/
notion_to_habitica.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
import traceback
from notion_wrapper import Notion
from habitica_wrapper import Habitica
import os
from dotenv import load_dotenv
import dateutil.parser as d_util
from utils import setup_logger
load_dotenv()
NOTION_TOKEN = os.getenv("NOTION_TOKEN")
NOTION_DATABASE_ID = os.getenv('NOTION_DATABASE_ID')
HABITICA_TOKEN = os.getenv("HABITICA_TOKEN")
HABITICA_USER_ID = os.getenv("HABITICA_USER_ID")
TYPE_MAPPING = {
'Habit': 'habit',
'To Do': 'todo',
'Daily': 'daily',
}
DAYS = {
'Sunday': 'su',
'Monday': 'm',
'Tuesday': 't',
'Wednesday': 'w',
'Thursday': 'th',
'Friday': 'f',
'Saturday': 's'
}
DIFFICULTY_MAPPING = {
'Trivial': '0.1',
'Easy': '1',
'Medium': '1.5',
'Hard': '2',
}
class Sync():
def __init__(self, notion_token, habitica_token, habitica_user_id):
self.notion = Notion(notion_token)
self.habitica = Habitica(habitica_user_id, habitica_token)
self.mandatory_fields = [
'Name',
'TaskType',
]
self.logger = setup_logger("NotionToHabitica")
def item_is_incomplete(self, item):
# Don't pass references to other objects columns.
for field in self.mandatory_fields:
if not item['properties'][field][item['properties'][field]['type']] \
and item['properties'][field]['type'] != 'checkbox':
return True
return False
def parse_repeat_days(self, days):
days = [DAYS[x['name']] for x in days]
skipped_days = [x for x in DAYS.values() if x not in days]
return {x: False for x in skipped_days}
def make_habitica_item(self, notion_item, type):
item = {
'type': type,
'text': notion_item['properties']['Name']['title'][0]['plain_text'],
'frequency': notion_item['properties']['Frequency']['select']['name'].lower(),
'attribute': notion_item['properties']['Attribute']['select']['name'][:3].lower() if
notion_item['properties']['Attribute']['select'] else None,
'everyX': notion_item['properties']['RepeatEvery']['number'],
'startDate': notion_item['properties']['StartDate']['date']['start'] if
notion_item['properties']['StartDate']['date'] else None,
'priority': DIFFICULTY_MAPPING[notion_item['properties']['Difficulty']['select']['name']],
# 'tags': 'to_implement?', # TODO category would be nice
}
if type == 'habit':
item['up'] = 1 if notion_item['properties']['Up']['checkbox'] else 0
item['down'] = 1 if notion_item['properties']['Down']['checkbox'] else 0
elif type == 'daily':
freq = item['frequency']
if freq == 'weekly':
item['repeat'] = self.parse_repeat_days(notion_item['properties']['WeeklyRepeatOn']['multi_select'])
elif freq == 'monthly':
if notion_item['properties']['MonthlySameWeekday']['checkbox']:
item['weeksOfMonth'] = True
else:
item['daysOfMonth'] = True
elif type == 'todo':
item['date'] = item['startDate']
else:
pass # Return error, not implemented.
return item
def sync_habitca_challenges(self):
"""
Updates select options from Notion column Challenge with
all available challenges in Habitica
:return:
"""
self.logger.info("Syncing habitica challenges...")
challenge_names = self.habitica.get_challenges()
if challenge_names:
challenge_names = [x['shortName'] for x in challenge_names['data']]
payload = {
"properties": {
"ChallengeName": {
"select": {
'options': [{'name': x} for x in challenge_names]
}
},
}}
self.notion.update_database(NOTION_DATABASE_ID, payload)
self.logger.info("Done!")
else:
self.logger.info("No challenges found.")
def sync_tasks(self):
self.logger.info("Syncing tasks...")
notion_items = self.notion.get_all_db_items(NOTION_DATABASE_ID)
# Filter out those with incomplete data:
notion_items = [x for x in notion_items if not self.item_is_incomplete(x)]
# Update each item in Habitica if necessary
for notion_item in notion_items:
try:
item_type = TYPE_MAPPING[notion_item['properties']['TaskType']['select']['name']]
# if notion_item['properties']['TaskType']['select']['name'] == 'Habit':
try:
habitica_item = self.habitica.get_task(
notion_item['properties']['habitica_id']['rich_text'][0]['plain_text'])
except IndexError:
habitica_item = None
if habitica_item:
# Update Habitica item
if d_util.parse(notion_item['last_edited_time']) > d_util.parse(habitica_item['data']['updatedAt']):
self.logger.info(f"Updating notion item "
f"|{notion_item['properties']['Name']['title'][0]['plain_text']}| "
)
item = self.make_habitica_item(notion_item, item_type)
self.habitica.update_habit(
notion_item['properties']['habitica_id']['rich_text'][0]['plain_text'],
item
)
else:
# Nothing's changed, pass
self.logger.info(f"Skipping notion item "
f"|{notion_item['properties']['Name']['title'][0]['plain_text']}| "
f": no changes")
else:
# New item, insert into Habitica.
if notion_item['properties']['ChallengeName']['select']:
# Get challenges to fetch the ChallengeID
challenges = self.habitica.get_challenges()
idd = [x for x in challenges['data'] if
x['shortName'] == notion_item['properties']['ChallengeName']['select']['name']][0]['id']
result = self.habitica.insert_challenge_habit(idd, self.make_habitica_item(notion_item, item_type))
else:
result = self.habitica.insert_habit(self.make_habitica_item(notion_item, item_type))
# And update item in Notion
if result['success']:
payload = {
"properties": {
"habitica_id": {
"rich_text": [
{
"text": {
"content": result['data']['_id']
}
}
]
},
}}
self.notion.update_page(notion_item['id'], payload)
else:
self.logger.error(f"Had trouble inserting habit into Habitica. Please investigate: "
f"{result.json()}")
except Exception:
self.logger.error(f"Failed to sync notion item "
f"{notion_item['properties']['Name']['title'][0]['plain_text']} with error:"
f" {traceback.format_exc()}")
self.logger.info("Done!")
return notion_items
def sync_all(self):
self.sync_habitca_challenges()
self.sync_tasks()
if __name__ == '__main__':
s = Sync(NOTION_TOKEN, HABITICA_TOKEN, HABITICA_USER_ID)
s.sync_all()