Notion書籍DB登録ショートカット

読書管理Notion DBをAndroidのHTTP Shortcutsアプリから操作するスクリプト。 タイトル入力 → Google Books APIで検索 → 候補選択 → ステータス選択 → Notion登録。

動作環境

  • アプリ: HTTP Shortcuts (Android)
  • ショートカット種別: スクリプトショートカット
  • 注意: showSelection はタイトル引数なし showSelection(items) で呼ぶ

スクリプト

var title = prompt("本のタイトルを入力");
 
var searchResult = sendHttpRequest(
  "https://www.googleapis.com/books/v1/volumes?q=" + encodeURIComponent(title) + "&langRestrict=ja&maxResults=5"
);
 
var books = JSON.parse(searchResult.response.body);
 
var choices = {};
for (var i = 0; i < books.items.length; i++) {
  var info = books.items[i].volumeInfo;
  choices[String(i)] = info.title + " / " + (info.authors ? info.authors[0] : "不明");
}
 
var selected = showSelection(choices);
var book = books.items[parseInt(selected)].volumeInfo;
 
var isbn13 = "";
if (book.industryIdentifiers) {
  for (var j = 0; j < book.industryIdentifiers.length; j++) {
    if (book.industryIdentifiers[j].type === "ISBN_13") isbn13 = book.industryIdentifiers[j].identifier;
  }
}
 
var status = showSelection({
  "ほしい": "ほしい",
  "積んでる": "積んでる",
  "読んでる": "読んでる",
  "読んだ": "読んだ"
});
 
var body = JSON.stringify({
  parent: { database_id: "3569dfa36b6680e19f72c3363d1fff27" },
  properties: {
    "タイトル": { title: [{ text: { content: book.title } }] },
    "著者": { rich_text: [{ text: { content: book.authors ? book.authors.join(", ") : "" } }] },
    "ISBN": { rich_text: [{ text: { content: isbn13 } }] },
    "出版社": { rich_text: [{ text: { content: book.publisher || "" } }] },
    "ステータス": { select: { name: status } },
    "登録日": { date: { start: new Date().toISOString().split("T")[0] } }
  }
});
 
var notionResult = sendHttpRequest("https://api.notion.com/v1/pages", {
  method: "POST",
  headers: {
    "Authorization": "Bearer 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",
    "Notion-Version": "2022-06-28",
    "Content-Type": "application/json"
  },
  body: body
});
 
if (notionResult.status === "success") {
  showToast("登録しました: " + book.title);
} else {
  showToast("失敗: " + notionResult.response.statusCode);
}

Notion DB情報

  • DB ID: 3569dfa36b6680e19f72c3363d1fff27
  • トークン: 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