feat(route): add route "CosplayTele" (#18217)
* feat(route): add route "CosplayTele" * feat(route/cosplaytele): add route "popular" * refactor(route/cosplaytele): optimize article loading with WP API * feat(route/cosplaytele): add route "tag" * perf(route/cosplaytele): optimize popular posts retrieval
This commit is contained in:
parent
07f728ba9a
commit
091feba32a
|
|
@ -0,0 +1,13 @@
|
|||
import { parseDate } from '@/utils/parse-date';
|
||||
import { WPPost } from './types';
|
||||
|
||||
function loadArticle(item: WPPost) {
|
||||
return {
|
||||
title: item.title.rendered,
|
||||
description: item.content.rendered,
|
||||
pubDate: parseDate(item.date_gmt),
|
||||
link: item.link,
|
||||
};
|
||||
}
|
||||
|
||||
export default loadArticle;
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import { SUB_NAME_PREFIX, SUB_URL } from './const';
|
||||
import loadArticle from './article';
|
||||
import { WPPost } from './types';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/category/:category',
|
||||
categories: ['picture'],
|
||||
example: '/cosplaytele/category/cosplay',
|
||||
parameters: { category: 'Category' },
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['cosplaytele.com/category/:category'],
|
||||
target: '/category/:category',
|
||||
},
|
||||
],
|
||||
name: 'Category',
|
||||
maintainers: ['AiraNadih'],
|
||||
handler,
|
||||
url: 'cosplaytele.com/',
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const limit = Number.parseInt(ctx.req.query('limit')) || 20;
|
||||
const category = ctx.req.param('category');
|
||||
const categoryUrl = `${SUB_URL}category/${category}/`;
|
||||
|
||||
const {
|
||||
data: [{ id: categoryId }],
|
||||
} = await got(`${SUB_URL}wp-json/wp/v2/categories?slug=${category}`);
|
||||
const { data: posts } = await got(`${SUB_URL}wp-json/wp/v2/posts?categories=${categoryId}&per_page=${limit}`);
|
||||
|
||||
return {
|
||||
title: `${SUB_NAME_PREFIX} - Category: ${category}`,
|
||||
link: categoryUrl,
|
||||
item: posts.map((post) => loadArticle(post as WPPost)),
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
const SUB_NAME_PREFIX = 'CosplayTele';
|
||||
const SUB_URL = 'https://cosplaytele.com/';
|
||||
|
||||
export { SUB_NAME_PREFIX, SUB_URL };
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import { SUB_NAME_PREFIX, SUB_URL } from './const';
|
||||
import loadArticle from './article';
|
||||
import { WPPost } from './types';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/',
|
||||
categories: ['picture'],
|
||||
example: '/cosplaytele',
|
||||
parameters: {},
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['cosplaytele.com/'],
|
||||
target: '',
|
||||
},
|
||||
],
|
||||
name: 'Latest',
|
||||
maintainers: ['AiraNadih'],
|
||||
handler,
|
||||
url: 'cosplaytele.com/',
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const limit = Number.parseInt(ctx.req.query('limit')) || 20;
|
||||
const { data: posts } = await got(`${SUB_URL}wp-json/wp/v2/posts?per_page=${limit}`);
|
||||
|
||||
return {
|
||||
title: `${SUB_NAME_PREFIX} - Latest`,
|
||||
link: SUB_URL,
|
||||
item: posts.map((post) => loadArticle(post as WPPost)),
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'CosplayTele',
|
||||
url: 'cosplaytele.com',
|
||||
description: 'Cosplaytele - Fast - Security - Free',
|
||||
lang: 'en',
|
||||
};
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import { SUB_NAME_PREFIX, SUB_URL } from './const';
|
||||
import loadArticle from './article';
|
||||
import { WPPost } from './types';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/popular/:period',
|
||||
categories: ['picture'],
|
||||
example: '/cosplaytele/popular/3',
|
||||
parameters: { period: 'Days' },
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['cosplaytele.com/:period'],
|
||||
target: '/popular/:period',
|
||||
},
|
||||
],
|
||||
name: 'Popular',
|
||||
maintainers: ['AiraNadih'],
|
||||
handler,
|
||||
url: 'cosplaytele.com/',
|
||||
};
|
||||
|
||||
function getPeriodConfig(period) {
|
||||
if (period === '1') {
|
||||
return {
|
||||
url: `${SUB_URL}24-hours/`,
|
||||
range: 'daily',
|
||||
title: `${SUB_NAME_PREFIX} - Top views in 24 hours`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
url: `${SUB_URL}${period}-day/`,
|
||||
range: `last${period}days`,
|
||||
title: `${SUB_NAME_PREFIX} - Top views in ${period} days`,
|
||||
};
|
||||
}
|
||||
|
||||
async function handler(ctx) {
|
||||
const limit = Number.parseInt(ctx.req.query('limit')) || 20;
|
||||
const period = ctx.req.param('period');
|
||||
|
||||
const { url, range, title } = getPeriodConfig(period);
|
||||
|
||||
const { data } = await got.post(`${SUB_URL}wp-json/wordpress-popular-posts/v2/widget`, {
|
||||
json: {
|
||||
limit,
|
||||
range,
|
||||
order_by: 'views',
|
||||
},
|
||||
});
|
||||
|
||||
const $ = load(data.widget);
|
||||
const links = $('.wpp-list li')
|
||||
.toArray()
|
||||
.map((post) => $(post).find('.wpp-post-title').attr('href'))
|
||||
.filter((link) => link !== undefined);
|
||||
const slugs = links.map((link) => link.split('/').findLast(Boolean));
|
||||
const { data: posts } = await got(`${SUB_URL}wp-json/wp/v2/posts?slug=${slugs.join(',')}&per_page=${limit}`);
|
||||
|
||||
return {
|
||||
title,
|
||||
link: url,
|
||||
item: posts.map((post) => loadArticle(post as WPPost)),
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import { SUB_NAME_PREFIX, SUB_URL } from './const';
|
||||
import loadArticle from './article';
|
||||
import { WPPost } from './types';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/tag/:tag',
|
||||
categories: ['picture'],
|
||||
example: '/cosplaytele/tag/aqua',
|
||||
parameters: { tag: 'Tag' },
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['cosplaytele.com/tag/:tag'],
|
||||
target: '/tag/:tag',
|
||||
},
|
||||
],
|
||||
name: 'Tag',
|
||||
maintainers: ['AiraNadih'],
|
||||
handler,
|
||||
url: 'cosplaytele.com/',
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const limit = Number.parseInt(ctx.req.query('limit')) || 20;
|
||||
const tag = ctx.req.param('tag');
|
||||
const tagUrl = `${SUB_URL}tag/${tag}/`;
|
||||
|
||||
const {
|
||||
data: [{ id: tagId }],
|
||||
} = await got(`${SUB_URL}wp-json/wp/v2/tags?slug=${tag}`);
|
||||
const { data: posts } = await got(`${SUB_URL}wp-json/wp/v2/posts?tags=${tagId}&per_page=${limit}`);
|
||||
|
||||
return {
|
||||
title: `${SUB_NAME_PREFIX} - Tag: ${tag}`,
|
||||
link: tagUrl,
|
||||
item: posts.map((post) => loadArticle(post as WPPost)),
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
interface WPPost {
|
||||
title: {
|
||||
rendered: string;
|
||||
};
|
||||
content: {
|
||||
rendered: string;
|
||||
};
|
||||
date_gmt: string;
|
||||
link: string;
|
||||
}
|
||||
|
||||
export type { WPPost };
|
||||
Loading…
Reference in New Issue