-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (111 loc) · 3.84 KB
/
Copy pathindex.js
File metadata and controls
131 lines (111 loc) · 3.84 KB
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
125
126
127
128
129
130
131
/**
* ____ __________
* / __ \_ _____ _____/ __/ / __ \_ __
* / / / / | / / _ \/ ___/ /_/ / / / / | /| / /
* / /_/ /| |/ / __/ / / __/ / /_/ /| |/ |/ /
* \____/ |___/\___/_/ /_/ /_/\____/ |__/|__/
*
* The copyright indication and this authorization indication shall be
* recorded in all copies or in important parts of the Software.
*
* @author 0verfl0w767
* @link https://github.com/0verfl0w767
* @license MIT LICENSE
*
*/
const express = require("express");
const http = require("http");
const fs = require("fs");
const app = express();
const cheerio = require("cheerio");
const HTMLParser = require("node-html-parser");
const { JSDOM } = require("jsdom");
const seatFloor = (roomNo) => {
let floor = "";
if (roomNo == 6) floor = "1층 열람실";
else if (roomNo == 7) floor = "2층 열람실";
else if (roomNo == 8) floor = "2층 열람실";
else if (roomNo == 9) floor = "2층 채움실";
else if (roomNo == 10) floor = "3층 열람실";
else if (roomNo == 11) floor = "3층 열람실";
else if (roomNo == 12) floor = "3층 집중실";
return floor;
};
app.use("/", express.static(__dirname + "/public"));
app.get("/", (req, res) => {
res.status(200).sendFile(__dirname + "/page/api.html");
});
app.get("/test", (req, res) => {
res.status(200).sendFile(__dirname + "/page/test.html");
});
app.get("/study", (req, res) => {
res.status(200).sendFile(__dirname + "/page/study.html");
});
/*app.get('/api', (req, res) => {
res.status(200).sendFile(__dirname + '/page/api.html')
})*/
app.get("/api/:roomNo", async (req, res) => {
const url = `https://libmo.syu.ac.kr/mobile/PA/seatMap.php?roomNo=${req.params.roomNo}`;
const response = await fetch(url);
const html = await response.text();
res.status(200).send(html);
});
app.get("/apiTest/:roomNo", async (req, res) => {
const url = `https://libmo.syu.ac.kr/mobile/PA/seatMap.php?roomNo=${req.params.roomNo}`;
const response = await fetch(url);
const html = await response.text();
const $ = cheerio.load(html);
let ary = [];
let ids = [];
$("script").each((index, element) => {
const scriptContent = $(element).html();
const matches = scriptContent.match(/document\.getElementById\('(\d+)'\)/g);
if (matches) {
matches.forEach((match) => {
const id = match.match(/'(\d+)'/)[1];
ids.push(id);
});
}
});
$("tr").each((index, tr) => {
$(tr)
.find("td")
.each((index, td) => {
const seatId = $(td).attr("id");
const seatClass = $(td).attr("class");
if (!seatId || !seatClass) return;
ary.push({
seatId: seatId,
seatStatus: ids.includes(seatId) ? "사용 불가능" : "사용 가능",
seatFloor: seatFloor(req.params.roomNo),
});
});
});
res.status(200).json(ary.sort((a, b) => a.seatId - b.seatId));
});
/*app.get('/apiTest/:roomNo', async (req, res) => {
const url = `https://libmo.syu.ac.kr/mobile/PA/seatMap.php?roomNo=${req.params.roomNo}`;
const response = await fetch(url);
const html = await response.text();
const dom = new JSDOM(html);
const document = dom.window.document;
console.log(document.documentElement.outerHTML);
let ary = [];
document.querySelectorAll('tr').forEach(tr => {
tr.querySelectorAll('td').forEach(td => {
const seatId = td.getAttribute('id');
const seatClass = td.getAttribute('class');
if (!seatId || !seatClass) return;
ary.push({
'seatId': seatId,
'seatStatus': seatClass === 'desk' ? '사용 가능' : '사용 불가능',
'seatFloor': seatFloor(req.params.roomNo),
});
});
});
res.status(200).json(ary.sort((a, b) => a.seatId - b.seatId));
})*/
app.get("*", (req, res) => {
res.status(404).json({ statusCode: 404, message: "unknown request." });
});
http.createServer(app).listen(4848, "0.0.0.0");