feat: use ofetch in parameter common-config wechat-mp

This commit is contained in:
DIYgod
2024-03-27 14:40:42 +08:00
parent eec2a71953
commit 8c0534393f
3 changed files with 17 additions and 20 deletions
+5 -6
View File
@@ -1,7 +1,7 @@
import * as entities from 'entities';
import { load, type CheerioAPI, type Element } from 'cheerio';
import { simplecc } from 'simplecc-wasm';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { config } from '@/config';
import { RE2JS } from 're2js';
import markdownit from 'markdown-it';
@@ -34,8 +34,9 @@ const resolveRelativeLink = ($: CheerioAPI, elem: Element, attr: string, baseUrl
const summarizeArticle = async (articleText: string) => {
const apiUrl = `${config.openai.endpoint}/chat/completions`;
const response = await got.post(apiUrl, {
json: {
const response = await ofetch(apiUrl, {
method: 'POST',
body: {
model: config.openai.model,
max_tokens: config.openai.maxTokens,
messages: [
@@ -49,7 +50,6 @@ const summarizeArticle = async (articleText: string) => {
},
});
// @ts-expect-error custom field
return response.data.choices[0].message.content;
};
@@ -305,8 +305,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
if (link) {
// if parser failed, return default description and not report error
try {
// @ts-expect-error custom field
const { data: res } = await got(link);
const res = await ofetch(link);
const $ = load(res);
const result = await Parser.parse(link, {
html: $.html(),
+6 -6
View File
@@ -1,5 +1,5 @@
import cheerio from 'cheerio';
import got from '@/utils/got';
import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';
import iconv from 'iconv-lite';
function transElemText($, prop) {
@@ -37,8 +37,8 @@ function getProp(data, prop, $) {
}
async function buildData(data) {
const response = await got.get(data.url);
const contentType = response.headers['content-type'] || '';
const response = await ofetch.raw(data.url);
const contentType = response.headers.get('content-type') || '';
// 若没有指定编码,则默认utf-8
let charset = 'utf-8';
for (const attr of contentType.split(';')) {
@@ -47,8 +47,8 @@ async function buildData(data) {
}
}
// @ts-expect-error custom property
const responseData = charset === 'utf-8' ? response.data : iconv.decode((await got.get({ url: data.url, responseType: 'buffer' })).data, charset);
const $ = cheerio.load(responseData);
const responseData = charset === 'utf-8' ? response._data : iconv.decode(await ofetch(data.url, { responseType: 'buffer' }), charset);
const $ = load(responseData);
const $item = $(data.item.item);
// 这里应该是可以通过参数注入一些代码的,不过应该无伤大雅
return {
+6 -8
View File
@@ -25,7 +25,7 @@
* For more details of these functions, please refer to the jsDoc in the source code.
*/
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load, type Cheerio, type Element } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';
@@ -196,9 +196,8 @@ const normalizeUrl = (url, bypassHostCheck = false) => {
const fetchArticle = (url, bypassHostCheck = false) => {
url = normalizeUrl(url, bypassHostCheck);
return cache.tryGet(url, async () => {
const response = await got(url);
// @ts-expect-error custom field
const $ = load(response.data);
const data = await ofetch(url);
const $ = load(data);
const title = ($('meta[property="og:title"]').attr('content') || '').replaceAll('\\r', '').replaceAll('\\n', ' ');
const author = $('meta[name=author]').attr('content');
@@ -209,9 +208,8 @@ const fetchArticle = (url, bypassHostCheck = false) => {
const originalUrl = detectOriginalArticleUrl($);
if (originalUrl) {
// try to fetch the description from the original article
const originalResponse = await got(normalizeUrl(originalUrl, bypassHostCheck));
// @ts-expect-error custom field
const original$ = load(originalResponse.data);
const data = await ofetch(normalizeUrl(originalUrl, bypassHostCheck));
const original$ = load(data);
description += fixArticleContent(original$('#js_content'));
}
@@ -230,7 +228,7 @@ const fetchArticle = (url, bypassHostCheck = false) => {
let mpName = $('.profile_nickname').first().text();
mpName = mpName && mpName.trim();
return { title, author, description, summary, pubDate, mpName, link: response.url };
return { title, author, description, summary, pubDate, mpName, link: url };
}) as Promise<{
title: string;
author: string;