From ec0d80d91f21d53262952ba1dc47bbbdd24cc8d8 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 3 Nov 2025 04:18:27 +0800 Subject: [PATCH] fix(route/tophub): fix selectors (#20408) * fix(route/tophub): fix selectors * fix(route/tophub): include image in response --- lib/routes/tophub/index.ts | 19 +++++++++++-------- lib/routes/tophub/list.ts | 24 ++++++++++++++---------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/routes/tophub/index.ts b/lib/routes/tophub/index.ts index e98709d68..a753fa47e 100644 --- a/lib/routes/tophub/index.ts +++ b/lib/routes/tophub/index.ts @@ -1,5 +1,5 @@ import { Route } from '@/types'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { config } from '@/config'; @@ -36,28 +36,31 @@ async function handler(ctx) { const id = ctx.req.param('id'); const link = `https://tophub.today/n/${id}`; - const response = await got.get(link, { + const response = await ofetch(link, { headers: { Referer: 'https://tophub.today', - Cookie: config.tophub.cookie, + Cookie: config.tophub?.cookie ?? '', }, }); - const $ = load(response.data); + const $ = load(response); - const title = $('div.Xc-ec-L.b-L').text().trim(); + const title = $('.tt h3').text().trim(); - const out = $('div.Zd-p-Sc > div:nth-child(1) tr') + const out = $('.rank-all-item:not(.history-content) .jc-c tr') .toArray() .map((e) => { const info = { - title: $(e).find('td.al a').text(), - link: $(e).find('td.al a').attr('href'), + title: $(e).find('td a').first().text(), + link: $(e).find('td a').first().attr('href'), + description: $(e).find('.ws').text().trim(), }; return info; }); return { title, + description: $('.tt p').text().trim(), + image: $('.ii img').attr('src'), link, item: out, }; diff --git a/lib/routes/tophub/list.ts b/lib/routes/tophub/list.ts index 0bf0229a0..9ce023004 100644 --- a/lib/routes/tophub/list.ts +++ b/lib/routes/tophub/list.ts @@ -1,10 +1,11 @@ import { Route } from '@/types'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { config } from '@/config'; import path from 'node:path'; import { art } from '@/utils/render'; +import xxhash from 'xxhash-wasm'; export const route: Route = { path: '/list/:id', @@ -34,26 +35,27 @@ export const route: Route = { maintainers: ['akynazh'], handler, description: `::: tip -将榜单条目集合到一个列表中,可避免推送大量条目,更符合阅读习惯且有热度排序,推荐使用。 +将榜单条目集合到一个列表中,且有热度排序,可避免推送大量条目。 :::`, }; async function handler(ctx) { + const { h64ToString } = await xxhash(); const id = ctx.req.param('id'); const link = `https://tophub.today/n/${id}`; - const response = await got.get(link, { + const response = await ofetch(link, { headers: { Referer: 'https://tophub.today', - Cookie: config.tophub.cookie, + Cookie: config.tophub?.cookie ?? '', }, }); - const $ = load(response.data); - const title = $('div.Xc-ec-L.b-L').text().trim(); - const items = $('div.Zd-p-Sc > div:nth-child(1) tr') + const $ = load(response); + const title = $('.tt h3').text().trim(); + const items = $('.rank-all-item:not(.history-content) .jc-c tr') .toArray() .map((e) => ({ - title: $(e).find('td.al a').text().trim(), - link: $(e).find('td.al a').attr('href'), + title: $(e).find('td a').text().trim(), + link: $(e).find('td a').attr('href'), heatRate: $(e).find('td:nth-child(3)').text().trim(), })); const combinedTitles = items.map((item) => item.title).join(''); @@ -61,13 +63,15 @@ async function handler(ctx) { return { title, + description: $('.tt p').text().trim(), + image: $('.ii img').attr('src'), link, item: [ { title, link, description: renderRank, - guid: combinedTitles, + guid: h64ToString(combinedTitles), }, ], };