Skip to content

Commit

Permalink
feat: add support picacomic service
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Mar 19, 2022
1 parent 1a8ff47 commit 328f520
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { httpOverHttp, httpsOverHttp } from 'tunnel'
import { v4 as uuidv4 } from 'uuid'
import { createHmac } from 'crypto'
import got, { HTTPError } from 'got'
import { isPromise } from './util'

const debug = require('debug')('lgou2w:picacomic-api')

Expand Down Expand Up @@ -59,10 +60,6 @@ function makeAuthorizationHeaders (token: string) {
return { authorization: token }
}

function isPromise<T = any> (obj: any): obj is Promise<T> {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

type Response<T> = {
code: number
message: string
Expand Down Expand Up @@ -216,7 +213,7 @@ export class PicaComicAPI {
.catch(catchError)
}

async fetchUserFavourite (payload: { token: string, page?: number, sort?: 'ua' | 'dd' | 'da' | 'ld' | 'vd' }): Promise<types.Comics> {
async fetchUserFavourite (payload: { token: string, page?: number, sort?: types.ComicSort }): Promise<types.Comics> {
return this.fetch
.get('users/favourite', {
headers: makeAuthorizationHeaders(payload.token),
Expand All @@ -238,7 +235,7 @@ export class PicaComicAPI {
.catch(catchError)
}

async fetchComics (payload: { token: string, category: string, page?: number, sort?: 'ua' | 'dd' | 'da' | 'ld' | 'vd' }): Promise<types.Comics> {
async fetchComics (payload: { token: string, category: string, page?: number, sort?: types.ComicSort }): Promise<types.Comics> {
return this.fetch
.get('comics', {
headers: makeAuthorizationHeaders(payload.token),
Expand Down Expand Up @@ -326,7 +323,7 @@ export class PicaComicAPI {
keyword: string
categories?: string[]
page?: number
sort?: 'ua' | 'dd' | 'da' | 'ld' | 'vd'
sort?: types.ComicSort
}): Promise<types.Comics> {
return this.fetch
.post('comics/advanced-search', {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './type'
export * from './api'
export * from './service'
101 changes: 101 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type * as types from './type'
import { PicaComicAPI, Options } from './api'
import { isPromise } from './util'

export type ServiceOptions = Partial<Omit<Options, 'reauthorizationTokenCallback'>> & {
email: string
password: string
token?: string
onReauthorizationToken?: (token: string) => void | Promise<void>
}

export class PicaComicService {
public readonly api: PicaComicAPI
public readonly email: string
public readonly password: string
public readonly onReauthorizationToken?: (token: string) => void | Promise<void>
public token: string

constructor (opts: ServiceOptions) {
const { email, password, token, onReauthorizationToken, ...other } = opts
this.email = email
this.password = password
this.token = token || ''
this.onReauthorizationToken = onReauthorizationToken
this.api = new PicaComicAPI({
...other,
reauthorizationTokenCallback: async (self) => {
const token = await self.signIn({ email, password })
this.token = token

if (isPromise(this.onReauthorizationToken)) {
await this.onReauthorizationToken(token)
} else if (this.onReauthorizationToken) {
this.onReauthorizationToken(token)
}

return token
}
})
}

async punchIn (): ReturnType<PicaComicAPI['punchIn']> {
return this.api.punchIn({ token: this.token })
}

async fetchUserProfile (): ReturnType<PicaComicAPI['fetchUserProfile']> {
return this.api.fetchUserProfile({ token: this.token })
}

async fetchUserFavourites (payload?: { page?: number, sort?: types.ComicSort }): ReturnType<PicaComicAPI['fetchUserFavourite']> {
return this.api.fetchUserFavourite({ token: this.token, ...payload })
}

async fetchCategories (): ReturnType<PicaComicAPI['fetchCategories']> {
return this.api.fetchCategories({ token: this.token })
}

async fetchComics (payload: { category: string, page?: number, sort?: types.ComicSort }): ReturnType<PicaComicAPI['fetchComics']> {
return this.api.fetchComics({ token: this.token, ...payload })
}

async fetchComic (payload: { id: string }): ReturnType<PicaComicAPI['fetchComic']> {
return this.api.fetchComic({ token: this.token, ...payload })
}

async fetchComicComments (payload: { comicId: string, page?: number }): ReturnType<PicaComicAPI['fetchComicComments']> {
return this.api.fetchComicComments({ token: this.token, ...payload })
}

async fetchComicEpisodes (payload: { comicId: string, page?: number }): ReturnType<PicaComicAPI['fetchComicEpisodes']> {
return this.api.fetchComicEpisodes({ token: this.token, ...payload })
}

async fetchComicEpisodePages (payload: { comicId: string, epsOrder: number, page?: number }): ReturnType<PicaComicAPI['fetchComicEpisodePages']> {
return this.api.fetchComicEpisodePages({ token: this.token, ...payload })
}

async search (payload: { keyword: string, categories?: string[], page?: number, sort?: types.ComicSort }): ReturnType<PicaComicAPI['search']> {
return this.api.search({ token: this.token, ...payload })
}

async switchComicLike (payload: { id: string }): ReturnType<PicaComicAPI['switchComicLike']> {
return this.api.switchComicLike({ token: this.token, ...payload })
}

async switchComicFavourite (payload: { id: string }): ReturnType<PicaComicAPI['switchComicFavourite']> {
return this.api.switchComicFavourite({ token: this.token, ...payload })
}

async setUserProfileSlogan (payload: { slogan: string }): ReturnType<PicaComicAPI['setUserProfileSlogan']> {
return this.api.setUserProfileSlogan({ token: this.token, ...payload })
}

stringifyImageUrl (image: { path: string, fileServer?: string }): ReturnType<PicaComicAPI['stringifyImageUrl']> {
return this.api.stringifyImageUrl(image)
}

fetchImage (image: { path: string, fileServer?: string }): ReturnType<PicaComicAPI['fetchImage']> {
return this.api.fetchImage(image)
}
}
10 changes: 10 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ export interface ComicEpisodePages {
title: string
}
}

export type ComicSort = 'ua' | 'dd' | 'da' | 'ld' | 'vd'

export const ComicSorts: Record<'DEFAULT' | 'NEW_TO_OLD' | 'OLD_TO_NEW' | 'LIKE' | 'VIEW', ComicSort> = {
DEFAULT: 'ua',
NEW_TO_OLD: 'dd',
OLD_TO_NEW: 'da',
LIKE: 'ld',
VIEW: 'vd'
}
3 changes: 3 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isPromise<T = any> (obj: any): obj is Promise<T> {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

0 comments on commit 328f520

Please sign in to comment.