Skip to content

Commit

Permalink
feat(route): add route "CosplayTele" (#18217)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AiraNadih authored Jan 26, 2025
1 parent 07f728b commit 091feba
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/routes/cosplaytele/article.ts
Original file line number Diff line number Diff line change
@@ -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;
47 changes: 47 additions & 0 deletions lib/routes/cosplaytele/category.ts
Original file line number Diff line number Diff line change
@@ -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)),
};
}
4 changes: 4 additions & 0 deletions lib/routes/cosplaytele/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const SUB_NAME_PREFIX = 'CosplayTele';
const SUB_URL = 'https://cosplaytele.com/';

export { SUB_NAME_PREFIX, SUB_URL };
41 changes: 41 additions & 0 deletions lib/routes/cosplaytele/latest.ts
Original file line number Diff line number Diff line change
@@ -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)),
};
}
8 changes: 8 additions & 0 deletions lib/routes/cosplaytele/namespace.ts
Original file line number Diff line number Diff line change
@@ -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',
};
75 changes: 75 additions & 0 deletions lib/routes/cosplaytele/popular.ts
Original file line number Diff line number Diff line change
@@ -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)),
};
}
47 changes: 47 additions & 0 deletions lib/routes/cosplaytele/tag.ts
Original file line number Diff line number Diff line change
@@ -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)),
};
}
12 changes: 12 additions & 0 deletions lib/routes/cosplaytele/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
interface WPPost {
title: {
rendered: string;
};
content: {
rendered: string;
};
date_gmt: string;
link: string;
}

export type { WPPost };

0 comments on commit 091feba

Please sign in to comment.