-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add Javascript importer to pull ship data from Notion as JSON and write to disk. - Add manually-triggered Github Action to run this script and open/update a pull request with the changes.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Import ship JSON files from Notion | ||
|
||
name: Import Ship JSON files from Notion | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
ship_name: | ||
description: The specific ship to update (leave blank for all ships) | ||
default: '' | ||
required: false | ||
type: string | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: write | ||
|
||
jobs: | ||
import: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
# Setup the runtime environment | ||
- uses: actions/checkout@v4 | ||
- name: Setup dependencies | ||
working-directory: scripts/notion-import | ||
run: | | ||
npm install | ||
# Import the data from Notion | ||
- name: Import ship data | ||
working-directory: scripts/notion-import | ||
env: | ||
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }} | ||
run: | | ||
npm run import ${{ inputs.ship_name }} | ||
# | ||
- name: Create Pull Request | ||
uses: peter-evans/create-pull-request@v7 | ||
with: | ||
add-paths: data/ships/*.json | ||
branch: action/update-ship-json | ||
commit-message: "auto-commit: import ship json files" | ||
# Auto-generated pull request details | ||
title: Update Ship JSON Files | ||
body: | | ||
Manually-triggered import of Ship JSON files from [Notion][1]. | ||
Auto-generated by the [import-ships Github Action][2]. | ||
[1]: https://bszlrd.notion.site/Pioneer-redesign-dashboard-b0ffc505ff3c4302b6ee873d75319c75 | ||
[2]: https://github.com/pioneerspacesim/pioneer/actions/workflows/import-ships.yml | ||
reviewers: bszlrd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright © 2008-2025 Pioneer Developers. See AUTHORS.txt for details | ||
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt | ||
|
||
import { Client } from "@notionhq/client" | ||
import path from "node:path" | ||
import fs from "node:fs/promises" | ||
|
||
const notion = new Client({ auth: process.env.NOTION_TOKEN }) | ||
|
||
const pageId = "1018c82e283f80cdab99f35ef6e34211" | ||
|
||
// Developer toggle to test script | ||
const doExport = true | ||
// Log DB schema for manual run | ||
// const db = await notion.databases.retrieve({ database_id: pageId }) | ||
// console.log(db) | ||
|
||
let start_cursor = undefined | ||
|
||
const dataPath = path.resolve(path.join(import.meta.dirname, '../../data/ships')) | ||
|
||
console.log(`Writing ship JSON data files to path ${dataPath}`) | ||
|
||
const name_to_filter = process.argv[2] | ||
|
||
let filter_export = { | ||
property: "i%3Au%3E", | ||
checkbox: { equals: true } | ||
} | ||
|
||
let filter_name = { | ||
property: "title", | ||
rich_text: { | ||
equals: name_to_filter | ||
} | ||
} | ||
|
||
while (true) { | ||
let { results, next_cursor, has_more } = await notion.databases.query({ | ||
database_id: pageId, | ||
start_cursor: start_cursor, | ||
filter: name_to_filter ? { "and": [ filter_export, filter_name ] } : filter_export, | ||
filter_properties: [ 'title', 'mSL%3A' /* model */, '_R%5DR' /* JSON (Copy) */ ] | ||
}) | ||
|
||
for (const result of results) { | ||
let name = result.properties['Name'].title[0].plain_text.toLowerCase() | ||
// Replace spaces and parentheses with underscores, trim trailing underscores | ||
name = name.replace(/[ \(\)]+/g, "_").replace(/_+$/, "") | ||
|
||
// FIXME: to avoid breaking savefile compatibility, we currently use the model name instead of ship name to construct the output filename | ||
let customName = result.properties['model'].rich_text[0] | ||
if (customName !== undefined) { | ||
name = customName.plain_text | ||
} else { | ||
console.log(`${name} is missing model field`) | ||
} | ||
|
||
const filePath = path.join(dataPath, name + '.json') | ||
const json = result.properties['JSON (Copy)'].formula.string + '\n' | ||
|
||
try { | ||
await fs.access(filePath) | ||
console.log(`Importing ship file ${filePath}`) | ||
} catch { | ||
console.log(`Ship file ${filePath} not found - first import or incorrectly-named ship?`) | ||
} | ||
|
||
if (doExport) | ||
await fs.writeFile(filePath, json) | ||
} | ||
|
||
start_cursor = next_cursor | ||
|
||
if (!has_more) | ||
break; | ||
} | ||
|
||
process.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "notion-import", | ||
"version": "0.1.0", | ||
"description": "Imports Pioneer ship data from Notion.so", | ||
"main": "notion-import.mjs", | ||
"scripts": { | ||
"import": "node notion-import.mjs" | ||
}, | ||
"author": "Pioneer Space Sim", | ||
"license": "GPL-3.0", | ||
"dependencies": { | ||
"@notionhq/client": "^2.2.15" | ||
} | ||
} |