From 07f728ba9a13cb3e6638c9e4b933c331424a10f0 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 27 Jan 2025 02:29:05 +0800 Subject: [PATCH] fix(route): 2048 jump logic (#18221) --- lib/routes/2048/index.ts | 57 +++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/routes/2048/index.ts b/lib/routes/2048/index.ts index 4aede42b464378..df17b1d8e34be8 100644 --- a/lib/routes/2048/index.ts +++ b/lib/routes/2048/index.ts @@ -3,13 +3,12 @@ import { getCurrentPath } from '@/utils/helpers'; const __dirname = getCurrentPath(import.meta.url); import cache from '@/utils/cache'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import timezone from '@/utils/timezone'; import { parseDate } from '@/utils/parse-date'; import { art } from '@/utils/render'; import path from 'node:path'; -import ofetch from '@/utils/ofetch'; export const route: Route = { path: '/:id?', @@ -65,25 +64,37 @@ export const route: Route = { }; async function handler(ctx) { - const id = ctx.req.param('id') ?? '2'; + const id = ctx.req.param('id') ?? '3'; const rootUrl = 'https://hjd2048.com'; - const entranceDomain = await cache.tryGet('2048:entranceDomain', async () => { - const { data: response } = await got('https://hjd.tw', { + const domainInfo = (await cache.tryGet('2048:domainInfo', async () => { + const response = await ofetch('https://hjd.tw', { headers: { accept: '*/*', }, }); - const $ = load(response); - const targetLink = new URL($('table.group-table tr').eq(1).find('td a').eq(0).attr('href')).href; - return targetLink; + let $ = load(response); + const targetLink = new URL($('.address-list a').eq(0).attr('href'), 'https://hjd.tw').href; + const redirectResponse = await ofetch.raw(targetLink); + $ = load(redirectResponse._data); + return { + url: redirectResponse.url, + cookie: + $('script') + .text() + .match(/var safeid='(.*?)',/)?.[1] ?? '', + }; + })) as { url: string; cookie: string }; + + const currentUrl = `${domainInfo.url}thread.php?fid-${id}.html`; + + const response = await ofetch.raw(currentUrl, { + headers: { + cookie: `_safe=${domainInfo.cookie}`, + }, }); - const currentUrl = `${entranceDomain}2048/thread.php?fid-${id}.html`; - - const response = await ofetch.raw(currentUrl); - const $ = load(response._data); const currentHost = `https://${new URL(response.url).host}`; // redirected host @@ -99,7 +110,7 @@ async function handler(ctx) { return { title: item.text(), - link: `${currentHost}/2048/${item.attr('href')}`, + link: `${currentHost}/${item.attr('href')}`, guid: `${rootUrl}/2048/${item.attr('href')}`, }; }) @@ -108,12 +119,13 @@ async function handler(ctx) { const items = await Promise.all( list.map((item) => cache.tryGet(item.guid, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, + const detailResponse = await ofetch(item.link, { + headers: { + cookie: `_safe=${domainInfo.cookie}`, + }, }); - const content = load(detailResponse.data); + const content = load(detailResponse); content('.ads, .tips').remove(); @@ -126,17 +138,14 @@ async function handler(ctx) { const downloadLink = content('#read_tpc').first().find('a').last(); const copyLink = content('#copytext')?.first()?.text(); - if (downloadLink?.text()?.startsWith('http') && /down2048\.com$/.test(new URL(downloadLink.text()).hostname)) { - const torrentResponse = await got({ - method: 'get', - url: downloadLink.text(), - }); + if (downloadLink?.text()?.startsWith('http') && /bt\.azvmw\.com$/.test(new URL(downloadLink.text()).hostname)) { + const torrentResponse = await ofetch(downloadLink.text()); - const torrent = load(torrentResponse.data); + const torrent = load(torrentResponse); item.enclosure_type = 'application/x-bittorrent'; const ahref = torrent('.uk-button').last().attr('href'); - item.enclosure_url = ahref?.startsWith('http') ? ahref : `https://data.datapps.org/${ahref}`; + item.enclosure_url = ahref?.startsWith('http') ? ahref : `https://bt.azvmw.com/${ahref}`; const magnet = torrent('.uk-button').first().attr('href');