Guest User

Untitled

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