refactor(route/telegram): use ofetch in telegram routes

This commit is contained in:
pseudoyu 2024-10-25 17:07:21 +08:00
parent 54083d0ba8
commit a19dc7df73
No known key found for this signature in database
3 changed files with 13 additions and 13 deletions

View File

@ -1,7 +1,7 @@
import { Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import * as cheerio from 'cheerio';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
@ -32,8 +32,8 @@ export const route: Route = {
async function handler() {
const link = 'https://telegram.org/blog';
const res = await got(link);
const $$ = cheerio.load(res.body);
const res = await ofetch(link);
const $$ = cheerio.load(res);
const items = await Promise.all(
$$('.dev_blog_card_link_wrap')
@ -42,8 +42,8 @@ async function handler() {
const $ = $$(each);
const link = 'https://telegram.org' + $.attr('href');
return cache.tryGet(link, async () => {
const result = await got(link);
const $ = cheerio.load(result.body);
const result = await ofetch(link);
const $ = cheerio.load(result);
return {
title: $('#dev_page_title').text(),
link,

View File

@ -2,7 +2,7 @@ import { Route, ViewType } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
import cache from '@/utils/cache';
import { config } from '@/config';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
@ -106,7 +106,7 @@ For backward compatibility reasons, invalid \`routeParams\` will be treated as \
},
],
name: 'Channel',
maintainers: ['DIYgod', 'Rongronggg9'],
maintainers: ['DIYgod', 'Rongronggg9', 'pseudoyu'],
handler,
description: `
:::tip
@ -154,19 +154,20 @@ async function handler(ctx) {
searchQuery = fallback(undefined, routeParams.searchQuery, null);
}
// TODO: some channels are not available in t.me/s/, need extra handling logics
const resourceUrl = searchQuery ? `https://t.me/s/${username}?q=${encodeURIComponent(searchQuery)}` : `https://t.me/s/${username}`;
const data = await cache.tryGet(
resourceUrl,
async () => {
const _r = await got(resourceUrl);
return _r.data;
const _r = await ofetch(resourceUrl);
return _r;
},
config.cache.routeExpire,
false
);
const $ = load(data);
const $ = load(data as string);
/*
* Since 2024/4/20, t.me/s/ mistakenly have every '&' in **hyperlinks** replaced by '&'.

View File

@ -1,5 +1,5 @@
import { Route, ViewType } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
@ -28,9 +28,8 @@ async function handler(ctx) {
}
const name = ctx.req.param('name');
const response = await got({
const response = await ofetch(`https://api.telegram.org/bot${config.telegram.token}/getStickerSet?name=${name}`, {
method: 'get',
url: `https://api.telegram.org/bot${config.telegram.token}/getStickerSet?name=${name}`,
});
const data = response.data.result;