Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BoardDescription {
- constructor(
- board,
- title,
- subtitle,
- image_limit
- ) {
- this.board = board; // i.e. 'rf'
- this.title = title; // i.e. 'Убежище'
- this.subtitle = subtitle; // i.e. 'Доска с ОП-модерацией'
- this.image_limit = image_limit; // i.e. 4
- }
- }
- class ThreadDescription {
- constructor(
- no,
- sub,
- com,
- name
- ) {
- this.no = no; // i.e. 55977
- this.title = sub; // Thread title
- this.message = com; // Thread OP message
- this.op_name = name; // i.e. 'Доброанон'
- }
- }
- class VichanApiClient {
- constructor(base_url) {
- this.base_url = base_url; // i.e. 'https://rf.dobrochan.net/vichan'
- }
- boards() {
- return fetch(`${this.base_url}/boards.json`)
- .then((response) => response.json())
- .then((data) => {
- const result = [];
- for (const board of data) {
- result.push(
- new BoardDescription(
- board.board,
- board.title,
- board.subtitle,
- board.image_limit
- )
- )
- }
- return result;
- })
- }
- page(board, page = 0) {
- return fetch(`${this.base_url}/${board}/${page}.json`)
- .then((response) => response.json())
- .then((data) => {
- const result = [];
- for (const thread of data.threads) {
- result.push(
- new ThreadDescription(
- thread.posts[0].no,
- thread.posts[0].sub,
- thread.posts[0].com,
- thread.posts[0].name,
- )
- )
- }
- return result;
- })
- }
- }
- class DobrochanConsolePrinter {
- print(title, data) {
- console.log(title);
- for (const item of data) {
- for (const field in item) {
- console.log(`${field}: ${item[field]}`);
- }
- }
- }
- }
- class DobrochanClient {
- constructor(
- base_url = 'https://rf.dobrochan.net/vichan'
- ) {
- this.api_client = new VichanApiClient(base_url);
- this.boards = this.api_client.boards();
- }
- get_boards() {
- return this.boards;
- }
- get_first_page(board) {
- return this.api_client.page(board, 0);
- }
- }
- async function main() {
- const printer = new DobrochanConsolePrinter();
- const dc = new DobrochanClient();
- printer.print(`Доски`, await dc.get_boards());
- printer.print(`Нулевая /b`, await dc.get_first_page('b'));
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment