feat(route): MissKON (#17178)

This commit is contained in:
Urabartin
2024-10-18 23:18:59 +08:00
committed by GitHub
parent d737248165
commit 6355d535cc
5 changed files with 185 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'MissKON',
url: 'misskon.com',
};
+33
View File
@@ -0,0 +1,33 @@
import { Route } from '@/types';
import { ENDPOINT, getPosts } from './utils';
export const route: Route = {
path: '/posts/:routeParams?',
categories: ['picture'],
example: '/misskon/posts/search=video&tags_exclude=353,3100&per_page=5',
parameters: { routeParams: 'Additional parameters for filtering posts, refer to [WordPress API Reference](https://developer.wordpress.org/rest-api/reference/posts/#arguments) for details.' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['misskon.com/'],
target: '/posts',
},
],
name: 'Posts',
maintainers: ['Urabartin'],
handler: async (ctx) => {
const { routeParams = '' } = ctx.req.param();
return {
title: `MissKON - ${routeParams || 'Posts'}`,
link: `${ENDPOINT}/posts` + (routeParams ? `?${routeParams}` : ''),
item: await getPosts(routeParams),
};
},
};
+38
View File
@@ -0,0 +1,38 @@
import { Route } from '@/types';
import { getPosts, getTags } from './utils';
export const route: Route = {
path: '/tag/:tag',
categories: ['picture'],
example: '/misskon/tag/cosplay',
parameters: { tag: 'Any tag that exists in MissKon' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['misskon.com/tag/:tag/'],
target: '/tag/:tag',
},
],
name: 'Tag',
maintainers: ['Urabartin'],
handler: async (ctx) => {
const { tag } = ctx.req.param();
const tagData = await getTags(tag);
const searchParams = new URLSearchParams();
searchParams.set('tags', tagData.id);
const items = await getPosts(searchParams.toString());
return {
title: `MissKON - ${tagData.name}`,
link: tagData.link,
description: tagData.description,
item: items,
};
},
};
+67
View File
@@ -0,0 +1,67 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { getPosts } from './utils';
export const route: Route = {
path: '/top/:k',
categories: ['picture'],
example: '/misskon/top/60',
parameters: { k: 'Top k days, can be 3, 7, 30 or 60' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
title: 'Top 3 days',
source: ['misskon.com/top3/'],
target: '/top/3',
},
{
title: 'Top 7 days',
source: ['misskon.com/top7/'],
target: '/top/7',
},
{
title: 'Top 30 days',
source: ['misskon.com/top30/'],
target: '/top/30',
},
{
title: 'Top 60 days',
source: ['misskon.com/top60/'],
target: '/top/60',
},
],
name: 'Top k days',
maintainers: ['Urabartin'],
handler: async (ctx) => {
const { k } = ctx.req.param();
if (!['3', '7', '30', '60'].includes(k)) {
throw new Error(`Invalid k: k=${k}`);
}
const topLink = `https://misskon.com/top${k}/`;
const response = await ofetch(topLink);
const $ = load(response);
const feedTitle = $('.page-title').text();
const feedDesc = $('.content > p').first().text();
const itemSlugs = $('#main-content article.item-list > h2 a')
.toArray()
.map((link) => new URL($(link).attr('href') || '').pathname.slice(1, -1));
const searchParams = new URLSearchParams();
searchParams.set('slug', itemSlugs.join(','));
searchParams.set('per_page', itemSlugs.length.toString());
return {
title: `MissKON - ${feedTitle}`,
link: topLink,
description: feedDesc,
item: await getPosts(searchParams),
};
},
};
+41
View File
@@ -0,0 +1,41 @@
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
const ENDPOINT = 'https://misskon.com/wp-json/wp/v2';
const getPosts = async (searchParams) => {
const url = new URL(`${ENDPOINT}/posts?${searchParams}`);
url.searchParams.append('_embed', 'wp:term');
const data = await ofetch(url.href);
return data.map((item) => {
const $ = load(item.content.rendered);
$('input').each(function () {
$(this).replaceWith($(this).attr('value') || '');
});
$('script').remove();
return {
title: item.title.rendered,
link: item.link,
description: $.html(),
pubDate: timezone(parseDate(item.date_gmt), 0),
category: item._embedded['wp:term']
.flat()
.filter((x) => x.taxonomy === 'post_tag')
.map((x) => x.name),
};
});
};
const getTags = async (slug) => {
const data = await ofetch(`${ENDPOINT}/tags?slug=${slug}`);
if (data.length === 0) {
throw new Error(`Invalid tag slug: ${slug}`);
}
return {
id: data[0].id,
name: data[0].name,
link: data[0].link,
description: data[0].description,
};
};
export { ENDPOINT, getPosts, getTags };