Skip to content

Commit

Permalink
feat(route/pixiv): add language attributes for novels (DIYgod#17667)
Browse files Browse the repository at this point in the history
* feat(route/pixiv): add language tags for novels

* rm redundant elements
  • Loading branch information
keocheung authored Nov 22, 2024
1 parent 7c4d3cc commit cc23a80
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
12 changes: 12 additions & 0 deletions lib/routes/pixiv/novel-api/content/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';

export async function getNovelLanguage(novelId: string): Promise<string> {
return (await cache.tryGet(`https://www.pixiv.net/novel/show.php?id=${novelId}`, async () => {
const rsp = await got(`https://www.pixiv.net/novel/show.php?id=${novelId}`);
const $ = load(rsp.data);
const data = JSON.parse($('#meta-preload-data').attr('content'));
return data?.novel[novelId].language;
})) as string;
}
5 changes: 5 additions & 0 deletions lib/routes/pixiv/novel-api/content/nsfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import queryString from 'query-string';
import { parseNovelContent } from './utils';
import type { NovelContent, NSFWNovelDetail } from './types';
import { parseDate } from '@/utils/parse-date';
import { getNovelLanguage } from './common';

export async function getNSFWNovelContent(novelId: string, token: string): Promise<NovelContent> {
return (await cache.tryGet(`https://app-api.pixiv.net/webview/v2/novel:${novelId}`, async () => {
Expand Down Expand Up @@ -43,6 +44,8 @@ export async function getNSFWNovelContent(novelId: string, token: string): Promi

const parsedContent = await parseNovelContent(novelDetail.text, images, token);

const language = await getNovelLanguage(novelId);

return {
id: novelDetail.id,
title: novelDetail.title,
Expand All @@ -68,6 +71,8 @@ export async function getNSFWNovelContent(novelId: string, token: string): Promi

seriesId: novelDetail.seriesId || null,
seriesTitle: novelDetail.seriesTitle || null,

language,
};
})) as NovelContent;
}
5 changes: 5 additions & 0 deletions lib/routes/pixiv/novel-api/content/sfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pixivUtils from '../../utils';
import { parseNovelContent } from './utils';
import { NovelContent, SFWNovelDetail } from './types';
import { parseDate } from '@/utils/parse-date';
import { getNovelLanguage } from './common';

const baseUrl = 'https://www.pixiv.net';

Expand Down Expand Up @@ -33,6 +34,8 @@ export async function getSFWNovelContent(novelId: string): Promise<NovelContent>

const parsedContent = await parseNovelContent(novelDetail.body.content, images);

const language = await getNovelLanguage(novelId);

return {
id: body.id,
title: body.title,
Expand All @@ -58,6 +61,8 @@ export async function getSFWNovelContent(novelId: string): Promise<NovelContent>

seriesId: body.seriesNavData?.seriesId?.toString() || null,
seriesTitle: body.seriesNavData?.title || null,

language,
};
})) as NovelContent;
}
2 changes: 2 additions & 0 deletions lib/routes/pixiv/novel-api/content/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface NovelContent {

seriesId: string | null;
seriesTitle: string | null;

language: string | null;
}

export interface SFWNovelDetail {
Expand Down
8 changes: 3 additions & 5 deletions lib/routes/pixiv/novel-api/series/nsfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ export async function getNSFWSeriesNovels(seriesId: string, limit: number = 10):
title: `#${chapterStartNum + index} ${novelContent.title}`,
description: `
<img src="${pixivUtils.getProxiedImageUrl(novelContent.coverUrl)}" />
<div lang="${novelContent.language}">
<p>${novelContent.description}</p>
<p>
收藏數:${novelContent.bookmarkCount}<br>
閱覧數:${novelContent.viewCount}<br>
喜歡數:${novelContent.likeCount}<br>
</p><hr>
<hr>
${novelContent.content}
</div>
`,
link: `${baseUrl}/novel/show.php?id=${novelContent.id}`,
pubDate: novelContent.createDate,
Expand Down
8 changes: 3 additions & 5 deletions lib/routes/pixiv/novel-api/series/sfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ export async function getSFWSeriesNovels(seriesId: string, limit: number = 10):
title: `#${chapterStartNum + index} ${novelContent.title}`,
description: `
<img src="${pixivUtils.getProxiedImageUrl(novelContent.coverUrl)}" />
<div lang="${novelContent.language}">
<p>${novelContent.description}</p>
<p>
收藏數:${novelContent.bookmarkCount}<br>
閱覧數:${novelContent.viewCount}<br>
喜歡數:${novelContent.likeCount}<br>
</p><hr>
<hr>
${novelContent.content}
</div>
`,
link: `${baseUrl}/novel/show.php?id=${novelContent.id}`,
pubDate: novelContent.createDate,
Expand Down
10 changes: 4 additions & 6 deletions lib/routes/pixiv/novel-api/user-novels/nsfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ConfigNotFoundError from '@/errors/types/config-not-found';
import cache from '@/utils/cache';
import { getToken } from '../../token';
import InvalidParameterError from '@/errors/types/invalid-parameter';
import { getNovelLanguage } from '../content/common';

function getNovels(user_id: string, token: string): Promise<NSFWNovelsResponse> {
return got('https://app-api.pixiv.net/v1/user/novels', {
Expand Down Expand Up @@ -46,17 +47,14 @@ export async function getNSFWUserNovels(id: string, fullContent: boolean = false

const items = await Promise.all(
novels.map(async (novel) => {
const language = await getNovelLanguage(novel.id);
const baseItem = {
title: novel.series?.title ? `${novel.series.title} - ${novel.title}` : novel.title,
description: `
<img src="${pixivUtils.getProxiedImageUrl(novel.image_urls.large)}" />
<div lang="${language}">
<p>${convertPixivProtocolExtended(novel.caption)}</p>
<p>
字數:${novel.text_length}<br>
閱覽數:${novel.total_view}<br>
收藏數:${novel.total_bookmarks}<br>
評論數:${novel.total_comments}<br>
</p>`,
</div>`,
author: novel.user.name,
pubDate: parseDate(novel.create_date),
link: `https://www.pixiv.net/novel/show.php?id=${novel.id}`,
Expand Down
9 changes: 4 additions & 5 deletions lib/routes/pixiv/novel-api/user-novels/sfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseDate } from '@/utils/parse-date';
import pixivUtils from '../../utils';
import { getSFWNovelContent } from '../content/sfw';
import type { SFWNovelsResponse, NovelList } from './types';
import { getNovelLanguage } from '../content/common';

const baseUrl = 'https://www.pixiv.net';

Expand Down Expand Up @@ -36,16 +37,14 @@ export async function getSFWUserNovels(id: string, fullContent: boolean = false,

const items = await Promise.all(
Object.values(data.body.works).map(async (item) => {
const language = await getNovelLanguage(item.id);
const baseItem = {
title: item.title,
description: `
<img src=${pixivUtils.getProxiedImageUrl(item.url)} />
<div lang="${language}">
<p>${item.description}</p>
<p>
字數:${item.textCount}<br>
閱讀時間:${item.readingTime} 分鐘<br>
收藏數:${item.bookmarkCount}<br>
</p>
</div>
`,
link: `${baseUrl}/novel/show.php?id=${item.id}`,
author: item.userName,
Expand Down

0 comments on commit cc23a80

Please sign in to comment.