add sqlite 保存下载历史

This commit is contained in:
theluyuan 2023-06-28 21:11:21 +08:00
parent 95f09a96b8
commit b439920702
9 changed files with 109 additions and 30 deletions

BIN
db/database.db Normal file

Binary file not shown.

View File

@ -4,6 +4,7 @@ const static = require('koa-static')
app.use(static("./view/dist"))
const { koaBody } = require('koa-body');
const KoaSSEStream = require('koa-sse-stream');
app.use(koaBody());
app.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*');
@ -16,10 +17,10 @@ app.use(async (ctx, next) => {
}
});
const index = require("./router/index.js")
app.use(index.routes()).use(index.allowedMethods())
const videoInfo = require("./router/videoInfo.js")
app.use(videoInfo.routes()).use(videoInfo.allowedMethods())
const videoInfo = require("./router/videoInfo.js");
app.use(videoInfo.routes()).use(videoInfo.allowedMethods())
app.listen(3050, "0.0.0.0");
console.log("link: http://127.0.0.1:3050")

View File

@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"axios": "^1.4.0",
"dayjs": "^1.11.8",
"koa": "^2.14.2",
"koa-body": "^6.0.1",
"koa-router": "^12.0.0",

View File

@ -4,6 +4,9 @@ dependencies:
axios:
specifier: ^1.4.0
version: 1.4.0
dayjs:
specifier: ^1.11.8
version: 1.11.8
koa:
specifier: ^2.14.2
version: 2.14.2
@ -618,6 +621,10 @@ packages:
engines: {node: '>= 14'}
dev: false
/dayjs@1.11.8:
resolution: {integrity: sha512-LcgxzFoWMEPO7ggRv1Y2N31hUf2R0Vj7fuy/m+Bg1K8rr+KAs1AEy4y9jd5DXe8pbHgX+srkHNS7TH6Q6ZhYeQ==}
dev: false
/debug@3.2.7:
resolution: {integrity: sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==}
peerDependencies:

View File

@ -2,9 +2,10 @@ const Router = require("koa-router")
const { getQqListData } = require("../util/getList/qq")
const KoaSSEStream = require('koa-sse-stream');
const { dowload } = require("../util/download");
const { addDownList, getDownList, setDownState } = require("../util/sql/download");
const dayjs = require("dayjs")
const router = new Router()
const dowloadlist = []
// const dowloadlist = []
router.get("/getQqList",async (ctx)=>{
let url = ctx.query.url
@ -16,10 +17,11 @@ router.get("/startDown",async (ctx)=>{
let title = ctx.query.title
let url = ctx.query.url
let save = ctx.query.save
dowloadlist.push({
title,url,save
})
// dowloadlist.push({
// title,url,save
// })
// startDown()
addDownList(title,url,save,new Date().getTime(),0)
ctx.body={
code:0,
msg:"添加成功"
@ -36,27 +38,31 @@ 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(){
function startDown(data){
start = true
dowload(dowloadlist[0],(data,isok)=>{
let name = dowloadlist[0].title
dowload(data,async (data2,isok)=>{
let name = data2.title
clientList.forEach((i)=>{
i.send(isok ? `${name}下载完成` : data )
i.send(isok ? `${name}下载完成` : data2 )
})
if(isok){
dowloadlist.shift()
// dowloadlist.shift()
await setDownState(data.id,1)
start = false
}
})
}
setInterval(()=>{
setInterval(async ()=>{
let dowloadlist = await getDownList()
console.log(dowloadlist)
if(dowloadlist.length != 0 && !start){
startDown()
startDown(dowloadlist[0])
}else if(dowloadlist.length == 0){
start = false
}

View File

@ -13,7 +13,7 @@ router.get("/search",async (ctx)=>{
ctx.body = info
})
route.get("/getinfo",async (ctx)=>{
router.get("/getinfo",async (ctx)=>{
let url = ctx.query.url
let info = await getinfo(url)
ctx.body = info

View File

@ -1,15 +1,39 @@
const sqlite = require("sqlite3")
const path = require("path")
database = new sqlite.Database(path.join(__dirname,"/db"), function(e) {
const path = require("path");
const { cwd } = require("process");
let p = path.join(cwd(),"/db/database.db")
database = new sqlite.Database(p, function(err) {
if (err) throw err;
});
function run(){
database.run(...arguments)
let args = arguments
console.log(args)
return new Promise((res,err)=>{
database.run(...args,(error)=>{
if(error){
err(error)
}else{
res("添加成功")
}
})
})
}
function
function getAll(sql){
let args = arguments
return new Promise((res,err)=>{
database.all(...args,function (error,data){
if(error){
err(error)
}else{
res(data)
}
})
})
}
module.exports = {
run
run,
getAll
}

View File

@ -1,6 +1,28 @@
const { run } = require("./base");
const { run, getAll } = require("./base");
function addDownList(title,url,save){
let sql = `insert into download(title,url,save) value(?,?,?)`
run(sql,title,url,save)
function addDownList(title,url,save,downDate,isOk){
let sql = `insert into download(title,url,save,downDate,isOk) values(?,?,?,?,?)`
run(sql,title,url,save,downDate,isOk)
}
async function getDownList(){
let sql = `select * from download where isOk == 0`
try{
let list = await getAll(sql)
return list
}catch(err){
console.error(err)
return []
}
}
async function setDownState(id,state){
let sql = `update download set isOk = ? where id = ?`
await run(sql,state,id)
}
module.exports = {
addDownList,
getDownList,
setDownState
}

View File

@ -1,16 +1,34 @@
const { run } = require("./base");
const fs = require("fs");
const path = require("path")
function initTable(){
// 创建下载列表
/**
* id
* title 剧集标题
* url 剧集链接
* save 保存地址
* downDate 添加下载日期 时间戳
* isOk 是否下载完成 0 未完成 1 完成
*/
let sql = `create table download (
id INT PRIMARY KEY NOT NULL autoincrement,
title VARCHAR(255) not null,
url varchar(255) not null,
save varcahr(255) not null
save varcahr(255) not null,
downDate INTEGER not null,
isOk INTEGER not null
)`
run(sql)
}
module.exports = {
initTable
function init(){
let isexists = fs.existsSync(path.join(__dirname,"/db/database.db"))
if(!isexists){
initTable()
}
}
module.exports = {
init
}