Rooni commited on
Commit
550ce38
·
verified ·
1 Parent(s): 6c4d40b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +30 -42
server.js CHANGED
@@ -1,55 +1,43 @@
1
  const express = require('express');
2
- const axios = require('axios');
3
- const cheerio = require('cheerio');
4
 
5
  const app = express();
6
  app.use(express.json());
7
 
8
- const parsePage = async (url) => {
9
- const response = await axios.get(url);
10
- const html = response.data;
11
- const $ = cheerio.load(html);
12
- const parsedResults = [];
13
-
14
- $('.b-content__inline_item').each((index, element) => {
15
- const titleElement = $(element).find('.b-content__inline_item-link a');
16
- const title = titleElement.text().trim();
17
- const pageUrl = titleElement.attr('href');
18
- const imageUrl = $(element).find('.b-content__inline_item-cover a img').attr('src');
19
-
20
- parsedResults.push({
21
- title: title,
22
- imageUrl: imageUrl,
23
- pageUrl: pageUrl,
24
- });
25
- });
26
-
27
- return parsedResults;
28
- };
29
-
30
- app.get('/parse', async (req, res) => {
31
  try {
32
- const baseUrl = 'https://hdrezka180maa.org/animation';
33
- let currentPage = 1;
34
- let hasMorePages = true;
35
- const allResults = [];
36
-
37
- while (hasMorePages) {
38
- const pageUrl = currentPage === 1 ? baseUrl : `${baseUrl}/page/${currentPage}/`;
39
- const results = await parsePage(pageUrl);
40
-
41
- if (results.length === 0) {
42
- hasMorePages = false;
43
- } else {
44
- allResults.push(...results);
45
- currentPage++;
46
- }
47
  }
 
 
 
 
 
 
 
 
48
 
49
- res.json(allResults);
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  } catch (error) {
51
  console.error(error);
52
- res.status(500).json({ error: 'Произошла ошибка сервера при парсинге.' });
53
  }
54
  });
55
 
 
1
  const express = require('express');
2
+ const xml2js = require('xml2js');
3
+ const js2xmlparser = require('js2xmlparser');
4
 
5
  const app = express();
6
  app.use(express.json());
7
 
8
+ // Функция преобразования JSON в XML
9
+ app.post('/to_xml', (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  try {
11
+ const jsonInput = req.body.input;
12
+ if (!jsonInput) {
13
+ return res.status(400).json({ error: 'Отсутствует параметр input в теле запроса.' });
 
 
 
 
 
 
 
 
 
 
 
 
14
  }
15
+ const xmlOutput = js2xmlparser.parse("root", jsonInput);
16
+ res.type('application/xml');
17
+ res.send(xmlOutput);
18
+ } catch (error) {
19
+ console.error(error);
20
+ res.status(500).json({ error: 'Произошла ошибка сервера при преобразовании JSON в XML.' });
21
+ }
22
+ });
23
 
24
+ // Функция преобразования XML в JSON
25
+ app.post('/to_json', (req, res) => {
26
+ try {
27
+ const xmlInput = req.body.input;
28
+ if (!xmlInput) {
29
+ return res.status(400).json({ error: 'Отсутствует параметр input в теле запроса.' });
30
+ }
31
+ const parser = new xml2js.Parser({ explicitArray: false });
32
+ parser.parseString(xmlInput, (err, result) => {
33
+ if (err) {
34
+ throw err;
35
+ }
36
+ res.json(result);
37
+ });
38
  } catch (error) {
39
  console.error(error);
40
+ res.status(500).json({ error: 'Произошла ошибка сервера при преобразовании XML в JSON.' });
41
  }
42
  });
43