Install notion-py on Pythonista3

Windows 機でテスト

Anaconda の 仮想環境にインストール

pip install notion

token_V2 は Chrome 拡張機能の EditThisCookie で Notion にアクセスして取得する

database の URL は Notion上で Copy link して取得する

from notion.client import NotionClient

# Obtain the `token_v2` value by inspecting your browser cookies on a logged-in (non-guest) session on Notion.so
client = NotionClient(token_v2="************************************************************************************************************************************************************")

# Access a database using the URL of the database page or the inline block
cv = client.get_collection_view("https://www.notion.so/********************************?v=********************************")

# List all the records with "Bob" in them
for row in cv.collection.get_rows():
    print("We estimate the value of '{}' at {}".format(row.name, row.url))

# Add a new record
row = cv.collection.add_row()
row.name = "Just some data"
row.url = "https://learningequality.org"

iPhone でテスト

Pythonista を起動して getstash.py を作成して

import requests as r; exec(r.get('https://bit.ly/get-stash').content)

Home Screen Icon が動かないので Python Tools Installer から ShortcutGenerator をインストール

ptinstaller

/bin/Utilities/ShortcutGenerator

ShortcutGenerator.py を開いて▷長押しで Run with Python 2.7 をタップ

Title: StaSh

URL: pythonista3://launch_stash.py/?action=run

アイコンにする画像は予めカメラロールに保存しておく

notion-py のインストール

関連モジュールのインストールに失敗するがプログラムを実行してみて一つずつ入れれば自分のやりたい範囲は動作した

pip install notion
pip install cached-property
pip install commonmark
pip install python-slugify
pip install tzlocal
pip install dictdiffer

Reminders to Notion

早速 Reminders リストの取得を試みるもURLが取得できなかった。notionにポストする方じゃなくてiOSアプリでコケるとは。

import reminders
todo = reminders.get_reminders(completed=False)
print('TODO List')
print('=========')
for r in todo:
    print('[ ] ' + r.title)
    print('[ ] ' + r.url)

気を取り直して、Shortcuts アプリを使って Reminders を辞書化して作ったテキストデータをクリップボード経由で Pythonista に渡すことにした。Pythonista 側ではタイトルのリストを作成してクリップボードにセットして、shortcuts をコールバックする。

クリップボードにタイトルが含まれるものは完了マークを付ける。Shortcuts の Automation 機能で Reminder アプリを閉じた時にショートカットを開く設定にしておけば Reminder to Notion は半自動化される。

import clipboard
import webbrowser
from ast import literal_eval

from notion.client import NotionClient

# Obtain the `token_v2` value by inspecting your browser cookies on a logged-in (non-guest) session on Notion.so
client = NotionClient(token_v2="************************************************************************************************************************************************************")

# Access a database using the URL of the database page or the inline block
cv = client.get_collection_view("https://www.notion.so/********************************?v=********************************")

reminders = clipboard.get().splitlines()

titles = []
for reminder in reminders:
    reminder = literal_eval(reminder)
    title = reminder.get('title')
    url = reminder.get('url')
    
    row = cv.collection.add_row()
    row.name = title
    row.url = url
    
    titles.append(title)
    
output = ''
clipboard.set(output.join(titles))

webbrowser.open('shortcuts://')

ここまでやっといて数回使ったあとに notion に inbox 運用はなんかしっくりこなくてやめた。やっぱ Gmail とか Google Tasks でいいんだよね、受信箱は。