Skip to content

Commit

Permalink
fix: get favorites all page
Browse files Browse the repository at this point in the history
  • Loading branch information
justorez committed May 27, 2024
1 parent d4a0df6 commit a9defb7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function main() {
}

if (answer === 'favorites') {
const res = await pica.favorites()
const res = await pica.favoritesAll()
comics.push(...res)
}

Expand Down
26 changes: 19 additions & 7 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PagePicture,
PageSearch,
PageEpisode,
Favorites
PageFavorites
} from './types'

const PICA_SECRET_KEY =
Expand Down Expand Up @@ -262,8 +262,8 @@ export class Pica {
return res.comics
}

async searchAll(keyword: string): Promise<Comic[]> {
const comics = []
async searchAll(keyword: string) {
const comics: Comic[] = []
if (keyword) {
const first = await this.search(keyword)
const pages = first.pages
Expand Down Expand Up @@ -292,10 +292,22 @@ export class Pica {
/**
* 获取收藏夹的内容
*/
async favorites() {
const url = 'users/favourite'
const res = await this.request<Favorites>('get', url)
return res.comics.docs
async favorites(page = 1, sort = this.Order.latest) {
const url = `users/favourite?page=${page}&s=${sort}`
const res = await this.request<PageFavorites>('get', url)
return res.comics
}

async favoritesAll() {
const comics: Comic[] = []
const first = await this.favorites()
const pages = first.pages
comics.push(...first.docs)
for (let page = 2; page <= pages; page++) {
const res = await this.favorites(page)
comics.push(...res.docs)
}
return comics
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ export type LoginResult = {
token: string
}

/**
* 收藏夹
*/
export type Favorites = {
docs: Comic[]
}

/**
* 分页
*/
Expand All @@ -35,6 +28,7 @@ export type Comic = {
_id: string
}

export type PageFavorites = Page<Comic>
export type PageSearch = Page<Comic>

/**
Expand Down
4 changes: 2 additions & 2 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe('测试哔咔相关 API', () => {
})

it('获取收藏夹', async () => {
const res = await pica.favorites()
fs.writeFileSync(p('favorites.json'), JSON.stringify(res), 'utf8')
const res = await pica.favoritesAll()
fs.writeFileSync(p('favoritesAll.json'), JSON.stringify(res), 'utf8')
})

it('搜索漫画', async () => {
Expand Down

0 comments on commit a9defb7

Please sign in to comment.