File size: 4,153 Bytes
abed4cc
e6227e8
40f1235
abed4cc
5d38af1
 
650a8e4
 
587da90
650a8e4
10fbdf7
5d38af1
587da90
c121514
 
 
 
5d38af1
c121514
650a8e4
 
 
 
 
 
 
2b60bab
650a8e4
587da90
5d38af1
650a8e4
587da90
 
 
40f1235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bdd039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c7c7b9
2bdd039
 
 
 
 
 
 
 
 
 
 
 
 
abed4cc
 
5d38af1
abed4cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require('express');
const axios = require('axios');

const app = express();
app.use(express.json());

app.post('/fetch-sheet', async (req, res) => {
  const { key, list, page, max } = req.body;

  if (!key || !list || !page || !max) {
    return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
  }

  // Экранирование значений для использования в URL
  const encodedKey = encodeURIComponent(key);
  const encodedList = encodeURIComponent(list);

  try {
    const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
    const response = await axios.get(url);
    const data = response.data;

    // Разделение данных на страницы
    const startIndex = (page - 1) * max;
    const endIndex = startIndex + max;
    const paginatedData = data.slice(startIndex, endIndex);

    res.json(paginatedData);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: '❌ Произошла ошибка сервера при запросе данных.' });
  }
});

app.post('/search', async (req, res) => {
  const { key, list, search, search_key, max, page } = req.body;

  if (!key || !list || !search || !search_key || !page || !max) {
    return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
  }

  const encodedKey = encodeURIComponent(key);
  const encodedList = encodeURIComponent(list);

  try {
    const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
    const response = await axios.get(url);
    let data = response.data;

    // Фильтрация данных по строке поиска
    data = data.filter(item => item[search_key] && item[search_key].toString().toLowerCase().includes(search.toLowerCase()));

    // Разделение данных на страницы
    const startIndex = (page - 1) * max;
    const endIndex = startIndex + max;
    const paginatedData = data.slice(startIndex, endIndex);

    res.json(paginatedData);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: '❌ Произошла ошибка сервера при поиске данных.' });
  }
});

app.post('/random', async (req, res) => {
  const { key, list, num } = req.body;

  if (!key || !list || !num) {
    return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
  }

  const encodedKey = encodeURIComponent(key);
  const encodedList = encodeURIComponent(list);

  try {
    const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
    const response = await axios.get(url);
    let data = response.data;

    // Получение случайных элементов из списка
    const shuffled = data.sort(() => 0.5 - Math.random());
    let selected = shuffled.slice(0, num);

    res.json(selected);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: '❌ Произошла ошибка сервера при получении случайных данных.' });
  }
});

app.post('/info', async (req, res) => {
  const { key, list, max } = req.body;

  if (!key || !list) {
    return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
  }

  const encodedKey = encodeURIComponent(key);
  const encodedList = encodeURIComponent(list);

  try {
    const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
    const response = await axios.get(url);
    const data = response.data;
    const maxPages = data.length;

    const info = { objects: maxPages };

    if (max) {
      const pages = Math.ceil(maxPages / max);
      info.pages = pages;
    }

    res.json(info);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: '❌ Произошла ошибка сервера при получении информации.' });
  }
});

const port = 7860;
app.listen(port, () => {
  console.log(`API сервер запущен на порту ${port}`);
});