Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2.  
  3. /* contents of .nvmrc
  4. 18.13.0
  5. */
  6.  
  7. /* contents of package.json:
  8. {
  9.   "dependencies": {
  10.     "sqlite3": "^5.0.8"
  11.   }
  12. }
  13. */
  14.  
  15. // API doc https://gitgud.io/InfinityNow/LynxChan/-/blob/master/doc/Json.txt
  16.  
  17. const https = require('https')
  18. const sqlite3 = require('sqlite3')
  19.  
  20. // Board to be archived, can be overriden by passing an argument `node app.js ausneets`
  21. const defaultBoard = 'polru'
  22. var board = defaultBoard
  23. const arg = process.argv.slice(2)[0]
  24. if (arg) {
  25.   board = arg
  26. }
  27.  
  28. const db = new sqlite3.Database(`endchan-${board}.db`)
  29.  
  30. async function getJson(url) {
  31.   return new Promise((resolve) => {
  32.       let data = ''
  33.  
  34.       https.get(url, res => {
  35.           res.on('data', chunk => { data += chunk })
  36.           res.on('end', () => {
  37.             resolve(JSON.parse(data))
  38.           })
  39.       })
  40.   })
  41. }
  42.  
  43. async function dbGet(query, args){
  44.   return new Promise(function(resolve, reject) {
  45.       db.get(query, args, function(err, row) {
  46.          if (err) { return reject(err) }
  47.          resolve(row)
  48.        })
  49.   })
  50. }
  51.  
  52. async function archiveThread(thread) {
  53.   const threadId = thread['threadId']
  54.   const threadJson = await getJson(`https://endchan.gg/${board}/res/${threadId}.json`)
  55.   var posts = threadJson.posts
  56.   posts.unshift(threadJson)
  57.  
  58.   for (var postIndex in posts) {
  59.     const post = posts[postIndex]
  60.     const files = post['files'].map((arr) => {
  61.       return arr.originalName
  62.     })
  63.     .join(',')
  64.  
  65.     db.run(`
  66.     INSERT OR REPLACE INTO posts
  67.     (threadId, postId, message, id, flagCode, creation, name, email, subject, files)
  68.     VALUES
  69.     (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  70.     `,
  71.       [
  72.         threadId,
  73.         postIndex == 0 ? threadId: post['postId'],
  74.         post['message'],
  75.         post['id'],
  76.         post['flagCode'],
  77.         post['creation'],
  78.         post['name'],
  79.         post['email'],
  80.         post['subject'],
  81.         files
  82.       ])
  83.   }
  84.  
  85.   return threadJson
  86. }
  87.  
  88. async function start() {
  89.   console.log('...fetching threads list...')
  90.   const threads = await getJson(`https://endchan.gg/${board}/catalog.json`)
  91.   threads.sort((a, b) => a.lastBump > b.lastBump)
  92.  
  93.   for (var threadIndex in threads) {
  94.     const thread = threads[threadIndex]
  95.     console.log(`Updating thread with id: ${thread.threadId}, lastBump: ${thread.lastBump}`)
  96.  
  97.     const oldLastBumpRow = await dbGet(`
  98.     SELECT lastBump FROM threads
  99.     WHERE threadId = ?
  100.     `,
  101.     [thread['threadId']])
  102.  
  103.     if (oldLastBumpRow && oldLastBumpRow.lastBump === thread['lastBump']) {
  104.       console.log('Up to date, finishing...')
  105.       break
  106.     }
  107.    
  108.     let opPost = await archiveThread(thread)
  109.  
  110.     db.run(`
  111.     INSERT OR REPLACE INTO threads
  112.     (threadId, lastBump)
  113.     VALUES
  114.     (?, ?)
  115.     `,
  116.       [
  117.         thread['threadId'],
  118.         thread['lastBump']
  119.       ])
  120.   }
  121. }
  122.  
  123. db.serialize(() => {
  124.   db.run(`
  125.     CREATE TABLE IF NOT EXISTS "threads" (
  126.         "threadId"  INTEGER,
  127.         "lastBump" DATE,
  128.         PRIMARY KEY("threadId")
  129.     );
  130.     `)
  131.  
  132.   db.run(`
  133.     CREATE TABLE IF NOT EXISTS "posts" (
  134.         "threadId" TEXT,
  135.         "postId"    INTEGER,
  136.         "message" TEXT,
  137.         "id" TEXT,
  138.         "flagCode" TEXT,
  139.         "creation" DATE,
  140.         "name" TEXT,
  141.         "email" TEXT,
  142.         "subject" TEXT,
  143.         "files" TEXT,
  144.         PRIMARY KEY("postId")
  145.     );
  146.     `)
  147.  
  148.   start()
  149. })
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement