From 385759d7b170f27d6ca411baa1b887177dede3cf Mon Sep 17 00:00:00 2001 From: Yuliang Tan Date: Sat, 8 Mar 2025 12:03:12 +0800 Subject: [PATCH] fix(route): discuz use ofetch (#18543) * fix(route): discuz use ofetch * fix(route): discuz use ofetch with charset handling --- lib/routes/discuz/discuz.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/routes/discuz/discuz.ts b/lib/routes/discuz/discuz.ts index e68a744856..217590169b 100644 --- a/lib/routes/discuz/discuz.ts +++ b/lib/routes/discuz/discuz.ts @@ -1,6 +1,6 @@ import { Route } from '@/types'; import cache from '@/utils/cache'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import iconv from 'iconv-lite'; import { parseDate } from '@/utils/parse-date'; @@ -22,14 +22,13 @@ function fixUrl(itemLink, baseUrl) { // discuz 7.x 与 discuz x系列 通用文章内容抓取 async function loadContent(itemLink, charset, header) { // 处理编码问题 - const response = await got({ + const response = await ofetch.raw(itemLink, { method: 'get', - url: itemLink, - responseType: 'buffer', + responseType: 'arrayBuffer', headers: header, }); - const responseData = iconv.decode(response.data, charset ?? 'utf-8'); + const responseData = iconv.decode(Buffer.from(response._data), charset ?? 'utf-8'); if (!responseData) { const description = '获取详细内容失败'; return { description }; @@ -77,14 +76,13 @@ async function handler(ctx) { Cookie: cookie, }; - const response = await got({ + const response = await ofetch.raw(link, { method: 'get', - url: link, - responseType: 'buffer', + responseType: 'arrayBuffer', headers: header, }); - const responseData = response.data; + const responseData = Buffer.from(response._data); // 若没有指定编码,则默认utf-8 const contentType = response.headers['content-type'] || ''; let $ = load(iconv.decode(responseData, 'utf-8'));