Advertisement
kenpusney

crawler.js

Nov 2nd, 2022 (edited)
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const [_, __, prefix, token] = process.argv;
  2.  
  3. import fetch from "node-fetch"
  4.  
  5. import fs from "fs/promises"
  6.  
  7. async function save(entry) {
  8.     console.log(entry.title)
  9.     const response = await fetch(`${prefix}/read/${entry.value}`, {
  10.         headers: { token }
  11.     })
  12.  
  13.     const article = await response.json()
  14.  
  15.     const { subject, content, url } = article.data
  16.  
  17.     await fs.writeFile(`articles/${url}.md`, `#${subject}\n\n ${content}`)
  18. }
  19.  
  20. async function crawler() {
  21.     const response = await fetch(`${prefix}/menu`, {
  22.         headers: {
  23.             token
  24.         }
  25.     })
  26.  
  27.     const menu = await response.json()
  28.  
  29.     let entries = menu.data
  30.  
  31.     while(entries.length > 0) {
  32.         let entry = entries.shift()
  33.         if (entry.value) {
  34.             await save(entry);
  35.         }
  36.         entries = entries.concat(entry.children)
  37.     }
  38.  
  39.     console.log(JSON.stringify(menu))
  40. }
  41.  
  42. crawler();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement