Skip to content

Commit

Permalink
update N from GT
Browse files Browse the repository at this point in the history
  • Loading branch information
kris524 committed Nov 26, 2023
1 parent d1f0d1d commit 6646aca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
68 changes: 38 additions & 30 deletions sync/google_to_notion_task_sync.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
"""Script that handles the sync from Google Tasks to Notion"""
import os
from notion_client import Client
from pprint import pprint
from dotenv import load_dotenv
from typing import List, Dict
import os.path
import logging

from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import redis
from notion_to_google_task_sync import authenticate_and_print

# Extra features:
# (???) If a task is added to GC, it should be added to Notion as well (???) Where?
# TODO: Currently you have a Many-to-one relationship, you might want to have a to One-to-one relationship
# This is what we want to work on now, we cant establish a true two way connection until we have one to one mapping

from notion_to_google_task_sync import authenticate_and_print
from notion_to_google_task_sync import r_reverse


# TODO: Also, update the redis db to have the reverse mapping, map from G id to N id

# DONE: Also, update the redis db to have the reverse mapping, map from G id to N id
# DONE If a task is ticked on Google Calendar, it should be ticket on Notion as well, etc.
# DONE If a task is changed (text edited) in GC it should be changed in Notion as well
# TODO If a task is deleted on GC, if should be deleted in Notion as well
# TODO If a task is added in GC, add it to Notion, corresponding page AND redis
# TODO If a task is deleted on GC, if should be deleted in Notion as well AND in the database


# def insert_google_task_into_notion(service, task_list_id):
# """Insert google tasks into notion"""
# current_google_tasks = [
# {"title": task["title"], "id": task["id"], "status": task["status"]}
# for task in service.tasks().list(tasklist=task_list_id).execute()["items"]
# ]



def update_notion_tasks(service, client, task_list_id):
"""Function that Updates tasks. Closes tasks marked as completed from Notion to Google Takss"""

def insert_google_task_into_notion(service, task_list_id):
"""Insert google tasks into notion"""
current_google_tasks = [
{"title": task["title"], "id": task["id"], "status": task["status"]}
for task in service.tasks().list(tasklist=task_list_id).execute()["items"]
for task in service.tasks().list(tasklist=task_list_id, showHidden=True).execute()["items"]
]


for google_task in current_google_tasks:

if r_reverse.get(google_task["id"]) is not None:
block = client.blocks.retrieve(r_reverse.get(google_task["id"]))

if google_task["status"] == "needsAction":
block["to_do"]["checked"] = False
else:
block["to_do"]["checked"] = True

block["to_do"]["rich_text"][0]["text"]["content"] = google_task["title"]
block["to_do"]["rich_text"][0]["plain_text"] = google_task["title"]

client.blocks.update(r_reverse.get(google_task["id"]), **block)



if __name__ == "__main__":
Expand All @@ -47,12 +59,8 @@ def insert_google_task_into_notion(service, task_list_id):

# this is the functionality that needs to be generalised in a function,
# this will be used for changed text and tick box changes
block = client.blocks.retrieve("0f3d06aa-4a00-4ce8-81cd-02e9ea24efb1")
print(block)
block["to_do"]["checked"] = False
block["to_do"]["rich_text"][0]["text"]["content"] = "Hello World"

client.blocks.update("0f3d06aa-4a00-4ce8-81cd-02e9ea24efb1", **block)
# insert_google_task_into_notion(service, task_list_id)
# import ipdb;ipdb.set_trace()
update_notion_tasks(service, client, task_list_id="eEctUkkwdGctZ3Q1d3RoQg")

print(block)
# insert_google_task_into_notion(service, task_list_id)
tasklists_id = ["eEctUkkwdGctZ3Q1d3RoQg", "TXN4Y01pN2FOTk9kTnpUbw", "aXBIaHhfNVMzeGo5VWVncg"]
11 changes: 8 additions & 3 deletions sync/notion_to_google_task_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

SCOPES = ["https://www.googleapis.com/auth/tasks"]

r = redis.Redis(host="localhost", port=6379, decode_responses=True)
r = redis.Redis(host="localhost", port=6379, decode_responses=True, db=0)

r_reverse = redis.Redis(host="localhost", port=6379, decode_responses=True, db=1)

# -----------------
# NOTION FUNCTIONS
Expand Down Expand Up @@ -170,6 +172,8 @@ def add_id_mapping_to_redis(service, notion_tasks, task_list_id):
and notion_task["status"] == google_task["status"]
):
r.set(notion_task["id"], google_task["id"])
r_reverse.set(google_task["id"], notion_task["id"]) # store reverse mapping in db1

logging.info(
f"Successfully added k: {notion_task['id']} v: {google_task['id']}"
)
Expand All @@ -187,13 +191,14 @@ def remove_deleted_tasks_ids_from_redis(service, notion_tasks, task_list_id):
service.tasks().delete(
tasklist=task_list_id, task=r.get(notion_id_in_db)
).execute()
google_task_id = r.get(notion_id_in_db)
r.delete(notion_id_in_db)

r_reverse.delete(google_task_id)

if __name__ == "__main__":

load_dotenv()
NOTION_ID = os.getenv("NOTION_KEY") # NOTION_KEY
NOTION_ID = os.getenv("NOTION_KEY")
if NOTION_ID is None:
raise KeyError("Missing NOTION ID environment variable")

Expand Down

0 comments on commit 6646aca

Please sign in to comment.