Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(route): segmentfault #17253

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/routes/segmentfault/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { host, acw_sc__v2, parseList, parseItems } from './utils';

export const route: Route = {
Expand Down Expand Up @@ -30,8 +30,8 @@ async function handler(ctx) {
const name = ctx.req.param('name');
const apiURL = `${host}/gateway/homepage/${name}/timeline?size=20&offset=`;

const response = await got(apiURL);
const data = response.data.rows;
const response = await ofetch(apiURL);
const data = response.rows;

const list = parseList(data);
const { author } = list[0];
Expand Down
46 changes: 23 additions & 23 deletions lib/routes/segmentfault/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { config } from '@/config';
Expand All @@ -10,18 +10,12 @@ const acw_sc__v2 = (link, tryGet) =>
tryGet(
'segmentfault:acw_sc__v2',
async () => {
const response = await got(link, {
decompress: true,
});
const response = await ofetch(link);

let acw_sc__v2 = '';
for await (const data of response.body) {
const strData = data.toString();
const matches = strData.match(/var arg1='(.*?)';/);
if (matches) {
acw_sc__v2 = getAcwScV2ByArg1(matches[1]);
break;
}
const matches = response.match(/var arg1='(.*?)';/);
if (matches) {
acw_sc__v2 = getAcwScV2ByArg1(matches[1]);
}
return acw_sc__v2;
},
Expand All @@ -34,22 +28,28 @@ const parseList = (data) =>
title: item.title,
link: new URL(item.url, host).href,
author: item.user.name,
pubDate: parseDate(item.created, 'X'),
pubDate: parseDate(item.created || item.modified, 'X'),
description: item.excerpt,
}));

const parseItems = (cookie, item, tryGet) =>
tryGet(item.link, async () => {
const response = await got(item.link, {
headers: {
cookie: `acw_sc__v2=${cookie};`,
},
});
const content = load(response.data);

item.description = content('article').html();
item.category = content('.badge-tag')
.toArray()
.map((item) => content(item).text());
let response;
try {
response = await ofetch(item.link, {
headers: {
cookie: `acw_sc__v2=${cookie};`,
},
});
const content = load(response);

item.description = content('article').html();
item.category = content('.badge-tag')
.toArray()
.map((item) => content(item).text());
} catch {
// ignore
}

return item;
});
Expand Down