Skip to content

Commit

Permalink
fix(route/5eplay): update data acquisition method (DIYgod#17120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlouxgit authored Oct 14, 2024
1 parent 3cd690a commit 2dd0033
Showing 1 changed file with 1 addition and 51 deletions.
52 changes: 1 addition & 51 deletions lib/routes/5eplay/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import { load } from 'cheerio';
import zlib from 'zlib';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { config } from '@/config';
import { getAcwScV2ByArg1 } from './utils';

export const route: Route = {
path: '/article',
Expand Down Expand Up @@ -34,70 +31,23 @@ export const route: Route = {
async function handler() {
const rootUrl = 'https://csgo.5eplay.com/';
const apiUrl = `${rootUrl}api/article?page=1&type_id=0&time=0&order_by=0`;
const articleUrl = `${rootUrl}article`;

const { data: response } = await got({
method: 'get',
url: apiUrl,
});

// get acw_sc__v2
const acw_sc__v2 = await cache.tryGet(
articleUrl,
async () => {
// Zlib Z_BUF_ERROR: unexpected end of file, should close decompress
const detailResponse = await got(
{
method: 'get',
url: articleUrl,
},
{
decompress: false,
}
);

const unzipData = zlib.createUnzip({
chunkSize: 20 * 1024,
});
unzipData.write(detailResponse.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;
},
Math.min(config.cache.routeExpire, 25 * 60),
false
);

const items = await Promise.all(
response.data.list.map((item) =>
cache.tryGet(item.jump_link, async () => {
if (!acw_sc__v2) {
return {
title: item.title,
description: item.title + (item.images?.[0] ? `<img src="${item.images[0]}" />` : ''),
pubDate: parseDate(item.dateline * 1000),
link: item.jump_link,
};
}
const { data: detailResponse } = await got({
method: 'get',
url: item.jump_link,
headers: {
cookie: `acw_sc__v2=${acw_sc__v2}`,
},
});
const $ = load(detailResponse);

const content = $('.article-text');
const res = [];
const res: string[] = [];

content.find('> p, > blockquote').each((i, e) => {
res.push($(e).text());
Expand Down

0 comments on commit 2dd0033

Please sign in to comment.