Skip to content

Python script for syncing Google Play Books highlights to Notion

License

Notifications You must be signed in to change notification settings

MohamedIrfanAM/books2notion

Repository files navigation

Books2Notion

Python script for syncing Google Play Books Highlights to Notion

Google Play Books Notion Notes


Table of Contents


Usage

Red highlights are reserved for saving new words, it gets added to 'New Words' database along with definition fetched from Wiktionary

Sync Modes

  1. append - appends new highlights and new words to notion, but won't sync highlights and new words from pages that have been synced before
  2. sync - syncs complete highlights of the books to notion
  3. sync-full - syncs complete highlights and new words

Automatic

Follow 7. Schedule script to schedule .Script is scheduled to run every hour in append mode. Enable Full_Sync checkbox for books in the database if you want to run the script in sync-full mode in the next scheduled run.

Run Manually

Clone manual branch

git clone --single-branch --branch manual https://github.com/MohamedIrfanAM/books2notion.git
python3 books2notion.py --mode <mode>

Example: python3 books2notion.py --mode sync-full

Installation and Setup

1. Clone Git repo

Make sure you have git installed. To schedule script, clone deployment branch

git clone https://github.com/MohamedIrfanAM/books2notion.git

For running the script manually, clone manual branch

git clone --single-branch --branch manual https://github.com/MohamedIrfanAM/books2notion.git

2.Install Python Modules

Install Python and proceed to install following modules

pip install --upgrade google-api-python-client, google-auth-httplib2, google-auth-oauthlib
pip install Pillow
pip install schedule

3.Authorize Notion

a. Get Notion Integration Token

Goto https://www.notion.so/my-integrations and make an integration, give it a name submit with default settings.


Copy the integration token and save it somewhere.

b. Duplicate template and share it to integration

Duplicate books template and share -> invite -> integration_name -> invite


4. Authorize Google APIs

a. Create Google cloud project

Goto https://console.cloud.google.com/ then click 'select a project'

Give the project a name and click create

b. Create OAuth consent screen

Fill 'name' and 'user support email'

Scroll down fill in 'Email address' and continue

click 'save and continue' for 'Scopes' and 'Test Users', don't have to fill in anything

Publish app

c. Create OAuth2.0 credentials

Download file and save it as credentials.json in the books2notion folder we created in 1. Clone Git repo

d. Enable APIs

We have to enable Books API, Google Drive API and Google Docs API

Search for each APIs mentioned above and enable it

5. Get env variables

a. Get NOTION_DATABASE_ID

Copy the url of database we duplicated in Duplicate Notion template
It should look like this

https://www.notion.so/lucas-gen/e32a031992f348aeae115fe6dee8353?v=1e75f5e2b07349f4b331e88c4ca3beac


Save the databse id somewhere

b. Get DRIVE_FOLDER_ID

Select any book from your Play Books library and open to read, you will find an option to save annotations to google drive and enable it (only have to doit once,then it will be default for everybook).

Go to google drive and navigate to folder you selected for saving annotations and copy it's url.
it should look like this - https://drive.google.com/drive/folders/<folder id>
copy the folder id and save it somewhere

c. Get IMAGE_HOST_KEY

goto https://freeimage.host/page/api?lang=en and save the api key somewhere

d. Get TIME_OFFSET

goto https://www.timeanddate.com/time/zone/ and find time offset from UTC
Example +05:30,-04:00

6. Authorize script

Run the script for the first time python3 schedule_sync.py in the terminal, you will prompted to give access to application via browser. This script will create a .env file in books2notion folder. You don't have to authorize everytime just first time only.
The .env file generated by running above script should look something like this

API_TOKEN=<google API token, filled by script>
REFRESH_TOKEN=<google refresh token, filled by script>
TOKEN_URI=<token URI, filled by script>
CLIENT_ID=<client id, filled by script>
CLIENT_SECRET=<client id, filled by script>
EXPIRY=<token expiry, filled by script>
NOTION_KEY=
NOTION_DATABASE_ID=
DRIVE_FOLDER_ID=
IMAGE_HOST_KEY=
TIME_OFFSET=

7. Schedule script

Heroku

a. Create Heroku Account

goto https://heroku.com/ and make a free account

b. Install Heroku cli

https://devcenter.heroku.com/articles/heroku-cli#install-the-heroku-cli

c. Heroku login
heroku login
d. Create app
heroku create
e. Fill env variables

goto https://dashboard.heroku.com/apps and you find your app, it might not be named books2notion

goto settings

Reveal config vars

Fill in env variables we saved from 3. Get Notion Integration Token, 5. Get env variables, 6. Authorize script

f. add buildback

croll down and you will find option to add buildpack

g. deploy application
git push heroku deployment:main
h. start dyno

Go to resources and toggle on dyno

Now that's it, we have successfully deployed application.

Local

Sheduling locally in Mac and Linux (cron)
Follow this article
Sheduling locally in Windows (windows task scheduler)
Follow this article

About

Python script for syncing Google Play Books highlights to Notion

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published