133 lines
5.0 KiB
JavaScript
133 lines
5.0 KiB
JavaScript
const http = require("http");
|
|
const url = require("url");
|
|
const { ServerModel } = require('./model/ServerModel');
|
|
const { SearchModel } = require('./model/SearchModel');
|
|
const { storeMusic, storeLrc, storePic, checkMusicExists } = require('./model/StoreModel');
|
|
const { SuccessResult, ErrorResult } = require('./model/ResultModel');
|
|
|
|
const { QQSource } = require('./source/QQSource');
|
|
const mfm = require('./source/MyFreemp3Source');
|
|
const rm123 = require('./source/RM123Source');
|
|
const { KugouSource } = require('./source/KugouSource');
|
|
const { ATYYSource } = require('./source/ATYYSource');
|
|
const { rm } = require("fs");
|
|
|
|
// 防止进程中断
|
|
// process.on('uncaughtException', function (err) {
|
|
// console.log('Caught exception: ' + err);
|
|
// });
|
|
// ***********************************************************************
|
|
// 注册源
|
|
const searcher = new SearchModel();
|
|
//searcher.registerSourceModel(new QQSource());
|
|
searcher.registerSourceModel(new mfm.MyFreemp3QQSource());
|
|
searcher.registerSourceModel(new mfm.MyFreemp3NeteaseSource());
|
|
//searcher.registerSourceModel(new mfm.MyFreemp3KugouSource()); 不能使用
|
|
searcher.registerSourceModel(new mfm.MyFreemp3KuwoSource());
|
|
//searcher.registerSourceModel(new mfm.MyFreemp3MiguSource());不能使用
|
|
searcher.registerSourceModel(new mfm.MyFreemp3QianqianSource());
|
|
searcher.registerSourceModel(new mfm.MyFreemp3QingtingSource());
|
|
//searcher.registerSourceModel(new mfm.MyFreemp3XimalayaSource());不能使用
|
|
searcher.registerSourceModel(new mfm.MyFreemp3LizhiSource());
|
|
//searcher.registerSourceModel(new mfm.MyFreemp3KgSource());不能使用
|
|
searcher.registerSourceModel(new mfm.MyFreemp31tingSource());
|
|
//searcher.registerSourceModel(new mfm.MyFreemp35singycSource());不能使用
|
|
//searcher.registerSourceModel(new mfm.MyFreemp35singfcSource());不能使用
|
|
//searcher.registerSourceModel(new KugouSource()); 不能使用
|
|
searcher.registerSourceModel(new rm123.RM123MiguSource());
|
|
// searcher.registerSourceModel(new rm123.RM123XimalayaSource());不能使用
|
|
searcher.registerSourceModel(new ATYYSource());
|
|
|
|
|
|
// ***********************************************************************
|
|
// 添加API接口
|
|
const _server = new ServerModel();
|
|
|
|
_server.get("/api/source/all", (req, res, query) => {
|
|
// 罗列都有哪些源
|
|
return new SuccessResult(searcher.getSourceList());
|
|
});
|
|
|
|
_server.get("/api/search", async (req, res, query) => {
|
|
//从源去进行搜索
|
|
let result = await searcher.searchMusic(query.source_name, query.keywords);
|
|
return new SuccessResult(result);
|
|
});
|
|
|
|
_server.get("/api/store/check", async (req, res, query) => {
|
|
let is_exists = checkMusicExists(query.music_path);
|
|
return new SuccessResult(is_exists);
|
|
});
|
|
|
|
_server.get("/api/store", async (req, res, query) => {
|
|
//存储文件
|
|
let music_path = query.music_path;
|
|
let music_url = query.music_url;
|
|
let lrc_content = query.lrc_content;
|
|
let pic_url = query.pic_url;
|
|
let title = query.title;
|
|
let author = query.author;
|
|
let extension = query.extension;
|
|
let store_music = parseInt(query.store_music);
|
|
let store_lrc = parseInt(query.store_lrc);
|
|
let store_pic = parseInt(query.store_pic);
|
|
let path_name = music_path.split('.').slice(0,-1).join('.');
|
|
let pic_extension = pic_url.split('?')[0].split('.').pop();
|
|
let result = '';
|
|
if(store_music) {
|
|
await storeMusic(music_path, music_url, {title, author, extension});
|
|
result += '保存歌曲成功! ';
|
|
}
|
|
if(store_lrc) {
|
|
await storeLrc(path_name + '.lrc', lrc_content);
|
|
result += '保存歌词成功! ';
|
|
}
|
|
if(store_pic) {
|
|
await storePic(path_name + '.' + pic_extension, pic_url);
|
|
result += '保存封面成功! ';
|
|
}
|
|
return new SuccessResult(result);
|
|
});
|
|
|
|
|
|
// ***********************************************************************
|
|
// 运行服务
|
|
const args = process.argv.splice(2);
|
|
let port = 5750;
|
|
if(args.length > 0 && !isNaN(args[0])) {
|
|
port = parseInt(args[0]);
|
|
}
|
|
const server = http.createServer(async (req, res) => {
|
|
let method = req.method;
|
|
let reqUrl = req.url;
|
|
let reqApi = reqUrl.split('?')[0];
|
|
console.log(method, reqUrl);
|
|
|
|
let result;
|
|
try {
|
|
if(method == "GET") {
|
|
let query = url.parse(reqUrl, true).query;
|
|
result = await _server.trigger(method, reqApi, req, res, query, null);
|
|
}else if(method == "POST") {
|
|
let query = url.parse(reqUrl, true).query;
|
|
let data = {};
|
|
result = await _server.trigger(method, reqApi, req, res, query, data);
|
|
}
|
|
}catch(err) {
|
|
console.error(err);
|
|
result = new ErrorResult(err);
|
|
}
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
//res.setHeader('Access-Control-Allow-Methods', '*');
|
|
res.setHeader('Access-Control-Allow-Headers', '*');
|
|
res.setHeader('Content-type', 'application/json;charset=utf-8');
|
|
//res.writeHead(200, { 'Content-type': 'application/json;charset=utf-8' });
|
|
res.end(JSON.stringify(result));
|
|
});
|
|
|
|
server.listen(port, () => {
|
|
console.log(`server running at prot ${port}`);
|
|
});
|
|
|