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 |
---|---|
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 |
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.
It was an Anaconda environment, but since there was no gkeepapi library in conda, I installed it with pip.
pip install gkeepapi
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.
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.
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