commit e3cfb585010b3db97fae543553ffd8b2dfbc45e1 Author: imbytecat Date: Wed Apr 12 01:44:01 2023 +0800 init diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..28a8571 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM node:18-alpine +WORKDIR /usr/src/app +ENV NODE_ENV=production +COPY . . +EXPOSE 8888 +CMD [ "node", "index.js" ] diff --git a/index.js b/index.js new file mode 100644 index 0000000..44648e2 --- /dev/null +++ b/index.js @@ -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/"); diff --git a/package.json b/package.json new file mode 100644 index 0000000..2de18ec --- /dev/null +++ b/package.json @@ -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" +} \ No newline at end of file