Guest User

Untitled

a guest
Dec 4th, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BoardDescription {
  2.     constructor(
  3.         board,
  4.         title,
  5.         subtitle,
  6.         image_limit
  7.     ) {
  8.         this.board = board; // i.e. 'rf'
  9.         this.title = title; // i.e. 'Убежище'
  10.         this.subtitle = subtitle; // i.e. 'Доска с ОП-модерацией'
  11.         this.image_limit = image_limit; // i.e. 4
  12.     }
  13. }
  14.  
  15. class ThreadDescription {
  16.     constructor(
  17.         no,
  18.         sub,
  19.         com,
  20.         name
  21.     ) {
  22.         this.no = no; // i.e. 55977
  23.         this.title = sub; // Thread title
  24.         this.message = com; // Thread OP message
  25.         this.op_name = name; // i.e. 'Доброанон'
  26.     }
  27. }
  28.  
  29. class VichanApiClient {
  30.     constructor(base_url) {
  31.         this.base_url = base_url; // i.e. 'https://rf.dobrochan.net/vichan'
  32.     }
  33.  
  34.     boards() {
  35.         return fetch(`${this.base_url}/boards.json`)
  36.             .then((response) => response.json())
  37.             .then((data) => {
  38.                 const result = [];
  39.                 for (const board of data) {
  40.                     result.push(
  41.                         new BoardDescription(
  42.                             board.board,
  43.                             board.title,
  44.                             board.subtitle,
  45.                             board.image_limit
  46.                         )
  47.                     )
  48.                 }
  49.                 return result;
  50.             })
  51.     }
  52.  
  53.     page(board, page = 0) {
  54.         return fetch(`${this.base_url}/${board}/${page}.json`)
  55.             .then((response) => response.json())
  56.             .then((data) => {
  57.                 const result = [];
  58.                 for (const thread of data.threads) {
  59.                     result.push(
  60.                         new ThreadDescription(
  61.                             thread.posts[0].no,
  62.                             thread.posts[0].sub,
  63.                             thread.posts[0].com,
  64.                             thread.posts[0].name,
  65.                         )
  66.                     )
  67.                 }
  68.                 return result;
  69.             })
  70.     }
  71. }
  72.  
  73. class DobrochanConsolePrinter {
  74.     print(title, data) {
  75.         console.log(title);
  76.         for (const item of data) {
  77.             for (const field in item) {
  78.                 console.log(`${field}: ${item[field]}`);
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. class DobrochanClient {
  85.     constructor(
  86.         base_url = 'https://rf.dobrochan.net/vichan'
  87.     ) {
  88.         this.api_client = new VichanApiClient(base_url);
  89.         this.boards = this.api_client.boards();
  90.     }
  91.  
  92.     get_boards() {
  93.         return this.boards;
  94.     }
  95.  
  96.     get_first_page(board) {
  97.         return this.api_client.page(board, 0);
  98.     }
  99. }
  100.  
  101. async function main() {
  102.     const printer = new DobrochanConsolePrinter();
  103.     const dc = new DobrochanClient();
  104.     printer.print(`Доски`, await dc.get_boards());
  105.     printer.print(`Нулевая /b`, await dc.get_first_page('b'));
  106. }
  107.  
  108. main();
Advertisement
Add Comment
Please, Sign In to add comment