Skip to content

Commit

Permalink
fix(sdk): skip error comics blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
justorez committed Apr 27, 2024
1 parent 7484059 commit f201b01
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pica-cli",
"author": "justorez",
"version": "1.5.0",
"version": "1.5.1",
"description": "😉 哔咔漫画下载器",
"packageManager": "[email protected]",
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pnpm dev:zip

## 更新日志

- 2024/04/27 处理响应 400 异常:被禁止访问的漫画
- 2024/02/21 支持通过命令行输入账号密码,硬编码密钥
- 2024/02/08 支持下载指定章节
- 2024/02/01 支持通过漫画ID精确下载
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,22 @@ async function main() {
const cid = comic._id

spinner.start('正在获取章节信息')
let episodes = await pica.episodesAll(cid)
let episodes = await pica.episodesAll(cid).catch((error) => {
if (error === 400) {
spinner.stop()
log.error(`「${title}」无法访问,可能已被哔咔禁止`)
}
return []
})
episodes = filterEpisodes(episodes, cid)
spinner.stop()

log.info(`${title} 查询到 ${episodes.length} 个未下载章节`)

if (episodes.length === 0) {
continue
}

log.info(`${title} 查询到 ${pico.cyan(episodes.length)} 个未下载章节`)

const selectedEpisodes = PICA_DL_CHAPTER
? selectChapterByInput(PICA_DL_CHAPTER, episodes)
: PICA_IN_GITHUB
Expand All @@ -180,7 +186,7 @@ async function main() {
})

for (const ep of selectedEpisodes) {
spinner.start(`正在获取章节 ${ep.title} 的图片信息`)
spinner.start(`正在获取章节 ${pico.cyan(ep.title)} 的图片信息`)
let pictures = await pica.picturesAll(cid, ep)
pictures = filterPictures(pictures, title, ep.title)
spinner.stop()
Expand Down
12 changes: 9 additions & 3 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ export class Pica {
return result.data
},
(error: AxiosError) => {
const { config, message } = error
const { config, message, response } = error

// 哔咔禁止访问的资源
if (response?.status === 400) {
return Promise.reject(response?.status)
}

const url = config?.url || ''

const retryCount = this.retryMap.get(url) || 0
Expand All @@ -94,7 +100,7 @@ export class Pica {
this.retryMap.delete(url)
}

debug('\nerror %s %s %O', url, message, error.response?.data)
debug('\nerror %s %s %O', url, message, response?.data)
return Promise.reject(message)
}
)
Expand Down Expand Up @@ -256,7 +262,7 @@ export class Pica {
return res.comics
}

async searchAll(keyword: string) {
async searchAll(keyword: string): Promise<Comic[]> {
const comics = []
if (keyword) {
const first = await this.search(keyword)
Expand Down
37 changes: 25 additions & 12 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Pica } from '../src/sdk'
import path from 'node:path'
import fs from 'node:fs'
import { beforeAll, describe, it } from 'vitest'
import { beforeAll, describe, it, expect } from 'vitest'
import { loadEnv } from '../src/utils'

loadEnv()
Expand All @@ -13,7 +13,9 @@ describe('测试哔咔相关 API', () => {
const pica = new Pica()

beforeAll(async () => {
await pica.login()
const account = process.env.PICA_ACCOUNT || ''
const password = process.env.PICA_PASSWORD || ''
await pica.login(account, password)
})

it('获取排行榜', async () => {
Expand All @@ -28,25 +30,36 @@ describe('测试哔咔相关 API', () => {

it('搜索漫画', async () => {
const res = await pica.searchAll('美丽新世界')
expect(res[0].title).toBe('美丽新世界')
fs.writeFileSync(p('searchAll.json'), JSON.stringify(res), 'utf8')
})

const bookId = '5ccb04083478850224b4da84'

it('获取漫画全部章节', async () => {
it.skip('获取漫画全部章节', async () => {
const res = await pica.episodesAll(bookId)
fs.writeFileSync(p('episodesAll.json'), JSON.stringify(res), 'utf8')
})

it('获取章节下的图片', async () => {
const episodes = await pica.episodesAll(bookId)
for (const ep of episodes.slice(0, 2)) {
const res = await pica.picturesAll(bookId, ep)
fs.writeFileSync(
p(`picturesAll.${ep.order}.json`),
JSON.stringify(res),
'utf8'
)
it.skip('获取章节下的图片', async () => {
const ep = (await pica.episodes(bookId)).docs[0]
const res = await pica.picturesAll(bookId, ep)
fs.writeFileSync(
p(`picturesAll.${ep.order}.json`),
JSON.stringify(res),
'utf8'
)
})

it('被禁止访问的漫画响应400', async () => {
try {
const bookId = '5ca02791a550ec2474e788e2'
await pica.episodes(bookId)
} catch (error) {
expect(error).toBe(400)
if (error === 400) {
console.error(`无法访问,可能已被哔咔禁止`)
}
}
})
})

0 comments on commit f201b01

Please sign in to comment.