Skip to content

Commit

Permalink
fix: Modify the download message notification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
helson-lin committed Sep 16, 2024
1 parent 6593135 commit 6fd1dc8
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 162 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ lib/*
**/.DS_Store
*.swp
.DS_Store
database/sqlite.db
46 changes: 22 additions & 24 deletions bin/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ function createServer (port) {
ws(app).getWss('/')

const { getNetwork, initializeFrontEnd, modifyYml } = Utils

// registerEventCallback
this.registerEventCallback(({ name, status, url, message }) => {
const isSuccess = status === '3'
Utils.msg(this.config.webhooks, this.config.webhookType, 'ffandown notification', `${name}: ${isSuccess ? 'download successful' : 'download failed'}`)
.catch(e => {
Utils.LOG.warn('message failed:' + e)
})
})

app.ws('/ws', (ws, req) => {
ws.send(Utils.sendWsMsg('connected'))
ws.on('message', async (msg) => {
Expand Down Expand Up @@ -83,19 +93,9 @@ function createServer (port) {
for (const urlItem of url) {
// eslint-disable-next-line max-len
this.createDownloadMission({ url: urlItem, dir, preset, enableTimeSuffix: enableTimeSuffix ?? false, useragent, outputformat }).then(() => {
Utils.LOG.info('download success:' + urlItem)
// eslint-disable-next-line max-len
Utils.msg(this.config.webhooks, this.config.webhookType, 'ffandown download success', `${urlItem}`)
.catch(e => {
Utils.LOG.warn('message failed:' + e)
})
Utils.LOG.info('download mission created:' + urlItem)
}).catch((e) => {
Utils.LOG.warn('download failed:' + e)
// eslint-disable-next-line max-len
Utils.msg(this.config.webhooks, this.config.webhookType, 'ffandown download failed', `${urlItem}: ${e}`)
.catch(e => {
Utils.LOG.warn('message failed:' + e)
})
Utils.LOG.warn('download mission create failed:' + e)
})
}
} else {
Expand All @@ -108,18 +108,9 @@ function createServer (port) {
useragent,
outputformat,
}).then(() => {
Utils.LOG.info('download success:' + url)
Utils.msg(this.config.webhooks, this.config.webhookType, 'ffandown download success', `${url}`)
.catch(e => {
Utils.LOG.warn('message failed:' + e)
})
Utils.LOG.info('download mission created:' + url)
}).catch((e) => {
Utils.LOG.warn('download failed:' + e)
// eslint-disable-next-line max-len
Utils.msg(this.config.webhooks, this.config.webhookType, 'ffandown download failed', `${url}: ${e}`)
.catch(e => {
Utils.LOG.warn('message failed:' + e)
})
Utils.LOG.warn('download mission create failed:' + e)
})
}
res.send({ code: 0, message: `${name} video download mission create success` })
Expand Down Expand Up @@ -237,7 +228,14 @@ function createServer (port) {
})
app.listen(port, async () => {
// initial front end resouce
await initializeFrontEnd()
try {
await initializeFrontEnd()
} catch (e) {
// download frontend static file error;
console.warn(colors.red(e));
process.exit(0)

}
const list = await getNetwork()
const listenString = list.reduce((pre, val) => {
return pre + `\n ${colors.white(' -')} ${colors.brightCyan('http://' + val + ':' + port + '/')}`
Expand Down
2 changes: 1 addition & 1 deletion bin/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getFeiShuBody = (text, More) => {
content: {
post: {
zh_cn: {
title: '文件下载通知',
title: 'ffandown',
content,
},
},
Expand Down
38 changes: 28 additions & 10 deletions bin/utils/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs')
const fse = require('fs-extra')
const path = require('path')
const download = require('download')
const log = require('./log')

const getLatestVersion = async (repo = 'ffandown-front') => {
// eslint-disable-next-line max-len
Expand Down Expand Up @@ -88,17 +89,34 @@ const addVersionFile = (version, msg, upd = new Date().getTime()) => {
}

const autoUpdateFrontEnd = async () => {
const { version, urls } = await getLatestVersion()
const { browser_download_url } = urls[0]
if (!browser_download_url) throw new Error('no latest release url found')
fse.ensureDirSync(path.join(process.cwd(), 'public'))
fse.emptyDirSync(path.join(process.cwd(), 'public'))
await download('https://nn.oimi.space/' + browser_download_url, path.join(process.cwd(), 'public'), {
filename: 'ffandown.zip',
extract: true,
const ORA = await import('ora')
const ora = ORA?.default
const spinner = ora({
text: `auto update frontend`,
})
moveDistFile()
addVersionFile(version, '自动更新成功')
spinner.prefixText = '[ffandown]'
spinner.start()
try {
const { version, urls } = await getLatestVersion()
const { browser_download_url } = urls[0]
if (!browser_download_url) throw new Error('no latest release url found')
fse.ensureDirSync(path.join(process.cwd(), 'public'))
fse.emptyDirSync(path.join(process.cwd(), 'public'))
// add download supported log
spinner.info('downloading new version frontend')
await download('https://nn.oimi.space/' + browser_download_url, path.join(process.cwd(), 'public'), {
filename: 'ffandown.zip',
extract: true,
})
spinner.info('moving static file')
moveDistFile()
spinner.info('generate version file')
addVersionFile(version, '自动更新成功')
spinner.succeed('update frontend succeed')
} catch (e) {
spinner.fail('check update failed:')
throw e;
}
}

const initializeFrontEnd = async () => {
Expand Down
4 changes: 2 additions & 2 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
port: 8081
downloadDir: /media/
webhooks: ''
webhookType: bark
webhooks: 'https://open.feishu.cn/open-apis/bot/v2/hook/ec34fee9-5695-4e2d-8938-129e71656ad2'
webhookType: feishu
thread: true
useFFmpegLib: true
maxDownloadNum: 5
Expand Down
Binary file modified database/sqlite.db
Binary file not shown.
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@
"live"
],
"author": "heslonlin",
"license": "ISC",
"license": "MIT",
"dependencies": {
"body-parser": "^1.20.1",
"colors": "^1.4.0",
"download": "^8.0.0",
"express": "^4.18.2",
"express-ws": "^5.0.2",
"figlet": "^1.6.0",
"fluent-ffmpeg": "^2.1.2",
"fs-extra": "^11.1.0",
"js-yaml": "^4.1.0",
"node-fetch": "2",
"oimi-helper": "^1.3.3",
"oimi-helper": "^1.3.7",
"ora": "^8.1.0",
"pidusage": "^3.0.2",
"request": "^2.88.2",
"sequelize": "^6.31.0",
"sqlite3": "^5.1.6",
"systeminformation": "^5.17.12",
"uuid": "^9.0.0",
"winston": "^3.8.2",
Expand Down
Loading

0 comments on commit 6fd1dc8

Please sign in to comment.