fix(route): segment fault user (#12900)

This commit is contained in:
Tony
2023-07-28 23:45:22 +08:00
committed by GitHub
parent e0800cf4a2
commit 3428cc2beb
2 changed files with 42 additions and 2 deletions
+8 -2
View File
@@ -1,7 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { acw_sc__v2 } = require('./utils');
const host = 'https://segmentfault.com';
module.exports = async (ctx) => {
@@ -19,10 +19,16 @@ module.exports = async (ctx) => {
author,
}));
const acwScV2Cookie = await acw_sc__v2(list[0].link, ctx.cache.tryGet);
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got(item.link);
const response = await got(item.link, {
headers: {
cookie: `acw_sc__v2=${acwScV2Cookie};`,
},
});
const content = cheerio.load(response.data);
item.description = content('article')
+34
View File
@@ -0,0 +1,34 @@
const zlib = require('zlib');
const got = require('@/utils/got');
const config = require('@/config').value;
const { getAcwScV2ByArg1 } = require('@/v2/5eplay/utils');
const acw_sc__v2 = (link, tryGet) =>
tryGet(
'segmentfault:acw_sc__v2',
async () => {
const response = await got(link, {
decompress: false,
});
const unzipData = zlib.createUnzip();
unzipData.write(response.body);
let acw_sc__v2 = '';
for await (const data of unzipData) {
const strData = data.toString();
const matches = strData.match(/var arg1='(.*?)';/);
if (matches) {
acw_sc__v2 = getAcwScV2ByArg1(matches[1]);
break;
}
}
return acw_sc__v2;
},
config.cache.routeExpire,
false
);
module.exports = {
acw_sc__v2,
};