Browse Source

Merge branch 'ncm' of xie/instrunet_backend into master

xie 4 months ago
parent
commit
58abbc0221
6 changed files with 100 additions and 42 deletions
  1. BIN
      .DS_Store
  2. BIN
      .idea/.DS_Store
  3. 0 7
      .idea/dataSources.xml
  4. 95 34
      main.js
  5. 1 0
      package-lock.json
  6. 4 1
      package.json

BIN
.DS_Store


BIN
.idea/.DS_Store


+ 0 - 7
.idea/dataSources.xml

@@ -1,13 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
-    <data-source source="LOCAL" name="database" uuid="737a284f-b67d-4faf-aaa3-ea1b7a3f4aaf">
-      <driver-ref>sqlite.xerial</driver-ref>
-      <synchronize>true</synchronize>
-      <jdbc-driver>org.sqlite.JDBC</jdbc-driver>
-      <jdbc-url>jdbc:sqlite:$PROJECT_DIR$/database.db</jdbc-url>
-      <working-dir>$ProjectFileDir$</working-dir>
-    </data-source>
     <data-source source="LOCAL" name="@8.141.5.12" uuid="d06398b7-49f6-46ef-acd8-28d6def335d4">
       <driver-ref>mysql.8</driver-ref>
       <synchronize>true</synchronize>

+ 95 - 34
main.js

@@ -1,11 +1,10 @@
+let argv = require('minimist')(process.argv.slice(2));
 const cron = require("node-cron");
 const fs = require('fs');
 const express = require('express');
 const app = express();
-const app_serve = express();
 const mysql = require('mysql2')
 const SendSeekable = require('send-seekable');
-const WebSocket = require('ws');
 const pool = mysql.createPool({
     keepAliveInitialDelay: 10000, enableKeepAlive: true,
 })
@@ -17,16 +16,15 @@ const nodemailer = require('nodemailer')
 const bodyParser = require("body-parser");
 const nrc = require('node-run-cmd')
 const https = require("node:https");
-const {WebSocketServer} = require("ws");
-const http = require("node:http");
 const {OpenCC} = require("opencc");
 const converter = new OpenCC('s2t.json')
 app.use(bodyParser.json({"limit": "200mb"}));
 app.use(express.json());
-
-app_serve.use(bodyParser.json({"limit": "200mb"}));
-app_serve.use(express.json());
-app_serve.use(SendSeekable)
+app.use(SendSeekable)
+let availCache = {}
+cron.schedule('* */12 * * *', () => {
+    availCache = {}
+})
 
 const transporter = nodemailer.createTransport({
     host: 'smtp.qq.com', port: 465, secure: true, auth: {
@@ -34,31 +32,48 @@ const transporter = nodemailer.createTransport({
     }
 })
 
+/// TODO
+let ncmAPIUrl = "http://localhost:5999";
 
-app.post('/submit', function (req, res) {
+
+function Submit(req, res) {
     let uuid = crypto.randomUUID()
     db.connect(function (err) {
         if (err) {
             console.log(err)
         }
     })
+    try {
+        res.header("Access-Control-Allow-Origin", "*");
 
-    res.header("Access-Control-Allow-Origin", "*");
+    } catch (e) {
+        console.log(e)
+    }
 
     let albumCover = null;
     try {
         fetch(req.body.file).then(res => {
             res.arrayBuffer().then(r => fs.writeFileSync("./" + uuid, Buffer.from(r)));
         })
-        fetch(req.body.albumCover).then(res=>{
-            res.arrayBuffer().then(r=>{
+        fetch(req.body.albumCover).then(res => {
+            res.arrayBuffer().then(r => {
                 albumCover = r
             })
         })
 
-        res.end("api_success");
+        try {
+            res.end("api_success")
+        } catch (e) {
+            console.log(e)
+        }
+
     } catch (err) {
-        res.status(500).send("Server Error");
+        try {
+            res.status(500).send("Server Error");
+
+        } catch (e) {
+            console.log(e)
+        }
         return
     }
 
@@ -133,7 +148,7 @@ app.post('/submit', function (req, res) {
                 to: req.body.email,
                 subject: "你的音频已处理完成。",
                 text: "你的音频已处理完成。",
-                html: fs.readFileSync("./Template.html", "utf8").toString().replace("{song_name}", req.body.name).replace("{href_link}", "https://andyxie.cn:4000/player?play="+uuid)
+                html: fs.readFileSync("./Template.html", "utf8").toString().replace("{song_name}", req.body.name).replace("{href_link}", "https://andyxie.cn:4000/player?play=" + uuid)
             }).then((result) => {
                 console.log("Message sent: %s", result.messageId)
 
@@ -144,15 +159,17 @@ app.post('/submit', function (req, res) {
     })
 
 
-})
+}
+
+
+app.post('/submit', Submit)
+
 
 app.options('/submit', function (req, res) {
     res.header("Access-Control-Allow-Origin", "*");
     res.header("Access-Control-Allow-Headers", "Content-Type");
     res.end()
 })
-
-
 app.post('/search_api', async function (req, res) {
     async function OnFetched(err, rows) {
         try {
@@ -178,7 +195,6 @@ app.post('/search_api', async function (req, res) {
                    or artist like '%${await converter.convertPromise(req.body.searchStr)}%'`, OnFetched)
     db.unprepare()
 })
-
 app.options('/search_api', function (req, res) {
     res.header("Access-Control-Allow-Origin", "*");
     res.header("Access-Control-Allow-Headers", "Content-Type");
@@ -206,13 +222,60 @@ app.get("/getSingle", function (req, res) {
 
     }
 })
-var availCache = {}
-cron.schedule('* */12 * * *', () => {
-    availCache = {}
+
+// 163
+app.options('/ncm/url', function (req, res) {
+    res.header("Access-Control-Allow-Origin", "*");
+    res.header("Access-Control-Allow-Headers", "Content-Type");
+    res.end()
 })
-app_serve.get('/:uuid', function (req, res, next) {
+app.post("/ncm/url", function (req, res) {
+    if (req.body.id) {
+        let id = req.body.id
+        fetch(
+            ncmAPIUrl + "/song/download/url/v1?id=" + id + "&level=hires", {
+                headers: {
+                    Cookie: "MUSIC_U=0087F9D8E102A1C1661EBE1792412F3351DA64D1BD3D862BA77E45E9024524725F3A1983345D9B5A4014C725D19C069DD71081F6FE3659F9E1FD412DC427FB809FAF7789AEEA10E9DE6F06C58D1959BA209D2A83C3FA753261036C4CFD0D143B6C7748B8A6D2DD5C2E96E75D1E847E4AAE035CB2C86B175D9AFC6A164C522ED76E24AE654740AB6BAF5B29597F7E3B0158B2EC1C37F2688279871873FA7ADAEF8280A059E84C4BBFB9E4F225F9A2065DF652247D5496587A7B1E3D35DB0CD3F825C06FE5BFE5CFEF1770847099704360504B73C9B396E37CECE4F9DDEE6001588C3C4F5B2861D9ADF339FC47DD480858CA800620785EA032215B63B81025304DB3331F384793FF8EE681247E34C7931176F2F618B66C122F0602F1EA15F963E422DEC79C257F3577A197BECE71E316C751C3B9F5F3CD07BFDC0270A287A1BB6576"
+                }
+            }
+        ).then(async result => {
+            let result_json = await result.json()
+            if (result.status === 200) {
+                let infos = await (await fetch(ncmAPIUrl + "/song/detail?ids=" + id)).json();
+                req.body.file = "data:audio/flac;base64,"+Buffer.from(await (await fetch(result_json.data.url)).arrayBuffer()).toString("base64")
+                req.body.name = infos.songs[0].name;
+                req.body.albumName = infos.songs[0].al.name
+                req.body.albumCover = infos.songs[0].al.picUrl
+                req.body.link = result_json.data.url
+                req.body.artist = infos.songs[0].ar[0].name
+                /// Complete
+
+
+                Submit(req, res)
+
+
+
+            }
+
+        })
+
+    } else {
+        res.contentType("application/json");
+        res.header("Access-Control-Allow-Origin", "*");
+        res.header("Access-Control-Allow-Headers", "Content-Type");
+        res.end("{}")
+    }
+})
+
+app.get("/favicon.ico", function (req, res) {
+    res.end("");
+})
+
+// Fetch
+app.get('/:uuid', function (req, res) {
 
     let uuid = req.params.uuid;
+    console.log(uuid);
 
     function Provider(err, rows) {
         try {
@@ -255,17 +318,15 @@ app_serve.get('/:uuid', function (req, res, next) {
 
 })
 
-// TODO Always use HTTPS before publishing.
-
- https.createServer({
-     key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
-}, app).listen(8080)
-
 
- https.createServer({
-     key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
+if (argv.https === "true") {
+    https.createServer({
+        key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
+    }, app).listen(8080)
+    console.log("Listening on port 8080 with TLS")
+} else {
+    app.listen(8080)
+    console.log("Listening on port 8080")
+}
 
- }, app_serve).listen(8079)
 
-// app.listen(8080)
-// app_serve.listen(8079)

+ 1 - 0
package-lock.json

@@ -13,6 +13,7 @@
         "express": "^4.21.1",
         "html-entities": "^2.5.2",
         "https": "^1.0.0",
+        "minimist": "^1.2.8",
         "mysql2": "^3.11.5",
         "node-cron": "^3.0.3",
         "node-run-cmd": "^1.0.1",

+ 4 - 1
package.json

@@ -3,7 +3,9 @@
   "version": "1.0.0",
   "main": "main.js",
   "scripts": {
-    "test": "node main.js"
+    "test": "node main.js",
+    "start-https": "node main.js --https=true",
+    "start-http": "node main.js --https=false"
   },
   "keywords": [],
   "author": "",
@@ -14,6 +16,7 @@
     "express": "^4.21.1",
     "html-entities": "^2.5.2",
     "https": "^1.0.0",
+    "minimist": "^1.2.8",
     "mysql2": "^3.11.5",
     "node-cron": "^3.0.3",
     "node-run-cmd": "^1.0.1",