Notion APIを利用して,Qiitaの最新記事を指定時間に取得する


はじめに

Qiitaで気になるタグの最新記事をNotionに追加するためのスクリプトを書きました.
crontabを使って指定時間にスクリプトを実行すれば,最新記事が自動的にNotionに追加されるようになります.

スクリプト

main.py
# coding: UTF-8
from notion.client import NotionClient
from notion.block import TodoBlock
import requests
import json
import datetime

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

# Replace this URL with the URL of the page you want to edit
page = client.get_block(<<block>>)

# Typescriptに関する記事を5つ取得しています
url = "https://qiita.com//api/v2/items?page=1&per_page=5&query=tag%3Atypescript"

response = requests.get(url)
jsonData = response.json()
today = datetime.date.today()

for jsonObj in jsonData:
  newchild = page.children.add_new(
      TodoBlock, title="【{0}】[{1}]({2})".format(today.strftime('%Y%m%d'), jsonObj["title"], jsonObj["url"]))

<<token_v2>> には,ログインしたページのクッキーの「token_v2」という項目の値を入力します.
<<block>>には,記事を追加したいpageのURLを入力します.

これを実行すると,以下のような感じで記事が追加されます.