Copy data between Google Keep accounts in Python

Google account migration

In order to transfer the data of Google account to another Google account, you need to do your best for each service. In the past, we have migrated in the following ways.

data Migration method
Email I did not migrate, but it seems that it can be done by connecting to the old and new accounts with IMAP from Thunderbird etc.
drive Download & upload
calendar Export & import
YouTube Use a brand account, add a new account as a user and make it an administrator
photo Use the sharing function
Keep Download in HTML formatManually copy and paste what you have done to recreate the memo

Keep data migration in Python

I decided to migrate my Google account again this time and decided to use the above method, but since the Keep data has increased from the previous time, I decided to automate the migration of Keep with Python.

Since there is a library called gkeepapi that can operate Keep from Python, I will use it.

Install gkeepapi

It was an Anaconda environment, but since there was no gkeepapi library in conda, I installed it with pip.

pip install gkeepapi

Python script

Write and execute the following script. The process, which would take half a day if done manually, was completed in a few seconds! It took me a day to create and debug the script, but it was a good learning experience.

cpgkeep.py


import gkeepapi

#Log in to the migration source
src_keep = gkeepapi.Keep()
src_success = src_keep.login('[email protected]', 'old-password')

#Login to the migration destination
dst_keep = gkeepapi.Keep()
dst_success = dst_keep.login('[email protected]', 'new-password')

#Copy label
src_labels = src_keep.labels()
for src_label in src_labels:
    if dst_keep.findLabel(src_label.name) is None:
        #Created by confirming that the label with the same name does not exist
        dst_label = dst_keep.createLabel(src_label.name)
        print('created label => [' + src_label.name + ']')

#Sync
dst_keep.sync()

#Copy of memo (note)
src_gnotes = src_keep.all()
for src_gnote in src_gnotes:
    #Creating notes (Do not check for the same name as titles can be duplicated)
    dst_gnote = dst_keep.createNote(src_gnote.title, src_gnote.text)
    #Pinned copy
    dst_gnote.pinned = src_gnote.pinned
    #Copy label
    src_labels = src_gnote.labels.all()
    for src_label in src_labels:
        dst_label = dst_keep.findLabel(src_label.name)
        dst_gnote.labels.add(dst_label)        
    print('created note => [' + src_gnote.title + ']')

#Sync
dst_keep.sync()

What this script is doing is a simple copy of the memo data. I personally used a lot of "pinning" and "labeling" functions, so I made them compatible as well.

We have confirmed that it works on Python 3.6 and 3.8 in the Windows environment.

Difficult points

This is the process of adding a label to a memo. I needed to get the label from the pre-migration memo, but the function or method to do that wasn't listed in the Documentation (https://gkeepapi.readthedocs.io/en/latest/). My heart was broken, but when I tried my best to look at the Module on GitHub, the NodeLabels class defined a method called def all (self): that is not in the document, and I could possibly use this Isn't it? So I wrote src_gnote.labels.all () and tried it, and it was right. It feels like you've found a hidden command.

Case where authentication fails

There was an environment where authentication failed with the following error message.

gkeepapi.exception.LoginException: ('NeedsBrowser', 'To access your account, you
 must sign in on the web. Touch Next to start browser sign-in.')

I do not know the cause well, but if you access the following URL with a browser from the corresponding terminal with both the migration source and migration destination Google accounts, click the "Next" button, and then execute the Python script again, authentication will pass. .. https://accounts.google.com/b/0/DisplayUnlockCaptcha

However, after a while, the authentication error will appear again, so you need to access it again with a browser and then execute it.

The authentication process is hard-coded, such as passwords, so it may be better to review it. I am authenticating with tokens in resume.py, so I would like to try again if I have time.

Recommended Posts

Copy data between Google Keep accounts in Python
Get Google Fit API data in Python
Handle Ambient data in Python
Display UTM-30LX data in Python
Various ways to calculate the similarity between data in python
Get Leap Motion data in Python.
Difference between list () and [] in Python
Difference between == and is in python
Read Protocol Buffers data in Python3
Get data from Quandl in Python
Handle NetCDF format data in Python
Download Google Drive files in Python
Get additional data in LDAP with python
Differences in authenticity between Python and JavaScript
Data input / output in Python (CSV, JSON)
Differences between Ruby and Python in scope
difference between statements (statements) and expressions (expressions) in Python
Ant book in python: Sec. 2-4, data structures
Try working with binary data in Python
Differences in syntax between Python and Java
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Python: Preprocessing in machine learning: Data acquisition
Get Youtube data in Python using Youtube Data API
Exclusive file access between processes in Python
Easily graph data in shell and Python
Python: Preprocessing in machine learning: Data conversion
Differences in multithreading between Python and Jython
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
Creating Google Spreadsheet using Python / Google Data API
Exchange encrypted data between Python and C #
Get time series data from k-db.com in Python
Python variables and data types learned in chemoinformatics
Unattended operation of Google Spreadsheets (etc.) in Python
Receive and display HTML form data in Python
Put text scraped in Python into Google Sheets
Run Google Analytics API (core v3) in python
[Python] Swapping rows and columns in Numpy data
[python] Difference between variables and self. Variables in class
Read table data in PDF file with Python
Make a copy of the list in Python
Real-time visualization of thermography AMG8833 data in Python
[Python] Conversion memo between time data and numerical data
Data analysis in Python: A note about line_profiler
Keep key names case in Python standard ConfigParser
The story of reading HSPICE data in Python
What to do to get google spreadsheet in python
A well-prepared record of data analysis in Python
TensorFlow: Run data learned in Python on Android
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Data analysis python
Epoch in Python
Discord in Python