Skip to content

Commit

Permalink
feat: add limit support for nsfw novels
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuyumi25 committed Nov 1, 2024
1 parent d1c2eaa commit 81469f3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
11 changes: 2 additions & 9 deletions lib/routes/pixiv/api/get-novels-nsfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,9 @@ export async function parseNovelContent(novelDetail: nsfwNovelDetail, token: str

function convertPixivProtocolExtended(caption: string): string {
const protocolMap = new Map([
// 小說鏈接 Novel links
[/pixiv:\/\/novels\/(\d+)/g, 'https://www.pixiv.net/novel/show.php?id=$1'],

// 插畫鏈接 Illustration links
[/pixiv:\/\/illusts\/(\d+)/g, 'https://www.pixiv.net/artworks/$1'],

// 用戶鏈接 User links
[/pixiv:\/\/users\/(\d+)/g, 'https://www.pixiv.net/users/$1'],

// 小說系列鏈接 Novel series links
[/pixiv:\/\/novel\/series\/(\d+)/g, 'https://www.pixiv.net/novel/series/$1'],
]);

Expand All @@ -289,7 +282,7 @@ function convertPixivProtocolExtended(caption: string): string {
return convertedText;
}

export async function getR18Novels(id: string, fullContent: boolean) {
export async function getR18Novels(id: string, fullContent: boolean, limit: number = 100) {
if (!config.pixiv || !config.pixiv.refreshToken) {
throw new ConfigNotFoundError(
'該用戶爲 R18 創作者,需要 PIXIV_REFRESHTOKEN。This user is an R18 creator, PIXIV_REFRESHTOKEN is required - pixiv RSS is disabled due to the lack of <a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>'
Expand All @@ -302,7 +295,7 @@ export async function getR18Novels(id: string, fullContent: boolean) {
}

const response = await getNovels(id, token);
const novels = response.data.novels;
const novels = limit ? response.data.novels.slice(0, limit) : response.data.novels;
const username = novels[0].user.name;

const items = await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/pixiv/api/get-novels-sfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async function getNovelFullContent(novelId: string): Promise<{ content: string;
images,
};
}
export async function getNonR18Novels(id: string, limit: number = 100, fullContent: boolean) {
export async function getNonR18Novels(id: string, fullContent: boolean, limit: number = 100) {
const url = `${baseUrl}/users/${id}/novels`;
const { data: allData } = await got(`${baseUrl}/ajax/user/${id}/profile/all`, {
headers: {
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/pixiv/novels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ async function handler(ctx): Promise<Data> {

// Use R18 API first if auth exists
if (hasPixivAuth()) {
return await getR18Novels(id, fullContent);
return await getR18Novels(id, fullContent, limit);
}

// Attempt non-R18 API when Pixiv auth is missing
const nonR18Result = await getNonR18Novels(id, limit, fullContent).catch(() => null);
const nonR18Result = await getNonR18Novels(id, fullContent, limit).catch(() => null);
if (nonR18Result) {
return nonR18Result;
}

// Fallback to R18 API as last resort
return await getR18Novels(id, fullContent);
return await getR18Novels(id, fullContent, limit);
}

0 comments on commit 81469f3

Please sign in to comment.