Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding search method by artist #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/lib/Classes/NHentai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosInstance } from 'axios'
import axios, { AxiosInstance, AxiosError } from 'axios'
import { load } from 'cheerio'
import { CookieJar } from 'tough-cookie'
import { HttpsCookieAgent } from 'http-cookie-agent/http'
Expand Down Expand Up @@ -123,6 +123,38 @@ export class NHentai {
})
}

/**
* Searches for a doujin by an artist
* @param artist Name of the artist to search
* @param options Options for searching
* @returns The result of the search
*/
public artist = async (
artist: string,
options?: { page?: number }
): Promise<IList> => {
if (!artist)
throw new Error("The 'artist' parameter shouldn't be undefined")
let page = 1
if (options?.page && options.page > 0) page = options.page
return await this.#axios
.get<string>(`${this._options.site}/artist/${artist}/?page=${page}`)
.then((res) => {
const results = parseDoujinList(
load(res.data),
this._options.site.split('nhentai.')[1] as 'to'
)
if (!results.data.length)
throw new Error('No author results found')
return results
})
.catch((reason: AxiosError) => {
if (reason.response!.status === 404)
throw new Error("Can't find artist")
throw new Error(reason.message)
})
}

/**
* Gets the info of a doujin by its ID
* @param id ID of the doujin
Expand Down