Last active
July 16, 2024 12:18
-
-
Save gauchy/3866fa059eecd2081ec92bea07a4a29f to your computer and use it in GitHub Desktop.
Notion to Habitica Sync tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, json | |
#Notion's token and databaseId | |
token = 'XXX' | |
databaseId = 'XXX' | |
#Notion's headers | |
headers = { | |
"Authorization": "Bearer " + token, | |
"Content-Type": "application/json", | |
"Notion-Version": "2021-05-13" | |
} | |
#Habitica's headers | |
headersHabitica = { | |
"x-api-user": "XXX", | |
"x-api-key": "XXX" | |
} | |
#Completed Status | |
def completed(): | |
return 'Completed'; | |
#Prefix to the task when created in Habitica | |
def prefixChar(): | |
return "N2H_" | |
def readDatabaseOfNotion(databaseId, headers): | |
readUrl = f"https://api.notion.com/v1/databases/{databaseId}/query" | |
res = requests.request("POST", readUrl, headers=headers) | |
data = res.json() | |
print(res.status_code) | |
# print(res.text) | |
with open('./notion.json', 'w', encoding='utf8') as f: | |
json.dump(data, f, ensure_ascii=False) | |
def readHabiticaData(headersHabitica): | |
url = "https://habitica.com/api/v3/tasks/user?type=todos" | |
res = requests.request("GET", url, headers=headersHabitica) | |
data = res.json() | |
print(res.status_code) | |
# print(res.text) | |
with open('./habitica.json', 'w', encoding='utf8') as f: | |
json.dump(data, f, ensure_ascii=False) | |
def readHabiticaDoneData(headersHabitica): | |
url = "https://habitica.com/api/v3/tasks/user?type=completedTodos" | |
res = requests.request("GET", url, headers=headersHabitica) | |
data = res.json() | |
print(res.status_code) | |
# print(res.text) | |
with open('./habitica_done.json', 'w', encoding='utf8') as f: | |
json.dump(data, f, ensure_ascii=False) | |
def createTodoInHabitica(name, headersHabitica): | |
name = prefixN2H(name) | |
url = "https://habitica.com/api/v3/tasks/user" | |
res = requests.post(url, headers=headersHabitica, json={"text": name, "type": "todo", "priority":"2"}) | |
data = res.json() | |
#print(res.text) | |
def scoreTaskInHabitica(id): | |
url = f"https://habitica.com/api/v3/tasks/{id}/score/up" | |
res = requests.post(url, headers=headersHabitica) | |
print(res.status_code) | |
def scoreTaskInNotion(name, headers): | |
url = f"https://api.notion.com/v1/pages/{name}" | |
res = requests.patch(url, headers=headers, json={"properties": {"Status" : {"select" :{"name": completed()}}}}) | |
data = res.json() | |
#print(res.text) | |
print(res.status_code) | |
def prefixN2H(taskName): | |
return prefixChar()+taskName | |
def isAbsentInHabitica(taskName, habiticaList): | |
taskName = prefixN2H(taskName) | |
for i in habiticaList: | |
if taskName == i['name']: | |
return False | |
return True | |
def getHabiticaList(habitica_file): | |
lst = [] | |
f = open(habitica_file,encoding="utf8") | |
data = json.load(f) | |
for i in data['data']: | |
name = i['text'] | |
id = i['id'] | |
dict = {'name':name , 'id':id} | |
lst.append(dict) | |
f.close() | |
return lst | |
def getNotionList(condn): | |
lst = [] | |
f = open("notion.json") | |
data = json.load(f) | |
for i in data['results']: | |
name = i['properties']['Name']['title'][0]['text']['content'] | |
status = i['properties']['Status']['select']['name'] | |
id = i['id'] | |
if condn(status): | |
dict = {'name':name , 'id':id} | |
lst.append(dict) | |
f.close() | |
return lst | |
def notionDoneCondn(status): | |
return status == completed() | |
def notionNotDoneCondn(status): | |
return status != completed() | |
def getDoneListOfNotion(): | |
return getNotionList(notionDoneCondn) | |
def getNotDoneListOfNotion(): | |
return getNotionList(notionNotDoneCondn) | |
def getTaskId(name, list): | |
for i in list: | |
if name == i['name']: | |
return i['id'] | |
def syncNotionToHabitica(): | |
print('==========================') | |
print('Syncing Notion to Habitica') | |
print('==========================') | |
habiticaList = getHabiticaList("habitica.json") | |
notionDoneList = getDoneListOfNotion() | |
for task in notionDoneList: | |
print('Processing completed Notion Task in Habitica ' + task['name']) | |
name = prefixN2H(task['name']) | |
habiticaid = getTaskId(name,habiticaList) | |
if habiticaid is not None: | |
print('Scoring in Habitica for ' + name + ':' + habiticaid) | |
scoreTaskInHabitica(habiticaid) | |
notionNotDoneList = getNotDoneListOfNotion() | |
for task in notionNotDoneList: | |
print('Processing Incomplete Notion Task ' + task['name']) | |
if isAbsentInHabitica(task['name'],habiticaList): | |
print('Missing in Habitica, Creating '+ task['name'] ) | |
createTodoInHabitica(task['name'], headersHabitica) | |
def syncHabiticaToNotion(): | |
print('==========================') | |
print('Syncing Habitica to Notion') | |
print('==========================') | |
notionNotDoneList = getNotDoneListOfNotion() | |
habiticaDoneList = getHabiticaList("habitica_done.json") | |
for task in notionNotDoneList: | |
try: | |
print('Processing Notion task ' + task['name']) | |
habitica_name = prefixN2H(task['name']) | |
for i in habiticaDoneList : | |
if habitica_name == i['name']: | |
print('Scoring in Notion for ' + habitica_name + ':' + task['id']) | |
scoreTaskInNotion(task['id'], headers) | |
except Exception as e: | |
#print(e) | |
print('Cannot score : Old or absent Habitica todo ' + task['name']) | |
def readDB(): | |
print('==========================') | |
print('Reading data') | |
print('==========================') | |
print('Reading Notion Data') | |
readDatabaseOfNotion(databaseId, headers) | |
print('Reading Habitica Data') | |
readHabiticaData(headersHabitica) | |
print('Reading Habitica Done Data') | |
readHabiticaDoneData(headersHabitica) | |
readDB() | |
syncNotionToHabitica() | |
syncHabiticaToNotion() | |
Hi there, I updated some steps of the tutorial like adding the integration and add few code inside like the difficulty of the task in Habitica depends on the priority in Notion or removing the prefix.
https://github.com/vandaref/from_notion_to_habitica/tree/main
My bad I forgot to make a fork of your work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following is machine translation, English is not my native language.
Great project 👍
two way sync is work, but if the task name is duplicated, there will be problems.
I made some small modifications 62 - 190
syncNotionToHabitica
function check "habitica.json" and "habitica_done.json" for absences