fix(route/cosplaytele): revert to iterative content fetching due to WP JSON API being disabled (#18964)

Co-authored-by: AiraNadih <AiraNadih@users.noreply.github.com>
This commit is contained in:
AiraNadih 2025-04-30 05:46:54 +08:00 committed by GitHub
parent f19a725157
commit e5df827aee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 25 deletions

View File

@ -1,12 +1,20 @@
import { load } from 'cheerio';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { WPPost } from './types';
function loadArticle(item: WPPost) {
async function loadArticle(link) {
const resp = await got(link);
const article = load(resp.body);
const title = article('h1.entry-title').text().trim();
const description = article('.entry-content').html() ?? '';
const pubDate = parseDate(article('time')[0].attribs.datetime);
return {
title: item.title.rendered,
description: item.content.rendered,
pubDate: parseDate(item.date_gmt),
link: item.link,
title,
description,
pubDate,
link,
};
}

View File

@ -1,8 +1,9 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
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: '/category/:category',
@ -34,14 +35,19 @@ async function handler(ctx) {
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}`);
const response = await got(categoryUrl);
const $ = load(response.body);
const itemRaw = $('#content .post-item').slice(0, limit).toArray();
return {
title: `${SUB_NAME_PREFIX} - Category: ${category}`,
link: categoryUrl,
item: posts.map((post) => loadArticle(post as WPPost)),
item: await Promise.all(
itemRaw.map((e) => {
const item = $(e);
const link = item.find('h5.post-title a').attr('href');
return cache.tryGet(link, () => loadArticle(link));
})
),
};
}

View File

@ -1,8 +1,9 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
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: '/',
@ -31,11 +32,19 @@ export const route: Route = {
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}`);
const response = await got(SUB_URL);
const $ = load(response.body);
const itemRaw = $('#content .post-item').slice(0, limit).toArray();
return {
title: `${SUB_NAME_PREFIX} - Latest`,
link: SUB_URL,
item: posts.map((post) => loadArticle(post as WPPost)),
item: await Promise.all(
itemRaw.map((e) => {
const item = $(e);
const link = item.find('h5.post-title a').attr('href');
return cache.tryGet(link, () => loadArticle(link));
})
),
};
}

View File

@ -1,9 +1,9 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
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',
@ -64,12 +64,10 @@ async function handler(ctx) {
.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)),
item: await Promise.all(links.map((link) => cache.tryGet(link, () => loadArticle(link)))),
};
}

View File

@ -1,8 +1,9 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
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: '/tag/:tag',
@ -34,14 +35,19 @@ async function handler(ctx) {
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}`);
const response = await got(tagUrl);
const $ = load(response.body);
const itemRaw = $('#content .post-item').slice(0, limit).toArray();
return {
title: `${SUB_NAME_PREFIX} - Tag: ${tag}`,
link: tagUrl,
item: posts.map((post) => loadArticle(post as WPPost)),
item: await Promise.all(
itemRaw.map((e) => {
const item = $(e);
const link = item.find('h5.post-title a').attr('href');
return cache.tryGet(link, () => loadArticle(link));
})
),
};
}