104 lines
2.6 KiB
JavaScript
104 lines
2.6 KiB
JavaScript
const Router = require("koa-router")
|
|
const {getQqListData} = require("../util/getList/qq")
|
|
const utils = require("../util/utils")
|
|
const KoaSSEStream = require('koa-sse-stream');
|
|
const {dowload} = require("../util/download");
|
|
const {addDownList, getDownList, setDownState} = require("../util/sql/download");
|
|
const {getMediaInfo} = require("../util/utils");
|
|
const router = new Router()
|
|
// const download = []
|
|
|
|
router.get("/getMediaInfo", async (ctx) => {
|
|
let url = ctx.query.filePath
|
|
let data
|
|
await getMediaInfo(url).then(value => {
|
|
data = value;
|
|
})
|
|
let verify = (data.media === null)
|
|
console.info(verify)
|
|
ctx.body = {
|
|
code: verify ? -1 : 0,
|
|
msg: verify ? "读取文件失败" : "",
|
|
data: data
|
|
}
|
|
})
|
|
|
|
router.get("/getQqList", async (ctx) => {
|
|
let url = ctx.query.url
|
|
let video_id = url.split("/")
|
|
for (let i in video_id) {
|
|
if (video_id[i] === "cover") {
|
|
video_id = video_id[i + 1].replace(".html")
|
|
break
|
|
}
|
|
}
|
|
ctx.body = await getQqListData(url)
|
|
})
|
|
|
|
router.get("/startDown", async (ctx) => {
|
|
let title = ctx.query.title
|
|
let url = ctx.query.url
|
|
let save = ctx.query.save
|
|
// download.push({
|
|
// title,url,save
|
|
// })
|
|
// startDown()
|
|
addDownList(title, url, save, new Date().getTime(), 0)
|
|
ctx.body = {
|
|
code: 0,
|
|
msg: "添加成功"
|
|
}
|
|
})
|
|
|
|
router.get("/test", async (ctx) => {
|
|
let rep = await utils.getTencentVideoPlayList('mzc00200t7i1qwp')
|
|
ctx.body = {
|
|
code: 0,
|
|
msg: "test",
|
|
data: rep
|
|
}
|
|
})
|
|
|
|
|
|
// 连接池
|
|
const clientList = [];
|
|
// koa-sse-stream 配置
|
|
const SSE_CONF = {
|
|
maxClients: 5, // 最大连接数
|
|
pingInterval: 40000 // 重连时间
|
|
}
|
|
router.get('/msg', KoaSSEStream(SSE_CONF), ctx => {
|
|
// 每次连接会进行一个 push
|
|
clientList.push(ctx.sse);
|
|
if (clientList.length > 5) {
|
|
// 超过5个删除最开始的一个
|
|
// todo 浏览器关闭后自动检测删除
|
|
clientList.shift()
|
|
}
|
|
})
|
|
let start = false
|
|
|
|
function startDown(data) {
|
|
start = true
|
|
dowload(data, async (data2, isok) => {
|
|
let name = data2.title
|
|
clientList.forEach((i) => {
|
|
i.send(isok ? `${name}下载完成` : data2)
|
|
})
|
|
if (isok) {
|
|
// dowloadlist.shift()
|
|
await setDownState(data.id, 1)
|
|
start = false
|
|
}
|
|
})
|
|
}
|
|
|
|
setInterval(async () => {
|
|
let dowloadlist = await getDownList()
|
|
if (dowloadlist.length !== 0 && !start) {
|
|
startDown(dowloadlist[0])
|
|
} else if (dowloadlist.length === 0) {
|
|
start = false
|
|
}
|
|
}, 1000)
|
|
module.exports = router |