diff --git a/lib/routes/cosplaytele/article.ts b/lib/routes/cosplaytele/article.ts index c5a534cce..00db9197e 100644 --- a/lib/routes/cosplaytele/article.ts +++ b/lib/routes/cosplaytele/article.ts @@ -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, }; } diff --git a/lib/routes/cosplaytele/category.ts b/lib/routes/cosplaytele/category.ts index 755f01fd3..6062ef26a 100644 --- a/lib/routes/cosplaytele/category.ts +++ b/lib/routes/cosplaytele/category.ts @@ -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)); + }) + ), }; } diff --git a/lib/routes/cosplaytele/latest.ts b/lib/routes/cosplaytele/latest.ts index b986a8d73..fe5d0b000 100644 --- a/lib/routes/cosplaytele/latest.ts +++ b/lib/routes/cosplaytele/latest.ts @@ -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)); + }) + ), }; } diff --git a/lib/routes/cosplaytele/popular.ts b/lib/routes/cosplaytele/popular.ts index fe2b20577..fc590cc17 100644 --- a/lib/routes/cosplaytele/popular.ts +++ b/lib/routes/cosplaytele/popular.ts @@ -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)))), }; } diff --git a/lib/routes/cosplaytele/tag.ts b/lib/routes/cosplaytele/tag.ts index 41676a57c..a1e11d467 100644 --- a/lib/routes/cosplaytele/tag.ts +++ b/lib/routes/cosplaytele/tag.ts @@ -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)); + }) + ), }; }