diff --git a/lib/v2/segmentfault/user.js b/lib/v2/segmentfault/user.js index 98fa8b9f0..2ea5510c0 100644 --- a/lib/v2/segmentfault/user.js +++ b/lib/v2/segmentfault/user.js @@ -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') diff --git a/lib/v2/segmentfault/utils.js b/lib/v2/segmentfault/utils.js new file mode 100644 index 000000000..463b2a914 --- /dev/null +++ b/lib/v2/segmentfault/utils.js @@ -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, +};