From a19dc7df73bcf0a59aa8a4af70fb36ecc403049f Mon Sep 17 00:00:00 2001 From: pseudoyu Date: Fri, 25 Oct 2024 17:07:21 +0800 Subject: [PATCH] refactor(route/telegram): use ofetch in telegram routes --- lib/routes/telegram/blog.ts | 10 +++++----- lib/routes/telegram/channel.ts | 11 ++++++----- lib/routes/telegram/stickerpack.ts | 5 ++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/routes/telegram/blog.ts b/lib/routes/telegram/blog.ts index 28811c4da7decc..dce3188af317ef 100644 --- a/lib/routes/telegram/blog.ts +++ b/lib/routes/telegram/blog.ts @@ -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, diff --git a/lib/routes/telegram/channel.ts b/lib/routes/telegram/channel.ts index d8fd9efcb542f2..15ca3ced5c5c32 100644 --- a/lib/routes/telegram/channel.ts +++ b/lib/routes/telegram/channel.ts @@ -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 '&'. diff --git a/lib/routes/telegram/stickerpack.ts b/lib/routes/telegram/stickerpack.ts index dbe39ad5668bfb..2c9e2eb91a7147 100644 --- a/lib/routes/telegram/stickerpack.ts +++ b/lib/routes/telegram/stickerpack.ts @@ -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;