ptSend/util/Download/index.js

135 lines
4.0 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");
const { sendSeed } = require('../SendSeed/index.js');
// 小于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 {
state: 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}@ZmWeb.mp4`
let state = fs.renameSync(dinfo.save + "/" + dinfo.title + ".mp4", dinfo.save + "/" + rname)
let newPath = ""
let tem = dinfo.save.split("/")
tem.pop()
let seedName = `${vinfo.name}.${vinfo.rename}.${vinfo.year}.S${addZeor(vinfo.season)}.WEB-DL.${fbl}p.${Video.Format}.${Audio.Format}@ZmWeb`
tem.push(seedName)
newPath = tem.join("/")
return {
state: 1,
info: {
vid: dinfo.vid,
oldPath: dinfo.save,
newPath: newPath,
seedName,
title: `${vinfo.rename} ${vinfo.year} S${addZeor(vinfo.season)}Epxxx WEB-DL ${fbl}p ${Video.Format} ${Audio.Format}@ZmWeb`,
Duration: Video.Duration,
rname,
ep: addZeor(dinfo.title),
fbl
}
}
} else {
console.log("未找到当前剧集信息")
return {
state: 0
}
}
}
function dowv(dinfo){
return new Promise((res)=>{
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, s.state)
state = false
if (s.info) {
await sendSeed(s.info)
}
res(s)
} else {
// 重新下载
console.log("下载失败重新下载")
state = false
}
}
})
})
}
// 当前下载状态
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)
await dowv(dinfo)
}
}
console.log("定时检测下载")
// 设置定时器 每分钟检测更新
let job = schedule.scheduleJob('* * * * * *', () => {
CheckDown()
});