Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // post_history.db to kakikomi.txt
- const iconv = require("iconv-lite")
- const fs = require('fs')
- let filename = 'post_history.db'
- const KAKIKOMI_FORMAT = `--------------------------------------------
- Date : %{date}%
- Subject: %{title}%
- URL : %{url}%
- FROM : %{name}%
- MAIL : %{mail}%
- %{body}%
- `
- let buf = [];
- (async () => {
- const data = await fs.promises.readFile(filename, "utf8")
- for (let line of data.split(/\n/)) {
- try {
- let j = JSON.parse(line)
- if (!j.timestamp) throw 'invalid date'
- if (j.type === 'myself') {
- let txt = KAKIKOMI_FORMAT
- let date = new Date(j.timestamp)
- let dateStr = new Intl.DateTimeFormat('jp', {
- year: '2-digit', month: '2-digit', day: '2-digit',
- hour: '2-digit',
- minute: '2-digit',
- second: '2-digit'
- }).format(date)
- txt = txt.replace(/%\{date\}%/, dateStr)
- txt = txt.replace(/%\{title\}%/, j.title)
- txt = txt.replace(/%\{url\}%/, j.location)
- txt = txt.replace(/%\{name\}%/, j.mname ?? '')
- txt = txt.replace(/%\{mail\}%/, j.mail ?? '')
- txt = txt.replace(/%\{body\}%/, j.body)
- txt = txt.replace(/\n/g, '\x0d\x0a')
- buf.push([j.timestamp, txt])
- }
- } catch (e) {
- console.log(e)
- console.log(line)
- }
- }
- if (buf.length) {
- await fs.promises.writeFile('kakikomi.txt', iconv.encode(buf.sort((a, b) => a[0] > b[0] ? 1 : -1).map(x => x[1]).join(`\x0d\x0a`), 'cp932'), 'binary')
- }
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement