Skip to content

Instantly share code, notes, and snippets.

@Hydrock
Forked from gauchy/notion2Habitica.py
Created May 8, 2022 17:26
Show Gist options
  • Select an option

  • Save Hydrock/7e0612d3a002a91cd420c8eeb3400e70 to your computer and use it in GitHub Desktop.

Select an option

Save Hydrock/7e0612d3a002a91cd420c8eeb3400e70 to your computer and use it in GitHub Desktop.

Revisions

  1. @gauchy gauchy revised this gist Jan 5, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion notion2Habitica.py
    Original file line number Diff line number Diff line change
    @@ -92,7 +92,7 @@ def getHabiticaList():

    def getNotionList(condn):
    lst = []
    f = open("notion.json")
    f = open("notion.json", encoding='utf-8')
    data = json.load(f)

    for i in data['results']:
  2. @gauchy gauchy revised this gist Nov 15, 2021. No changes.
  3. @gauchy gauchy renamed this gist Nov 15, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @gauchy gauchy created this gist Nov 15, 2021.
    159 changes: 159 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,159 @@
    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")
    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()