This commit is contained in:
Liam Chan 2023-04-12 01:44:01 +08:00
commit e3cfb58501
3 changed files with 73 additions and 0 deletions

6
Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM node:18-alpine
WORKDIR /usr/src/app
ENV NODE_ENV=production
COPY . .
EXPOSE 8888
CMD [ "node", "index.js" ]

54
index.js Normal file
View File

@ -0,0 +1,54 @@
import http from "http";
const unicodeToChar = (text) => {
return text.replace(/\\u[\dA-F]{4}/gi, function (match) {
return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16));
});
};
http
.createServer(async (request, response) => {
const responseBody = {
message: "ok",
data: {},
};
let statusCode = 200;
let html;
try {
const res = await fetch("https://live.kankanews.com/live/8029037XEw6");
html = await res.text();
} catch (error) {
response.message = "fetch failed";
statusCode = 500;
}
const pattern = /play_url:"(.*?)"/g;
let match = [];
let urls = [];
while ((match = pattern.exec(html)) !== null) {
if (match.length > 0) {
const playUrl = match[1];
const decodedUrl = unicodeToChar(playUrl);
urls.push(decodedUrl);
}
}
if (urls.length > 0) {
responseBody.data = {
urls,
};
} else {
responseBody.message = "no urls";
statusCode = 404;
}
response.writeHead(statusCode, { "Content-Type": "application/json" });
response.end(JSON.stringify(responseBody));
})
.listen(8888);
console.log("Server running at http://127.0.0.1:8888/");

13
package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "live-shanghai",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}