diff --git a/db/database.db b/db/database.db new file mode 100644 index 0000000..d4f427b Binary files /dev/null and b/db/database.db differ diff --git a/index.js b/index.js index 53e7680..89e5e28 100644 --- a/index.js +++ b/index.js @@ -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.listen(3050, "0.0.0.0"); \ No newline at end of file +app.use(videoInfo.routes()).use(videoInfo.allowedMethods()) +app.listen(3050, "0.0.0.0"); +console.log("link: http://127.0.0.1:3050") \ No newline at end of file diff --git a/package.json b/package.json index be6993e..2982dca 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0338c4a..d623100 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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: diff --git a/router/index.js b/router/index.js index 5de8a43..116cca2 100644 --- a/router/index.js +++ b/router/index.js @@ -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 } diff --git a/router/videoInfo.js b/router/videoInfo.js index d9bb649..c4328b1 100644 --- a/router/videoInfo.js +++ b/router/videoInfo.js @@ -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 diff --git a/util/sql/base.js b/util/sql/base.js index 6c6bf0b..d056e40 100644 --- a/util/sql/base.js +++ b/util/sql/base.js @@ -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 } \ No newline at end of file diff --git a/util/sql/download.js b/util/sql/download.js index af2393b..3a24b67 100644 --- a/util/sql/download.js +++ b/util/sql/download.js @@ -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 } \ No newline at end of file diff --git a/util/sql/init.js b/util/sql/init.js index 66a9da7..73966e7 100644 --- a/util/sql/init.js +++ b/util/sql/init.js @@ -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) } +function init(){ + let isexists = fs.existsSync(path.join(__dirname,"/db/database.db")) + if(!isexists){ + initTable() + } + +} module.exports = { - initTable + init } \ No newline at end of file