2023-06-14 15:43:04 +08:00
|
|
|
const Router = require("koa-router")
|
2023-06-30 20:49:07 +08:00
|
|
|
const {getQqListData} = require("../util/getList/qq")
|
2023-06-30 22:56:45 +08:00
|
|
|
const utils = require("../util/utils")
|
2023-06-14 15:43:04 +08:00
|
|
|
const KoaSSEStream = require('koa-sse-stream');
|
2023-06-30 20:49:07 +08:00
|
|
|
const {dowload} = require("../util/download");
|
|
|
|
const {addDownList, getDownList, setDownState} = require("../util/sql/download");
|
2023-06-28 21:11:21 +08:00
|
|
|
const dayjs = require("dayjs")
|
2023-06-14 15:43:04 +08:00
|
|
|
const router = new Router()
|
2023-06-28 21:11:21 +08:00
|
|
|
// const dowloadlist = []
|
2023-06-14 15:43:04 +08:00
|
|
|
|
2023-06-30 20:49:07 +08:00
|
|
|
router.get("/test", async (ctx) => {
|
|
|
|
let url = ctx.query.url
|
2023-07-01 21:01:05 +08:00
|
|
|
await utils.createTorrent(url,'C:\\Users\\SummerTail\\Downloads\\Video\\斗破苍穹.年番.Fights.Break.Sphere.S05E06.2022.2160p.WEB-DL.H265.AAC-ADWeb.torrent').then(value => {
|
2023-06-30 22:56:45 +08:00
|
|
|
ctx.body = {
|
|
|
|
code: 0,
|
|
|
|
msg: "Test",
|
|
|
|
data: value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-30 20:49:07 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
router.get("/getQqList", async (ctx) => {
|
2023-06-14 15:43:04 +08:00
|
|
|
let url = ctx.query.url
|
2023-06-29 22:00:07 +08:00
|
|
|
let videoid = url.split("/")
|
2023-06-30 22:56:45 +08:00
|
|
|
for (let i in videoid) {
|
|
|
|
if (videoid[i] == "cover") {
|
|
|
|
videoid = videoid[i + 1].replace(".html")
|
2023-06-29 22:00:07 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 15:43:04 +08:00
|
|
|
let list = await getQqListData(url)
|
|
|
|
ctx.body = list
|
|
|
|
})
|
|
|
|
|
2023-06-30 20:49:07 +08:00
|
|
|
router.get("/startDown", async (ctx) => {
|
2023-06-14 15:43:04 +08:00
|
|
|
let title = ctx.query.title
|
|
|
|
let url = ctx.query.url
|
2023-06-14 22:02:50 +08:00
|
|
|
let save = ctx.query.save
|
2023-06-28 21:11:21 +08:00
|
|
|
// dowloadlist.push({
|
|
|
|
// title,url,save
|
|
|
|
// })
|
2023-06-14 15:43:04 +08:00
|
|
|
// startDown()
|
2023-06-30 20:49:07 +08:00
|
|
|
addDownList(title, url, save, new Date().getTime(), 0)
|
|
|
|
ctx.body = {
|
|
|
|
code: 0,
|
|
|
|
msg: "添加成功"
|
2023-06-14 15:43:04 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// 连接池
|
|
|
|
const clientList = [];
|
|
|
|
// koa-sse-stream 配置
|
|
|
|
const SSE_CONF = {
|
|
|
|
maxClients: 5, // 最大连接数
|
|
|
|
pingInterval: 40000 // 重连时间
|
|
|
|
}
|
|
|
|
router.get('/msg', KoaSSEStream(SSE_CONF), ctx => {
|
|
|
|
// 每次连接会进行一个 push
|
|
|
|
clientList.push(ctx.sse);
|
2023-06-30 20:49:07 +08:00
|
|
|
if (clientList.length > 5) {
|
2023-06-28 21:11:21 +08:00
|
|
|
// 超过5个删除最开始的一个
|
|
|
|
// todo 浏览器关闭后自动检测删除
|
2023-06-15 10:47:25 +08:00
|
|
|
clientList.shift()
|
|
|
|
}
|
2023-06-14 15:43:04 +08:00
|
|
|
})
|
|
|
|
let start = false
|
2023-06-30 20:49:07 +08:00
|
|
|
|
|
|
|
function startDown(data) {
|
2023-06-14 22:02:50 +08:00
|
|
|
start = true
|
2023-06-30 20:49:07 +08:00
|
|
|
dowload(data, async (data2, isok) => {
|
2023-06-28 21:11:21 +08:00
|
|
|
let name = data2.title
|
2023-06-30 20:49:07 +08:00
|
|
|
clientList.forEach((i) => {
|
|
|
|
i.send(isok ? `${name}下载完成` : data2)
|
2023-06-14 15:43:04 +08:00
|
|
|
})
|
2023-06-30 20:49:07 +08:00
|
|
|
if (isok) {
|
2023-06-28 21:11:21 +08:00
|
|
|
// dowloadlist.shift()
|
2023-06-30 20:49:07 +08:00
|
|
|
await setDownState(data.id, 1)
|
2023-06-15 10:45:32 +08:00
|
|
|
start = false
|
|
|
|
}
|
2023-06-14 15:43:04 +08:00
|
|
|
})
|
|
|
|
}
|
2023-06-30 20:49:07 +08:00
|
|
|
|
|
|
|
setInterval(async () => {
|
2023-06-28 21:11:21 +08:00
|
|
|
let dowloadlist = await getDownList()
|
2023-06-30 20:49:07 +08:00
|
|
|
if (dowloadlist.length != 0 && !start) {
|
2023-06-28 21:11:21 +08:00
|
|
|
startDown(dowloadlist[0])
|
2023-06-30 20:49:07 +08:00
|
|
|
} else if (dowloadlist.length == 0) {
|
2023-06-14 15:43:04 +08:00
|
|
|
start = false
|
|
|
|
}
|
2023-06-30 20:49:07 +08:00
|
|
|
}, 1000)
|
2023-06-14 15:43:04 +08:00
|
|
|
module.exports = router
|