Advertisement
Guest User

posthistory2kakikomi

a guest
Jul 16th, 2023
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // post_history.db to kakikomi.txt
  2.  
  3. const iconv = require("iconv-lite")
  4. const fs = require('fs')
  5. let filename = 'post_history.db'
  6. const KAKIKOMI_FORMAT = `--------------------------------------------
  7. Date   : %{date}%
  8. Subject: %{title}%
  9. URL    : %{url}%
  10. FROM   : %{name}%
  11. MAIL   : %{mail}%
  12.  
  13. %{body}%
  14.  
  15.  
  16. `
  17. let buf = [];
  18.  
  19. (async () => {
  20.   const data = await fs.promises.readFile(filename, "utf8")
  21.   for (let line of data.split(/\n/)) {
  22.     try {
  23.       let j = JSON.parse(line)
  24.       if (!j.timestamp) throw 'invalid date'
  25.       if (j.type === 'myself') {
  26.         let txt = KAKIKOMI_FORMAT
  27.         let date = new Date(j.timestamp)
  28.         let dateStr = new Intl.DateTimeFormat('jp', {
  29.           year: '2-digit', month: '2-digit', day: '2-digit',
  30.           hour: '2-digit',
  31.           minute: '2-digit',
  32.           second: '2-digit'
  33.         }).format(date)
  34.         txt = txt.replace(/%\{date\}%/, dateStr)
  35.         txt = txt.replace(/%\{title\}%/, j.title)
  36.         txt = txt.replace(/%\{url\}%/, j.location)
  37.         txt = txt.replace(/%\{name\}%/, j.mname ?? '')
  38.         txt = txt.replace(/%\{mail\}%/, j.mail ?? '')
  39.         txt = txt.replace(/%\{body\}%/, j.body)
  40.         txt = txt.replace(/\n/g, '\x0d\x0a')
  41.         buf.push([j.timestamp, txt])
  42.       }
  43.     } catch (e) {
  44.       console.log(e)
  45.       console.log(line)
  46.     }
  47.   }
  48.   if (buf.length) {
  49.     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')
  50.   }
  51. })()
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement