Rooni commited on
Commit
0cb0cc3
·
verified ·
1 Parent(s): 4c7c7b9

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +25 -109
server.js CHANGED
@@ -1,124 +1,40 @@
1
  const express = require('express');
2
  const axios = require('axios');
 
3
 
4
  const app = express();
5
  app.use(express.json());
6
 
7
- app.post('/fetch-sheet', async (req, res) => {
8
- const { key, list, page, max } = req.body;
9
-
10
- if (!key || !list || !page || !max) {
11
- return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
12
- }
13
-
14
- // Экранирование значений для использования в URL
15
- const encodedKey = encodeURIComponent(key);
16
- const encodedList = encodeURIComponent(list);
17
-
18
- try {
19
- const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
20
- const response = await axios.get(url);
21
- const data = response.data;
22
-
23
- // Разделение данных на страницы
24
- const startIndex = (page - 1) * max;
25
- const endIndex = startIndex + max;
26
- const paginatedData = data.slice(startIndex, endIndex);
27
-
28
- res.json(paginatedData);
29
- } catch (error) {
30
- console.error(error);
31
- res.status(500).json({ error: '❌ Произошла ошибка сервера при запросе данных.' });
32
- }
33
- });
34
-
35
- app.post('/search', async (req, res) => {
36
- const { key, list, search, search_key, max, page } = req.body;
37
-
38
- if (!key || !list || !search || !search_key || !page || !max) {
39
- return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
40
- }
41
-
42
- const encodedKey = encodeURIComponent(key);
43
- const encodedList = encodeURIComponent(list);
44
-
45
- try {
46
- const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
47
- const response = await axios.get(url);
48
- let data = response.data;
49
-
50
- // Фильтрация данных по строке поиска
51
- data = data.filter(item => item[search_key] && item[search_key].toString().toLowerCase().includes(search.toLowerCase()));
52
-
53
- // Разделение данных на страницы
54
- const startIndex = (page - 1) * max;
55
- const endIndex = startIndex + max;
56
- const paginatedData = data.slice(startIndex, endIndex);
57
-
58
- res.json(paginatedData);
59
- } catch (error) {
60
- console.error(error);
61
- res.status(500).json({ error: '❌ Произошла ошибка сервера при поиске данных.' });
62
- }
63
- });
64
-
65
- app.post('/random', async (req, res) => {
66
- const { key, list, num } = req.body;
67
-
68
- if (!key || !list || !num) {
69
- return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
70
- }
71
-
72
- const encodedKey = encodeURIComponent(key);
73
- const encodedList = encodeURIComponent(list);
74
-
75
- try {
76
- const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
77
- const response = await axios.get(url);
78
- let data = response.data;
79
-
80
- // Получение случайных элементов из списка
81
- const shuffled = data.sort(() => 0.5 - Math.random());
82
- let selected = shuffled.slice(0, num);
83
-
84
- res.json(selected);
85
- } catch (error) {
86
- console.error(error);
87
- res.status(500).json({ error: '❌ Произошла ошибка сервера при получении случайных данных.' });
88
- }
89
- });
90
-
91
- app.post('/info', async (req, res) => {
92
- const { key, list, max } = req.body;
93
-
94
- if (!key || !list) {
95
- return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
96
- }
97
-
98
- const encodedKey = encodeURIComponent(key);
99
- const encodedList = encodeURIComponent(list);
100
-
101
  try {
102
- const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
103
- const response = await axios.get(url);
104
- const data = response.data;
105
- const maxPages = data.length;
106
-
107
- const info = { objects: maxPages };
108
-
109
- if (max) {
110
- const pages = Math.ceil(maxPages / max);
111
- info.pages = pages;
112
- }
113
-
114
- res.json(info);
 
 
 
 
 
 
 
 
115
  } catch (error) {
116
  console.error(error);
117
- res.status(500).json({ error: 'Произошла ошибка сервера при получении информации.' });
118
  }
119
  });
120
 
121
  const port = 7860;
122
  app.listen(port, () => {
123
- console.log(`API сервер запущен на порту ${port}`);
124
  });
 
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
+ app.get('/parse', async (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  try {
10
+ // Запрос HTML-кода страницы
11
+ const response = await axios.get('https://hdrezka180maa.org/animation');
12
+ const html = response.data;
13
+ const $ = cheerio.load(html);
14
+ const parsedResults = [];
15
+
16
+ // Парсинг данных с использованием селекторов
17
+ $('.b-content__inline_item').each((index, element) => {
18
+ const title = $(element).find('.b-content__inline_item-link a').text().trim();
19
+ const imageUrl = $(element).find('.b-content__inline_item-cover a img').attr('src');
20
+ const pageUrl = $(element).find('.b-content__inline_item-link a').attr('href');
21
+
22
+ parsedResults.push({
23
+ title: title,
24
+ imageUrl: imageUrl,
25
+ pageUrl: pageUrl,
26
+ });
27
+ });
28
+
29
+ // Отправка результата
30
+ res.json(parsedResults);
31
  } catch (error) {
32
  console.error(error);
33
+ res.status(500).json({ error: 'Произошла ошибка сервера при парсинге.' });
34
  }
35
  });
36
 
37
  const port = 7860;
38
  app.listen(port, () => {
39
+ console.log(`Сервер запущен на порту ${port}`);
40
  });