98 lines
2.6 KiB
JavaScript
Executable file
98 lines
2.6 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
|
|
import path from "path"
|
|
import fs from "fs"
|
|
|
|
// DAT_ROOT should exists in ~/.zshrc-local
|
|
const indexPath = path.join(process.env["NOTES_DIR"], "")
|
|
const processFile = path.join(process.cwd(), process.argv[2])
|
|
|
|
|
|
const data = fs.readFileSync(processFile, 'utf8')
|
|
const jsonData = JSON.parse(data)
|
|
|
|
function wantSave(str) {
|
|
return typeof str == "string" && str.split("\n").length > 10
|
|
}
|
|
|
|
function saveToFileSync(content, file) {
|
|
const dir = path.dirname(file);
|
|
try {
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
}
|
|
|
|
fs.writeFileSync(file, content, 'utf8')
|
|
} catch (err) {
|
|
console.error('Error saving file:', err)
|
|
}
|
|
}
|
|
|
|
const toSave = jsonData.flatMap(o => {
|
|
const date = new Date(o.create_time * 1000)
|
|
|
|
// const formattedDate = date.toLocaleDateString('en-GB', {
|
|
// day: '2-digit',
|
|
// month: '2-digit',
|
|
// year: '2-digit',
|
|
// })
|
|
// return formattedDate
|
|
|
|
const day = String(date.getDate()).padStart(2, '0')
|
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
const year = String(date.getFullYear()).slice(-2)
|
|
|
|
let baseNotePath = path.join(indexPath, "gpt", `${day}-${month}-${year}`, `${o.title}`)
|
|
|
|
let conversations = []
|
|
for (const [key, value] of Object.entries(o.mapping)) {
|
|
//console.log(key, value)
|
|
|
|
conversations.push({
|
|
path: path.join(baseNotePath, `${key}.json`),
|
|
content: JSON.stringify(value, null, 2)
|
|
})
|
|
|
|
let parts = value?.message?.content.parts
|
|
if (Array.isArray(parts)) {
|
|
|
|
if (parts.length == 0) {
|
|
// skip
|
|
} else if (parts.length == 1) {
|
|
let fileName = path.join(baseNotePath, `${key}.md`)
|
|
let md = parts[0]
|
|
|
|
if (wantSave(md)) {
|
|
conversations.push({
|
|
path: fileName,
|
|
content: md
|
|
})
|
|
}
|
|
} else {
|
|
|
|
for (const [key, value] of parts.entries()) {
|
|
let fileName = path.join(baseNotePath, `${key}-${key+1}.md`)
|
|
let md = parts[key]
|
|
|
|
if (wantSave(md)) {
|
|
conversations.push({
|
|
path: fileName,
|
|
content: md
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//process.exit(-1)
|
|
|
|
}
|
|
|
|
return conversations
|
|
})
|
|
|
|
|
|
for(const f of toSave) {
|
|
//for(const f of toSave.slice(0,5)) {
|
|
saveToFileSync(f.content,f.path)
|
|
}
|