feat(route/pixiv): add language attributes for novels (#17667)
* feat(route/pixiv): add language tags for novels * rm redundant elements
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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 () => {
|
||||
@@ -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,
|
||||
@@ -68,6 +71,8 @@ export async function getNSFWNovelContent(novelId: string, token: string): Promi
|
||||
|
||||
seriesId: novelDetail.seriesId || null,
|
||||
seriesTitle: novelDetail.seriesTitle || null,
|
||||
|
||||
language,
|
||||
};
|
||||
})) as NovelContent;
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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,
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ export interface NovelContent {
|
||||
|
||||
seriesId: string | null;
|
||||
seriesTitle: string | null;
|
||||
|
||||
language: string | null;
|
||||
}
|
||||
|
||||
export interface SFWNovelDetail {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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', {
|
||||
@@ -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}`,
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user