Transcribes a video saved in a Notion Database, and saves the transcription to the page
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
#!/usr/bin/env zx | |
const body = await fetch( | |
`https://api.notion.com/v1/databases/${process.env.NOTION_DATABASE_ID}/query`, | |
{ | |
method: "POST", | |
headers: { | |
Authorization: `Bearer ${process.env.NOTION_API_KEY}`, | |
"Notion-Version": "2022-06-28", | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({}), | |
} | |
).then((r) => r.json()); | |
const data = body.results | |
.map((result) => { | |
const { id, properties } = result; | |
const videoUrl = properties["Video URL"].url; | |
const fileName = videoUrl.split("/").reverse()[0].split("?")[0]; | |
return { id, videoUrl, fileName }; | |
}) | |
.slice(11, 12); // only do a few at a time | |
// get output of video | |
for await (let { videoUrl, fileName, id } of data) { | |
await $`curl -L ${videoUrl} --output ${fileName}`; | |
const r = await $`whisper ${fileName} --language en`; | |
const resp = await fetch(`https://api.notion.com/v1/blocks/${id}/children`, { | |
method: "PATCH", | |
headers: { | |
Authorization: `Bearer ${process.env.NOTION_API_KEY}`, | |
"Content-Type": "application/json", | |
"Notion-Version": "2022-02-22", | |
}, | |
body: JSON.stringify({ | |
children: [ | |
{ | |
object: "block", | |
type: "heading_1", | |
heading_1: { | |
rich_text: [ | |
{ | |
type: "text", | |
text: { | |
content: "Transcription", | |
}, | |
annotations: {}, | |
}, | |
], | |
}, | |
}, | |
...r._stdout.split("\n").map((line) => ({ | |
object: "block", | |
type: "paragraph", | |
paragraph: { | |
rich_text: [ | |
{ | |
type: "text", | |
text: { | |
content: line, | |
}, | |
annotations: { | |
bold: false, | |
}, | |
}, | |
], | |
}, | |
})), | |
], | |
}), | |
}).then((resp) => resp.json()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run, install:
And then: