ptSend/util/Download/index.js

100 lines
2.8 KiB
JavaScript

const path = require('path')
const {dow, fileIsExist,getMediaInfo} = require("../utils.js");
const { mkdirRecursions } = require('../markdir.js');
const schedule = require('node-schedule');
const { setDownState, getDownList } = require('../sql/download.js');
const { getVideoInfo } = require('../sql/video.js');
const fs = require("fs")
// 小于10添加0
function addZeor(num){
if(num < 10){
return "0" + num
}else{
return num
}
}
// 重命名
async function reName(dinfo){
let vinfo = await getVideoInfo(dinfo.vid)
console.log(vinfo)
if(vinfo){
let info = await getMediaInfo(dinfo.save + "/" + dinfo.title + ".mp4")
console.log(info,dinfo.save + "/" + dinfo.title + ".mp4")
info = info.media.track
let Video;
let Audio;
for(let i of info){
if(i["@type"] == "Video"){
Video = i
}
if(i["@type"] == "Audio"){
Audio = i
break
}
}
// 判断时长 0 为下载 1 下载完成 2 做种 3 没有下载完成或没有会员
if(Video.Duration < 4 * 60){
return 3
}
let fbl = 0
if(Video.Height > 3000){
fbl = 4320
}else if(Video.Height > 1440){
fbl = 2160
}else if(Video.Height > 1080){
fbl = 1440
}else if(Video.Height > 720){
fbl = 1080
}else if(Video.Height > 480){
fbl = 720
}else {
fbl = 480
}
let rname = `[${vinfo.name}].${vinfo.rename}.${vinfo.year}.S${addZeor(vinfo.season)}E${addZeor(dinfo.title)}.WEB-DL.${fbl}p.${Video.Format}.${Audio.Format}.mp4`
let state = fs.renameSync(dinfo.save + "/" + dinfo.title + ".mp4",dinfo.save + "/" + rname)
}else{
console.log("未找到当前剧集信息")
}
return 1
}
// 当前下载状态
let state = false
async function CheckDown(){
if(state){
return
}
let list = await getDownList()
if(list.length > 0){
state = true
let dinfo = list[0]
mkdirRecursions(dinfo.save)
dow(dinfo,async (msg,s)=>{
console.log(String(msg))
if(s){
let exist = fileIsExist(dinfo.save + "/" + dinfo.title + ".mp4")
if(exist){
// 设置状态
let s = await reName(dinfo)
await setDownState(dinfo.id,1)
state = false
}else{
// 重新下载
console.log("下载失败重新下载")
state = false
}
}
})
}
}
console.log("定时检测下载")
// 设置定时器 每分钟检测更新
let job = schedule.scheduleJob('* * * * * *', () => {
CheckDown()
});