Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Aug 29, 2024
1 parent 0ec240d commit 9d2ea2a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
53 changes: 49 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions tool.gpt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9d2ea2a

Please sign in to comment.