Last active
December 21, 2022 15:28
-
-
Save clemlesne/cc749a6fb7a681517394950aebc7199e to your computer and use it in GitHub Desktop.
Import Google Keep notes to Notion
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 csv | |
import gkeepapi | |
import keyring | |
import os | |
import re | |
import urllib.request | |
import uuid | |
from datetime import datetime | |
from notion.block import PageBlock, TextBlock, TodoBlock, BulletedListBlock, NumberedListBlock, ImageBlock | |
from notion.client import NotionClient | |
from tqdm import tqdm | |
GOOGLE_USERNAME = "YOUR EMAIL" | |
GOOGLE_PASSWORD = "YOUR PASSWORD" | |
NOTION_TOKEN = "YOUR TOKEN AS FOUND IN YOUR COOKIES" | |
NOTION_ROOT_URL = "YOUR ROOT URL FROM YOUR BROWSER" | |
print("Logging in to Google Keep (be patient, this can take up to 2 minutes)") | |
keep = gkeepapi.Keep() | |
keep.login(GOOGLE_USERNAME, GOOGLE_PASSWORD) | |
token = keep.getMasterToken() | |
keyring.set_password('google-keep-token', GOOGLE_USERNAME, token) | |
print("Downloading all notes") | |
gnotes = keep.all() | |
print("Importing to Notion") | |
notion = NotionClient(token_v2=NOTION_TOKEN) | |
root = notion.get_block(NOTION_ROOT_URL) | |
for note in tqdm(gnotes): | |
child = root.children.add_new(PageBlock, title=note.title) | |
for line in note.text.split('\n'): | |
if line.startswith("- "): | |
# Add bulleted list item | |
child.children.add_new(BulletedListBlock, title=line[2:]) | |
elif re.match(r'^\d+\. ', line): | |
# Add numbered list item | |
child.children.add_new(NumberedListBlock, title=re.sub(r'^\d+\. ', '', line, 1)) | |
elif line.startswith("☐ "): | |
# Add unchecked level 1 toto item | |
todo = child.children.add_new(TodoBlock, title=line[2:]) | |
elif line.startswith("☑ "): | |
# Add checked level 1 toto item | |
todo = child.children.add_new(TodoBlock, title=line[2:], checked=True) | |
elif line.startswith(" ☐ "): | |
# Add unchecked level 2 toto item | |
todo.children.add_new(TodoBlock, title=line[4:]) | |
elif line.startswith(" ☑ "): | |
# Add checked level 2 toto item | |
todo.children.add_new(TodoBlock, title=line[4:], checked=True) | |
else: | |
# Add text paragraph | |
child.children.add_new(TextBlock, title=line) | |
for image in note.images: | |
name = uuid.uuid4().hex | |
try: | |
url = keep.getMediaLink(image) | |
except KeyError: | |
continue | |
# Download file | |
urllib.request.urlretrieve(url, name) | |
# Upload it to Notion | |
block = child.children.add_new(ImageBlock) | |
block.upload_file(name) | |
# Clean up | |
os.remove(name) | |
# Add import metadata | |
child.children.add_new(TextBlock, title="Google Keep metadata:") | |
child.children.add_new(BulletedListBlock, title=f"Labels {[label.name for label in note.labels.all()]}") | |
child.children.add_new(BulletedListBlock, title=f"Created time {note.timestamps.created}") | |
child.children.add_new(BulletedListBlock, title=f"Last edited time {note.timestamps.updated}") | |
child.children.add_new(BulletedListBlock, title=f"Imported the {datetime.now().isoformat()}") | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment