-
-
Save Hydrock/7e0612d3a002a91cd420c8eeb3400e70 to your computer and use it in GitHub Desktop.
Notion to Habitica Sync tool
This file contains hidden or 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 | |
token = 'XXX' | |
databaseId = 'XXX' | |
headers = { | |
"Authorization": "Bearer " + token, | |
"Content-Type": "application/json", | |
"Notion-Version": "2021-05-13" | |
} | |
headersHabitica = { | |
"x-api-user": "XXX", | |
"x-api-key": "XXX" | |
} | |
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 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"}) | |
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 prefixN2H(taskName): | |
return "N2H_"+taskName | |
def isAbsentInHabitica(taskName, habiticaList): | |
taskName = prefixN2H(taskName) | |
for i in habiticaList: | |
if taskName == i['name']: | |
return False | |
return True | |
def getHabiticaList(): | |
lst = [] | |
f = open("habitica.json") | |
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", encoding='utf-8') | |
data = json.load(f) | |
for i in data['results']: | |
name = i['properties']['Name']['title'][0]['text']['content'] | |
status = i['properties']['Status']['select']['name'] | |
if condn(status): | |
lst.append(name) | |
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, habiticaList): | |
name = prefixN2H(name) | |
for i in habiticaList: | |
if name == i['name']: | |
return i['id'] | |
def syncNotionToHabitica(): | |
print('Reading Notion DB') | |
readDatabaseOfNotion(databaseId, headers) | |
print('Reading Habitica DB') | |
readHabiticaData(headersHabitica) | |
habiticaList = getHabiticaList() | |
notionDoneList = getDoneListOfNotion() | |
for task in notionDoneList: | |
print('Processing Completed Task ' + task) | |
habiticaid = getTaskId(task,habiticaList) | |
if habiticaid is not None: | |
print('Scoring in Habitica for ' + habiticaid) | |
scoreTaskInHabitica(habiticaid) | |
notionNotDoneList = getNotDoneListOfNotion() | |
for task in notionNotDoneList: | |
print('Processing InComplete Task' + task) | |
if isAbsentInHabitica(task,habiticaList): | |
print('Missing in Habitica, Creating '+ task ) | |
createTodoInHabitica(task, headersHabitica) | |
syncNotionToHabitica() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment