fix(route/kpopping): migrate kpics and news routes to use new API (#23043)

* fix(route/kpopping): migrate kpics and news routes to use new API

* fix(route/kpopping): fix script style, move to single filter segment
This commit is contained in:
Pinapelz
2026-08-17 15:59:56 -07:00
committed by GitHub
parent b8f30937cf
commit 697421be62
3 changed files with 145 additions and 349 deletions
+101
View File
@@ -0,0 +1,101 @@
import type { Context } from 'hono';
import type { Data, DataItem, Route } from '@/types';
import { ViewType } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { renderDescription } from './templates/description';
export const handler = async (ctx: Context): Promise<Data> => {
const filter = ctx.req.param('filter');
const params = new URLSearchParams(filter);
const apiUrl = new URL('https://kpopping.com/api/articles');
apiUrl.search = params.toString();
const response = await ofetch(apiUrl.href);
const articles = response.articles || [];
const items: DataItem[] = articles.map((item: any) => {
const title: string = item.title;
const description = renderDescription({
images: item.coverImage ? [{ src: item.coverImage, alt: title }] : undefined,
description: item.excerpt,
});
const link = `https://kpopping.com/community/${item.slug}`;
const dateStr = item.publishedAt ?? item.createdAt;
const pubDate = dateStr ? parseDate(dateStr) : undefined;
const category = item.category;
const author = item.authorName;
return {
title,
description,
link,
pubDate,
category,
author,
content: {
html: description,
text: description,
},
image: item.coverImage,
banner: item.coverImage,
};
});
return {
title: 'Community - kpopping',
link: 'https://kpopping.com/community',
item: items,
language: 'en',
};
};
export const route: Route = {
path: '/community/:filter{.+}?',
name: 'Community',
url: 'kpopping.com',
maintainers: ['nczitzk', 'pinapelz'],
handler,
example: '/kpopping/community/category=news&idolId=7d8f48d4-97c4-4164-9f04-11febc9c8ac1',
parameters: {
filter: 'Filter parameters in `key=value&key2=value2` format. Supported keys: `category`, `gender`, `sort`, `entityType`, `idolId`, `groupId`',
},
description: `::: tip
Query community posts using filter parameters found on kpopping such as \`idolId\`, \`groupId\`, \`gender\`, \`category\`, \`sort\`, etc.
:::`,
categories: ['new-media'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['kpopping.com/community'],
target: '/community',
},
],
view: ViewType.Articles,
zh: {
path: '/community/:filter{.+}?',
name: 'Community',
url: 'kpopping.com',
maintainers: ['nczitzk', 'pinapelz'],
handler,
example: '/kpopping/community/category=news&idolId=7d8f48d4-97c4-4164-9f04-11febc9c8ac1',
parameters: {
filter: '以 `key=value&key2=value2` 格式传递的过滤参数。支持的 key 包括 `category`、`gender`、`sort`、`entityType`、`idolId`、`groupId`',
},
description: `::: tip
支持通过 \`idolId\`\`groupId\`\`gender\`\`category\`\`sort\` 等过滤条件获取新闻与社区帖子。
:::`,
},
};
+44 -157
View File
@@ -1,162 +1,53 @@
import type { Cheerio, CheerioAPI } from 'cheerio';
import { load } from 'cheerio';
import type { Element } from 'domhandler';
import type { Context } from 'hono';
import type { Data, DataItem, Language, Route } from '@/types';
import type { Data, DataItem, Route } from '@/types';
import { ViewType } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { renderDescription } from './templates/description';
export const handler = async (ctx: Context): Promise<Data> => {
const { filter } = ctx.req.param();
const limit = Number(ctx.req.query('limit') ?? '30');
const filter = ctx.req.param('filter');
const params = new URLSearchParams(filter);
const baseUrl = 'https://kpopping.com';
const targetUrl: string = new URL(`kpics${filter ? `/${filter}` : ''}`, baseUrl).href;
const apiUrl = new URL('https://kpopping.com/api/photos');
apiUrl.search = params.toString();
const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language = $('html').attr('lang') ?? 'en';
const response = await ofetch(apiUrl.href);
let items: DataItem[] = $('div.pics div.matrix div.cell')
.slice(0, limit)
.toArray()
.map((el) => {
const $el: Cheerio<Element> = $(el);
const title: string = $el.find('figcaption section').text();
const description: string = renderDescription({
images: $el.find('a.picture img').attr('src')
? [
{
src: $el.find('a.picture img').attr('src'),
alt: title,
},
]
: undefined,
});
const linkUrl: string | undefined = $el.find('a').first().attr('href');
const authors: DataItem['author'] = $el.find('figcaption section a').contents().last().text();
const image: string | undefined = $el.find('a.picture img').attr('src');
const processedItem: DataItem = {
title,
description,
link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined,
author: authors,
content: {
html: description,
text: description,
},
image,
banner: image,
language: language as Language,
};
return processedItem;
const items: DataItem[] = response.map((item: any) => {
const title: string = item.title;
const description = renderDescription({
images: item.src ? [{ src: item.src, alt: title }] : undefined,
});
const link = `https://kpopping.com/kpics/${item.slug}`;
const dateStr = item.photoDate ?? item.createdAt;
const pubDate = dateStr ? parseDate(dateStr) : undefined;
const author = item.uploaderName || item.idolName;
const category = item.category;
items = (
await Promise.all(
items.map((item) => {
if (!item.link) {
return item;
}
return cache.tryGet(item.link, async (): Promise<DataItem> => {
const detailResponse = await ofetch(item.link!);
const $$: CheerioAPI = load(detailResponse);
const title: string = $$('h1').contents().first().text();
const description: string = renderDescription({
description: $$('div.pics').first().html() ?? undefined,
});
const pubDateStr: string | undefined = $$('meta[property="article:published_time"]').attr('content');
const categoryEls: Element[] = $$('div.buttons a').toArray();
const categories: string[] = [...new Set(categoryEls.map((el) => $$(el).text()).filter(Boolean))];
const authorEls: Element[] = $$('div.content-snippet aside:not(.like)').toArray();
const authors: DataItem['author'] = authorEls.map((authorEl) => {
const $$authorEl: Cheerio<Element> = $$(authorEl);
const $$authorAEl: Cheerio<Element> = $$authorEl.find('a').last();
return {
name: $$authorAEl.text(),
url: new URL($$authorAEl.attr('href') as string, baseUrl).href,
avatar: $$authorEl.find('img').attr('src'),
};
});
const image: string | undefined = $$('meta[name="twitter:image"]').attr('content');
const upDatedStr: string | undefined = $$('meta[property="article:modified_time"]').attr('content');
let processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate,
category: categories,
author: authors,
content: {
html: description,
text: description,
},
image,
banner: image,
updated: upDatedStr ? parseDate(upDatedStr) : item.updated,
language: language as Language,
};
const mediaEls: Element[] = $$('div.pics').first().find('img').toArray();
const medias: Record<string, Record<string, string>> = {};
let imageCount = 1;
for (const mediaEl of mediaEls) {
const $$mediaEl: Cheerio<Element> = $$(mediaEl);
const url: string | undefined = $$mediaEl.attr('src') ? new URL($$mediaEl.attr('src') as string, baseUrl).href : undefined;
if (!url) {
continue;
}
const medium = 'image';
const key = `${medium}${imageCount++}`;
medias[key] = {
url,
medium,
title: $$mediaEl.attr('alt') || title,
description: $$mediaEl.attr('alt') || title,
thumbnail: url,
};
}
if (Object.keys(medias).length > 0) {
processedItem = {
...processedItem,
media: medias,
};
}
return {
...item,
...processedItem,
};
});
})
)
).filter((_): _ is DataItem => true);
return {
title,
description,
link,
pubDate,
category,
author,
content: {
html: description,
text: description,
},
image: item.src,
banner: item.src,
};
});
return {
title: $('title').text(),
description: $('meta[property="og:description"]').attr('content'),
link: targetUrl,
title: 'kpics - kpopping',
link: 'https://kpopping.com/kpics',
item: items,
allowEmpty: true,
image: $('meta[property="og:image"]').attr('content'),
author: $('meta[property="og:site_name"]').attr('content'),
language: language as Language,
id: targetUrl,
language: 'en',
};
};
@@ -164,14 +55,14 @@ export const route: Route = {
path: '/kpics/:filter{.+}?',
name: 'Pics',
url: 'kpopping.com',
maintainers: ['nczitzk'],
maintainers: ['nczitzk', 'pinapelz'],
handler,
example: '/kpopping/kpics/gender-male/category-all/idol-any/group-any/order',
example: '/kpopping/kpics/gender=female&category=musicshow&idolId=a1664634-5caf-45d3-a57f-49d99d929aa9',
parameters: {
filter: 'Filter',
filter: 'Filter parameters in `key=value&key2=value2` format. Supported keys: `category`, `gender`, `sort`, `entityType`, `idolId`, `groupId`',
},
description: `::: tip
If you subscribe to [All male photo albums](https://kpopping.com/kpics/gender-male/category-all/idol-any/group-any/order)where the URL is \`https://kpopping.com/kpics/gender-male/category-all/idol-any/group-any/order\`, extract the part \`https://kpopping.com/kpics/\` to the end, which is \`gender-male/category-all/idol-any/group-any/order\`, and use it as the parameter to fill in. Therefore, the route will be [\`/kpopping/kpics/gender-male/category-all/idol-any/group-any/order\`](https://rsshub.app/kpopping/kpics/gender-male/category-all/idol-any/group-any/order).
Query photos using filter parameters found on kpopping such as \`idolId\`, \`groupId\`, \`gender\`, \`category\`, \`sort\`, etc.
:::`,
categories: ['picture'],
features: {
@@ -185,28 +76,24 @@ If you subscribe to [All male photo albums](https://kpopping.com/kpics/gender-ma
},
radar: [
{
source: ['kpopping.com/kpics/:filter'],
target: (params) => {
const filter: string = params.filter;
return `/kpopping/kpics${filter ? `/${filter}` : ''}`;
},
source: ['kpopping.com/kpics'],
target: '/kpics',
},
],
view: ViewType.Articles,
view: ViewType.Pictures,
zh: {
path: '/kpics/:filter{.+}?',
name: 'Pics',
url: 'kpopping.com',
maintainers: ['nczitzk'],
maintainers: ['nczitzk', 'pinapelz'],
handler,
example: '/kpopping/kpics/gender-male/category-all/idol-any/group-any/order',
example: '/kpopping/kpics/gender=female&category=musicshow&idolId=43012da1-8edb-4ca4-b060-9c0c1777c159',
parameters: {
filter: '筛选,可在对应分类页 URL 中找到',
filter: '以 `key=value&key2=value2` 格式传递的过滤参数。支持的 key 包括 `category`、`gender`、`sort`、`entityType`、`idolId`、`groupId`',
},
description: `::: tip
若订阅 [All male photo albums](https://kpopping.com/kpics/gender-male/category-all/idol-any/group-any/order),网址为 \`https://kpopping.com/kpics/gender-male/category-all/idol-any/group-any/order\`,请截取 \`https://kpopping.com/kpics/\` 到末尾的部分 \`gender-male/category-all/idol-any/group-any/order\` 作为 \`filter\` 参数填入,此时目标路由为 [\`/kpopping/kpics/gender-male/category-all/idol-any/group-any/order\`](https://rsshub.app/kpopping/kpics/gender-male/category-all/idol-any/group-any/order)
支持通过 \`idolId\`\`groupId\`\`gender\`\`category\`\`sort\` 等过滤条件获取照片
:::`,
},
};
-192
View File
@@ -1,192 +0,0 @@
import type { Cheerio, CheerioAPI } from 'cheerio';
import { load } from 'cheerio';
import type { Element } from 'domhandler';
import type { Context } from 'hono';
import type { Data, DataItem, Language, Route } from '@/types';
import { ViewType } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { renderDescription } from './templates/description';
export const handler = async (ctx: Context): Promise<Data> => {
const { filter } = ctx.req.param();
const limit = Number(ctx.req.query('limit') ?? '2');
const baseUrl = 'https://kpopping.com';
const targetUrl: string = new URL(`news${filter ? `/${filter}` : ''}`, baseUrl).href;
const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language = $('html').attr('lang') ?? 'en';
let items: DataItem[] = $('section.news-list-item')
.slice(0, limit)
.toArray()
.map((el) => {
const $el: Cheerio<Element> = $(el);
const $aEl: Cheerio<Element> = $el.find('h4.title-wr a').last();
const title: string = $aEl.text();
const pubDateStr: string | undefined = $el.find('time.datetime-wr').attr('datetime');
const linkUrl: string | undefined = $aEl.attr('href');
const categoryEls: Array<Cheerio<Element>> = [$el.find('h4.title-wr a').first()];
const categories: string[] = [...new Set(categoryEls.map((el) => $(el).text()).filter(Boolean))];
const authorEls: Element[] = $el.find('aside.author-wr').toArray();
const authors: DataItem['author'] = authorEls.map((authorEl) => {
const $authorEl: Cheerio<Element> = $(authorEl);
const $authorAEl: Cheerio<Element> = $authorEl.find('a').last();
return {
name: $authorAEl.text(),
url: new URL($authorAEl.attr('href') as string, baseUrl).href,
avatar: new URL($el.find('aside.author-wr a img').attr('src') as string, baseUrl).href,
};
});
const upDatedStr: string | undefined = pubDateStr;
const processedItem: DataItem = {
title,
pubDate: pubDateStr ? timezone(parseDate(pubDateStr, 'MMM D, YYYY h:mma'), 8) : undefined,
link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined,
category: categories,
author: authors,
doi: $el.find('meta[name="citation_doi"]').attr('content'),
updated: upDatedStr ? timezone(parseDate(upDatedStr, 'MMM D, YYYY h:mma'), 8) : undefined,
language: language as Language,
};
return processedItem;
});
items = (
await Promise.all(
items.map((item) => {
if (!item.link) {
return item;
}
return cache.tryGet(item.link, async (): Promise<DataItem> => {
const detailResponse = await ofetch(item.link!);
const $$: CheerioAPI = load(detailResponse);
const title: string = $$('h1').contents().first().text();
const description: string = renderDescription({
images: $$('figure.opening img').attr('src')
? [
{
src: new URL($$('figure.opening img').attr('src') as string, baseUrl).href,
alt: title,
},
]
: undefined,
description: $$('div#article-content').html() ?? undefined,
});
const pubDateStr: string | undefined = $$('meta[property="article:published_time"]').attr('content');
const categoryEls: Element[] = $$('aside.info a, div.supplements a.item').toArray();
const categories: string[] = [...new Set(categoryEls.map((el) => $$(el).text()?.trim()).filter(Boolean))];
const authorEls: Element[] = $$('div.content-snippet aside:not(.like)').toArray();
const authors: DataItem['author'] = authorEls.map((authorEl) => {
const $$authorEl: Cheerio<Element> = $$(authorEl);
const $$authorAEl: Cheerio<Element> = $$authorEl.find('a').last();
return {
name: $$authorAEl.text(),
url: new URL($$authorAEl.attr('href') as string, baseUrl).href,
avatar: $$authorEl.find('img').attr('src'),
};
});
const image: string | undefined = $$('meta[property="og:image"]').attr('content');
const upDatedStr: string | undefined = $$('meta[property="article:modified_time"]').attr('content');
const processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate,
category: categories,
author: authors,
content: {
html: description,
text: description,
},
image,
banner: image,
updated: upDatedStr ? parseDate(upDatedStr) : item.updated,
language: language as Language,
};
return {
...item,
...processedItem,
};
});
})
)
).filter((_): _ is DataItem => true);
return {
title: $('title').text(),
description: $('meta[property="og:description"]').attr('content'),
link: targetUrl,
item: items,
allowEmpty: true,
image: $('meta[property="og:image"]').attr('content'),
author: $('meta[property="og:site_name"]').attr('content'),
language: language as Language,
id: targetUrl,
};
};
export const route: Route = {
path: '/news/:filter{.+}?',
name: 'News',
url: 'kpopping.com',
maintainers: ['nczitzk'],
handler,
example: '/kpopping/news/gender-all/category-all/idol-any/group-any/order',
parameters: {
filter: 'Filter',
},
description: `::: tip
If you subscribe to [All male articles](https://kpopping.com/news/gender-male/category-all/idol-any/group-any/order)where the URL is \`https://kpopping.com/news/gender-male/category-all/idol-any/group-any/order\`, extract the part \`https://kpopping.com/news\` to the end, which is \`gender-male/category-all/idol-any/group-any/order\`, and use it as the parameter to fill in. Therefore, the route will be [\`/kpopping/news/gender-male/category-all/idol-any/group-any/order\`](https://rsshub.app/kpopping/news/gender-male/category-all/idol-any/group-any/order).
:::`,
categories: ['new-media'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['kpopping.com/news/:filter'],
target: (params) => {
const filter: string = params.filter;
return `/kpopping/news${filter ? `/${filter}` : ''}`;
},
},
],
view: ViewType.Articles,
zh: {
path: '/news/:filter{.+}?',
name: 'News',
url: 'kpopping.com',
maintainers: ['nczitzk'],
handler,
example: '/kpopping/news/gender-all/category-all/idol-any/group-any/order',
parameters: {
filter: '筛选,可在对应分类页 URL 中找到',
},
description: `::: tip
若订阅 [All male articles](https://kpopping.com/news/gender-male/category-all/idol-any/group-any/order),网址为 \`https://kpopping.com/news/gender-male/category-all/idol-any/group-any/order\`,请截取 \`https://kpopping.com/news/\` 到末尾的部分 \`gender-male/category-all/idol-any/group-any/order\` 作为 \`filter\` 参数填入,此时目标路由为 [\`/kpopping/news/gender-male/category-all/idol-any/group-any/order\`](https://rsshub.app/kpopping/news/gender-male/category-all/idol-any/group-any/order)。
:::`,
},
};