Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const low = require('lowdb')
  3.  
  4. const FileSync = require('lowdb/adapters/FileSync')
  5. const db = low(new FileSync('db.json'))
  6. const analysisDB = low(new FileSync('analysis.json'))
  7.  
  8. const Entities = require('html-entities').Html5Entities
  9. const entities = new Entities()
  10.  
  11. const main = async () => {
  12.     console.log("loading...")
  13.     const chanData = db.getState()
  14.     console.log("DONE")
  15.  
  16.     const analysis = {}
  17.  
  18.     for(let board in chanData){
  19.         analysis[board] = {
  20.             threads: 0,
  21.             replies: 0,
  22.             images: 0,
  23.             characters: 0,
  24.             totalThreadAgeSeconds: 0,
  25.             uniqueIPs: 0, // not 'actually' unique IPs, just the sum of the unique posters per thread
  26.      
  27.             avgRepliesPerMinutePerThread: 0,
  28.             avgPostLength: 0,
  29.             imageRatio: 0,
  30.             avgThreadAgeHours: 0,
  31.             avgPostersPerThread: 0,
  32.             avgPostsByPoster: 0, // per thread
  33.         }
  34.         for(let threadNum in chanData[board]){
  35.             const thread = chanData[board][threadNum]
  36.             const OP = thread.posts[0]
  37.             if(OP.closed || OP.sticky || thread.posts.length == 1) continue // remove
  38.             const threadAge = thread.posts[thread.posts.length - 1].time - OP.time
  39.             /*
  40.             if(OP.sticky){
  41.                 threadAge = thread.posts[thread.posts.length - 1].time - thread.posts[1].time
  42.       }
  43.       */
  44.      
  45.             analysis[board].threads++
  46.             analysis[board].replies += OP.replies
  47.             analysis[board].images += OP.images
  48.             for(let post of thread.posts){
  49.                 if(!post.com) post.com = ""
  50.                 if(board == "p") post.com = post.com.replace(/<span class="abbr">.+<\/table>/gms,"") //removes EXIF text from /p/ posts
  51.                 post.com = post.com.replace(/<a.*?<\/a>|<br>/gm," ") //replace post-links and linebreaks with a space
  52.                 post.com = post.com.replace(/<.*?>/gm,"") //remove any other HTML tags; greentext-HTML, /g/ [code], etc., but its text-content is kept
  53.                 post.com = entities.decode(post.com) //convert html entities to actual characters: "&gt;" becomes ">"
  54.                 post.com = post.com.trim() //remove whitespace from start and end
  55.                 analysis[board].characters += post.com.length
  56.             }
  57.             analysis[board].totalThreadAgeSeconds += threadAge
  58.             analysis[board].uniqueIPs += OP.unique_ips
  59.         }
  60.         analysis[board].avgRepliesPerMinutePerThread = analysis[board].replies / (analysis[board].totalThreadAgeSeconds / 60)
  61.         analysis[board].avgPostLength = analysis[board].characters / analysis[board].replies
  62.         analysis[board].imageRatio = analysis[board].images / analysis[board].replies
  63.         analysis[board].avgThreadAgeHours = analysis[board].totalThreadAgeSeconds / 3600 / analysis[board].threads
  64.         analysis[board].avgPostersPerThread = analysis[board].uniqueIPs / analysis[board].threads
  65.         analysis[board].avgPostsByPoster = (analysis[board].replies + analysis[board].threads) / analysis[board].uniqueIPs
  66.    
  67.  
  68.         console.log(`/${board}/`)
  69.         console.log(analysis[board])
  70.     }
  71.     analysisDB.setState(analysis)
  72.     analysisDB.write()
  73. }
  74.  
  75. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement