Skip to content

Commit

Permalink
refactor(route/telegram): use ofetch in telegram routes
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Oct 25, 2024
1 parent 54083d0 commit a19dc7d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/routes/telegram/blog.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions lib/routes/telegram/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 '&'.
Expand Down
5 changes: 2 additions & 3 deletions lib/routes/telegram/stickerpack.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a19dc7d

Please sign in to comment.