This repository has been archived on 2023-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
live-shanghai/index.js
Liam Chan 50aa3b66cf
All checks were successful
continuous-integration/drone/push Build is passing
only use useful cameras
2023-04-17 14:59:12 +08:00

61 lines
1.4 KiB
JavaScript

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];
if (playUrl.match(/sdi4|sdi5|sdi6/)) {
const decodedUrl = unicodeToChar(playUrl);
urls.push(decodedUrl);
}
}
}
if (urls.length > 0) {
responseBody.data = {
urls,
};
} else {
responseBody.message = "no urls";
statusCode = 404;
}
response.writeHead(statusCode, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS, POST, GET",
"Access-Control-Max-Age": 2592000, // 30 days
"Content-Type": "application/json",
});
response.end(JSON.stringify(responseBody));
})
.listen(8888);
console.log("Server running at http://127.0.0.1:8888/");