Skip to content

Commit

Permalink
feat: github 子目录文件下载
Browse files Browse the repository at this point in the history
  • Loading branch information
elecV2 committed Aug 21, 2021
1 parent 1de0c02 commit 973175f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
81 changes: 81 additions & 0 deletions examples/JSTEST/github-subdownload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// github 子目录文件下载
// 仅适用于 elecV2P
// Todo:
// [ ] 多文件同时下载(限制最大数

const config = {
repos: $env.repos || 'elecV2/elecV2P-dei', // github 仓库名。比如 elecV2/elecV2P
folder: $env.folder || 'examples/JSTEST', // 子目录。比如 script/JSFile
dest: $env.dest || 'script/JSFile', // 下载到此目录。
options: {
recursive: true, // 是否下载 folder 下的子目录文件
onlyreg: $env.onlyreg || '', // 只下载文件名满足该正则表达式的文件
skipreg: $env.skipreg || '', // 跳过下载文件名满足该正则表达式的文件
sizemax: $env.sizemax || 0, // 当文件大小超过此设置值时,不下载。0: 表示不限制
}
}

getTree(`https://api.github.com/repos/${config.repos}/contents/${config.folder}`).then(tree=>{
main(tree, config.dest, config.options)
}).catch(e=>console.error(e.message))

async function getTree(apigit) {
try {
console.log('start get', apigit, 'tree')
return await $axios(apigit).then(res=>res.data)
} catch(e) {
console.error('get', apigit, 'error', e.message)
return []
}
}

async function main(tree, dest, options = { recursive: true, onlyreg: '', skipreg: '', sizemax: 0 }) {
if (!(typeof tree === 'object' && tree.length > 0 && typeof dest === 'string')) {
console.log('输入参数有误', tree, dest)
return
}

for (let file of tree) {
if (file.type === 'file') {
if (options.sizemax > 0 && file.size > options.sizemax) {
console.log('skip download', file.name, 'file size:', file.size, 'is big than', options.sizemax)
continue
}
if (options.onlyreg) {
let reg = new RegExp(options.onlyreg)
if (!reg.test(file.name)) {
console.log('skip download', file.name, 'for onlyreg', reg)
continue
}
}
if (options.skipreg) {
let reg = new RegExp(options.skipreg)
if (reg.test(file.name)) {
console.log('skip download', file.name, 'for skipreg', reg)
continue
}
}

try {
await $download(file['download_url'], {
folder: dest,
name: file.name
}, (d) => {
if (d.progress) {
console.log(d.progress + '\r')
} else {
console.log(file.name, '下载完成')
}
}).then(d=>console.log('文件已下载至:', d))
} catch(e) {
console.error(e.message || e)
}
} else {
if (options.recursive) {
await main(await getTree(file.url), dest + '/' + file.name, options)
}
}
}

console.log('文件已下载至', dest)
}
2 changes: 2 additions & 0 deletions examples/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

![](https://raw.githubusercontent.com/elecV2/elecV2P-dei/master/examples/res/boxjs-test.png)

- [github-subdownload.js](https://github.com/elecV2/elecV2P-dei/blob/master/examples/JSTEST/github-subdownload.js) \- github 子目录文件下载
- [exam-rss.js](https://github.com/elecV2/elecV2P-dei/blob/master/examples/JSTEST/exam-rss.js) \- 使用 cheerio 解析 rss 实现限免软件推送
- [exam-tasksub.js](https://github.com/elecV2/elecV2P-dei/blob/master/examples/JSTEST/exam-tasksub.js) \- 通过 webhook 批量添加定时任务
- [reboot.js](https://github.com/elecV2/elecV2P-dei/blob/master/examples/JSTEST/reboot.js) \- 通过 JS 重启服务器
Expand All @@ -33,6 +34,7 @@
- 开始/暂停 定时任务
- 删除/保存 定时任务
- 执行 shell 指令
- store/cookie 常量管理

前提: elecV2P 服务器可通过外网访问

Expand Down

0 comments on commit 973175f

Please sign in to comment.