feat(route): add asianfanfics route (#18430)

* feat(route): add asianfanfics route

* feat: replace pptr with ofetch

* fix: 修复 asianfanfics 路由

- 添加 User-Agent
- 更新路由名称和描述
This commit is contained in:
KazooTTT 2025-03-04 22:11:06 +08:00 committed by GitHub
parent 691df2107e
commit e50f9b9412
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'Asianfanfics',
url: 'asianfanfics.com',
lang: 'en',
};

View File

@ -0,0 +1,89 @@
import { DataItem, Route } from '@/types';
import { config } from '@/config';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { load } from 'cheerio';
// test url http://localhost:1200/asianfanfics/tag/milklove/N
export const route: Route = {
path: '/tag/:tag/:type',
categories: ['reading'],
example: '/asianfanfics/tag/milklove/N',
parameters: {
tag: '标签',
type: '排序类型',
},
name: 'asianfanfics标签',
maintainers: ['KazooTTT'],
radar: [
{
source: ['www.asianfanfics.com/browse/tag/:tag/:type'],
target: '/tag/:tag/:type',
},
],
description: `匹配asianfanfics标签支持排序类型
- L: Latest
- N: Newest
- O: Oldest
- C: Completed
- OS: One Shots
`,
handler,
};
type Type = 'L' | 'N' | 'O' | 'C' | 'OS';
const typeToText = {
L: '最近更新',
N: '最近发布',
O: '最早发布',
C: '已完成',
OS: '短篇',
};
async function handler(ctx) {
const tag = ctx.req.param('tag');
const type = ctx.req.param('type') as Type;
if (!type || !['L', 'N', 'O', 'C', 'OS'].includes(type)) {
throw new Error('无效的排序类型');
}
const link = `https://www.asianfanfics.com/browse/tag/${tag}/${type}`;
const response = await ofetch(link, {
headers: {
'user-agent': config.trueUA,
Referer: 'https://www.asianfanfics.com/',
},
});
const $ = load(response);
const items: DataItem[] = $('.primary-container .excerpt')
.toArray()
.filter((element) => {
const $element = $(element);
return $element.find('.excerpt__title a').length > 0;
})
.map((element) => {
const $element = $(element);
const title = $element.find('.excerpt__title a').text();
const link = 'https://www.asianfanfics.com' + $element.find('.excerpt__title a').attr('href');
const author = $element.find('.excerpt__meta__name a').text().trim();
const pubDate = parseDate($element.find('time').attr('datetime') || '');
return {
title,
link,
author,
pubDate,
};
});
return {
title: `Asianfanfics - 标签:${tag} - ${typeToText[type]}`,
link,
item: items,
};
}

View File

@ -0,0 +1,68 @@
import { config } from '@/config';
import { DataItem, Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { load } from 'cheerio';
// test url http://localhost:1200/asianfanfics/text-search/milklove
export const route: Route = {
path: '/text-search/:keyword',
categories: ['reading'],
example: '/asianfanfics/text-search/milklove',
parameters: {
keyword: '关键词',
},
name: 'asianfanfics关键词',
maintainers: ['KazooTTT'],
radar: [
{
source: ['www.asianfanfics.com/browse/text_search?q=:keyword'],
target: '/text-search/:keyword',
},
],
description: `匹配asianfanfics搜索关键词`,
handler,
};
async function handler(ctx) {
const keyword = ctx.req.param('keyword');
if (keyword.trim() === '') {
throw new Error('关键词不能为空');
}
const link = `https://www.asianfanfics.com/browse/text_search?q=${keyword}+`;
const response = await ofetch(link, {
headers: {
'user-agent': config.trueUA,
},
});
const $ = load(response);
const items: DataItem[] = $('.primary-container .excerpt')
.toArray()
.filter((element) => {
const $element = $(element);
return $element.find('.excerpt__title a').length > 0;
})
.map((element) => {
const $element = $(element);
const title = $element.find('.excerpt__title a').text();
const link = 'https://www.asianfanfics.com' + $element.find('.excerpt__title a').attr('href');
const author = $element.find('.excerpt__meta__name a').text().trim();
const pubDate = parseDate($element.find('time').attr('datetime') || '');
return {
title,
link,
author,
pubDate,
};
});
return {
title: `Asianfanfics - 关键词:${keyword}`,
link,
item: items,
};
}