From 9d2ea2a4b0019a5c9eeee3266e931dc4ce7c18d1 Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Thu, 29 Aug 2024 16:03:59 -0700 Subject: [PATCH] Initial commit Signed-off-by: Daishan Peng --- src/index.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++++---- tool.gpt | 10 ++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tool.gpt diff --git a/src/index.ts b/src/index.ts index c8fb42f..cd31c1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,8 @@ import { Client } from "@notionhq/client"; import dotenv from "dotenv"; +import { writeFile, mkdir } from "fs/promises"; +import path from "path"; +import { SearchResponse } from "@notionhq/client/build/src/api-endpoints"; dotenv.config(); @@ -8,11 +11,53 @@ async function main() { auth: process.env.NOTION_TOKEN, }); - const response = await notion.databases.query({ - database_id: "FIXME", - }); + // Function to write a page to a file + async function writePageToFile(page: any, directory: string) { + const pageId = page.id.replace(/-/g, ''); + const filePath = path.join(directory, `${pageId}.data`); + await writeFile(filePath, JSON.stringify(page, null, 2)); + console.log(`Wrote page ${pageId} to ${filePath}`); + } + + // Function to fetch all pages + async function fetchAllPages() { + let pages: any[] = []; + let cursor: string | undefined = undefined; + + while (true) { + const response: SearchResponse = await notion.search({ + filter: { + property: "object", + value: "page", + }, + start_cursor: cursor, + }); + + pages = pages.concat(response.results); + + if (!response.has_more) { + break; + } + + cursor = response.next_cursor ?? undefined; + } + + return pages; + } + + // Fetch all pages + const pages = await fetchAllPages(); + + // Define the output directory + const outputDir = path.join(process.env.WORKSPACE_DIR!!, 'knowledge', 'integrations', 'notion'); + await mkdir(outputDir, { recursive: true }); // Ensure the directory exists + + // Write all pages to files + await Promise.all( + pages.map((page) => writePageToFile(page, outputDir)) + ); - console.log("Got response:", response); + console.log(`Finished writing ${pages.length} pages to ${outputDir}`); } main() diff --git a/tool.gpt b/tool.gpt new file mode 100644 index 0000000..592e110 --- /dev/null +++ b/tool.gpt @@ -0,0 +1,10 @@ +Name: Sync Notion Pages +Description: Provides access to the Notion API (read-only) +Credential: github.com/gptscript-ai/gateway-oauth2 as notion.read with NOTION_TOKEN as env and notion as integration +Context: syncPages + +--- +Name: syncPages +Description: Syncs Notion Pages + +#!/usr/bin/env npm run start