Skip to content

Commit

Permalink
Merge pull request #7 from Cansiny0320/feat-cli
Browse files Browse the repository at this point in the history
feat: cli
  • Loading branch information
Cansiny0320 authored Aug 26, 2022
2 parents 7aefd4b + 14a1ac7 commit 5c69fe1
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 46 deletions.
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,45 @@

🔧 可配置书籍来源网站

❓ 完善的帮助信息

## 💡 使用方法

> 需要 Node 版本 >= 15.0.0
```bash
$ npm i book-spider -g

$ book-spider [小说名]
$ bs -v # 查看版本号
$ bs -h # 查看帮助
$ bs [小说名] # 下载小说
```

默认会自动选择最快书源,若要指定书源

```bash

$ book-spider --source [url] [小说名]
$ book-spider -s [url] [小说名]
# url: 书源网址
# https://www.biquge.com.cn
# https://www.xbiquge.la
# https://www.xbiquwx.la
# https://www.biqugeu.net
# https://www.shuquge.com
$ bs --source [url] [小说名]
$ bs -s [url] [小说名]
$ bs list # 查看支持的书源
```

支持批量下载 小说名之间用空格分开

```bash
$ book-spider [小说名] [小说名] ...
$ bs [小说名] [小说名] ...
```

顺序下载模式,按章节顺序写入文件,支持断点断续(下载速度不如正常模式,适用于小说章节较多的情况)

```bash
$ book-spider -t [小说名]
$ bs -t [小说名]
```

默认并发数为 64,若要修改并发限制

```bash
$ book-spider --limit [number] [小说名]
$ book-spider -l [number] [小说名]
$ bs --limit [number] [小说名]
$ bs -l [number] [小说名]
```

## 运行效果
Expand All @@ -73,10 +71,10 @@ $ book-spider -l [number] [小说名]

npm i

npm run spider [小说名]
npm run start [小说名]

# 添加参数请使用
npm run spider -- -s [url] [小说名]
npm run start -s [url] [小说名]
...
```

Expand Down
2 changes: 1 addition & 1 deletion bin/book-spider.mjs → bin/cli.mjs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node
'use strict'
import '../dist/index.mjs'
import '../dist/cli.mjs'
5 changes: 3 additions & 2 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: ['src/index'],
entries: ['src/cli'],
// declaration: true,
clean: true,
failOnWarn: false,
rollup: {
// emitCJS: true,
esbuild: {
minify: true,
// minify: true,
},
},
})
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"bin": {
"book-spider": "bin/book-spider.mjs"
"bs": "bin/cli.mjs"
},
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"check-source": "esno ./scripts/checkSource.ts",
"prepublishOnly": "nr build",
"release": "bumpp && npm publish",
"spider": "esno src/index.ts",
"start": "esno src/cli.ts",
"typecheck": "tsc --noEmit",
"lint": "eslint",
"lint:fix": "eslint --fix"
},
"dependencies": {
"@cansiny0320/async-extra": "^0.2.1",
"axios": "^0.27.2",
"cac": "^6.7.12",
"cheerio": "^1.0.0-rc.10",
"iconv-lite": "^0.6.3",
"mri": "^1.2.0",
Expand Down
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { cac } from 'cac'
import pkg from '../package.json'
import { main } from './index'

const cli = cac('book-spider')

cli
.command('[...names]', '小说名', {
allowUnknownOptions: true,
})
.option('-s, --source <source>', '设置书源')
.option('-t, --turn', '顺序下载')
.option('-l, --limit <number>', '限制并发数', {
default: 64,
})
.example(' $ bs 铁血残明')
.example(' $ bs 铁血残明 -s https://www.xbiquge.la')
.example(' $ bs list')
.action((names, options) => {
main({
bookNames: names,
...options,
})
})

cli
.command('download', '批量下载小说')
.option('-s, --source <source>', '设置书源')
.option('-t, --turn', '顺序下载')
.option('-l, --limit <number>', '限制并发数', {
default: 64,
})
.action(options => {
main({
download: true,
...options,
})
})
cli.command('list', '显示所有书源').action(() => {
console.log(
[
'https://www.biquge.com.cn',
'https://www.xbiquge.la',
'https://www.xbiquwx.la',
'https://www.qu-la.com',
'https://www.shuquge.com',
].join('\n'),
)
})

cli
.help(sections => {
const [versionSection, usageSection] = sections
versionSection.body = `book-spider v${pkg.version}`
usageSection.body = ` $ bs <小说名 小说名...> [参数]
$ book-spider <小说名 小说名...> [参数]`
sections[2].title = 'Other Commands'
const [, ...otherCommands] = sections[2].body.split('\n')
sections[2].body = otherCommands.join('\n')
})
.version(pkg.version)
.parse()
47 changes: 23 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import fs from 'fs'
import mri from 'mri'

import type { IOptions } from './interface'
import { Spider } from './spider'
import { getSpecSource } from './utils'

const args = mri(process.argv.splice(2))
export const main = async (args: any) => {
const bookNames: string[] = args.bookNames ?? []
const url = args.source || args.s
const limit = args.limit || args.l
const inTurn = args.turn || args.t

const bookNames = args._
const url = args.source || args.s
const limit = args.limit || args.l
const inTurn = args.t
const options: IOptions = {
limit: parseInt(limit) || 64,
mode: inTurn ? 1 : 0,
}

const options: IOptions = {
limit: parseInt(limit) || 64,
mode: inTurn ? 1 : 0,
}

if (url) {
options.source = getSpecSource(url)
}
if (url) {
options.source = getSpecSource(url)
}

const spider = new Spider(options)
const spider = new Spider(options)

if (bookNames[0] === 'download') {
const download = fs.readFileSync('./download.txt', 'utf-8').split('\r\n')
download.forEach(item => {
spider.run(item)
})
} else {
bookNames.forEach(item => {
spider.run(item)
})
if (args.download) {
const download = fs.readFileSync('./download.txt', 'utf-8').split('\r\n')
download.forEach(item => {
spider.run(item)
})
} else {
bookNames.forEach(item => {
spider.run(item)
})
}
}

0 comments on commit 5c69fe1

Please sign in to comment.