forked from Project-And-Factory/BlurLyric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverRouter.js
161 lines (143 loc) · 4.29 KB
/
serverRouter.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const express = require('express');
const router = express.Router()
const axios = require('axios')
const user = require('./blurlyric/user');
const match = require('@unblockneteasemusic/server');
const fs = require("fs")
const path = require("path")
const fetch = require( 'node-fetch');
router.get('/createUser',(req,res)=>{
user.createUser((data)=>{
jsonTool('200',data,req,res)
return
})
})
router.get('/unblockmusic',async (req,res)=>{
//,['kugou','migu','kuwo']
// res.json({})
match(req.query.id,['kugou','kuwo','migu']).then(text =>{
// console.log(text);
res.json(text)
})
})
router.get('/getUser',(req,res)=>{
if (!req.query.id) {
jsonTool('200',{
"msg": '请填入ID',
},req,res)
return
}
user.getUser(req.query.id,(data)=>{
jsonTool(200,data,req,res)
})
})
router.get('/writeUser',(req,res)=>{
console.log(req.query)
if (!req.query.id||!req.query.res) {
jsonTool('405',null,req,res)
return
}
let dataSize = strSize(req.query.res.toString())
if(strSize(dataSize,'utf8')>20000000){
jsonTool('405',{msg: '文件过大 (>20mb)'},req,res)
return
}
user.upsetConfig(req.query,(data)=>{
jsonTool('200',data,req,res)
})
})
router.post('/writeUserPlaylist',(req,res)=>{
// console.log(req.query)
// if (!req.query.id||!req.query.res) {
// jsonTool('405',null,req,res)
// return
// }
// let dataSize = strSize(req.query.res.toString())
// if(strSize(dataSize,'utf8')>20000000){
// jsonTool('405',{msg: '文件过大 (>20mb)'},req,res)
// return
// }
console.log(req);
jsonTool('200',req,'',res)
// user.upsetPlaylist(req.query,(data)=>{
// jsonTool('200',data,req,res)
// })
})
function jsonTool(code,data,req,res) {
res.json({
"code": code || 200,
"date": new Date(),
"data": '' ||data,
"ip": '' ||req.ip
})
}
function strSize(str, charset) {
let total = 0;
charset = charset?.toLowerCase() || '';
for (i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
if (charset === 'utf-16' || charset === 'utf16') {
total += charCode <= 0xffff ? 2 : 4;
} else {
if (charCode <= 0x007f) {
total += 1;
} else if (charCode <= 0x07ff) {
total += 2;
} else if (charCode <= 0xffff) {
total += 3;
} else {
total += 4;
}
}
}
return total;
}
async function getFileByUrl(url,fileName){
console.log('['+(downloadTrack.now+1) + '/'+downloadTrack.list.length+']请求下载 ' + fileName)
await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/octet-stream' },
}).then(res=>res.buffer()).then(_=>{
fs.writeFile(path.join(__dirname,'./blurlyric/download/'+fileName ),_,'binary',function (err) {
downloadTrack.freeThread++
downloadTrack.try()
if (err) console.log('['+(downloadTrack.now+1) + '/'+downloadTrack.list.length+']下载失败 ' + fileName);
else console.log('['+(downloadTrack.now+1) + '/'+downloadTrack.list.length+']完成下载 ' + fileName)
})}).catch((error)=>{
downloadTrack.freeThread++
downloadTrack.try()
})
}
var downloadTrack={
list: [],
now: 0,
maxThread: 8,
freeThread: 8,
async try(){
if(this.list.length>this.now){
getFileByUrl(this.list[this.now].url,this.list[this.now].fileName)
this.freeThread--
this.now++
}
if(this.freeThread != 0&&this.list.length - this.now>1){
this.try()
}
}
}
router.get('/downloadUrl',(req,res)=>{
console.log(req.query)
if (!req.query.url||!req.query.fileName) {
jsonTool('405',null,req,res)
return
}
console.log({
url: req.query.url,
fileName: req.query.fileName
});
downloadTrack.list.push({
url: req.query.url,
fileName: req.query.fileName
})
downloadTrack.try()
})
module.exports = router