Skip to content

Commit

Permalink
feat(route/yicai): Add route for carousel part on front page (#16433)
Browse files Browse the repository at this point in the history
* feat(route/yicai): Add route for carousel part on front page

* .

* .

* .

* .

* .

* ...ESLint...
  • Loading branch information
dzx-dzx authored Aug 15, 2024
1 parent bbdae34 commit 8a3c8e1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
48 changes: 48 additions & 0 deletions lib/routes/yicai/carousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import { rootUrl, fetchFullArticles } from './utils';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';

export const route: Route = {
path: '/carousel',
categories: ['traditional-media'],
example: '/yicai/carousel',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['yicai.com/'],
},
],
name: '轮播',
maintainers: ['nczitzk'],
handler,
url: 'yicai.com/',
};

async function handler() {
const res = await ofetch(rootUrl);
const $ = load(res);
const items = await Promise.all(
fetchFullArticles(
$('#breaknews a')
.toArray()
.map((e) => ({ link: new URL($(e).attr('href'), rootUrl).href, title: $(e).text() })),
cache.tryGet
)
);

return {
title: '第一财经 - 轮播',
link: rootUrl,
item: items,
};
}
40 changes: 23 additions & 17 deletions lib/routes/yicai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ const ProcessItems = async (apiUrl, tryGet) => {
}),
}));

return Promise.all(
items.map((item) =>
tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
return Promise.all(fetchFullArticles(items, tryGet));
};
function fetchFullArticles(items, tryGet) {
return items.map((item) =>
tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});

const content = load(detailResponse.data);
const content = load(detailResponse.data);

content('h1').remove();
content('.u-btn6, .m-smallshare, .topic-hot').remove();
if (!item.pubDate) {
const dataScript = content("script[src='/js/alert.min.js']").next().text() || content('title').next().text();
const pb = new Map(JSON.parse(dataScript.match(/_pb = (\[.*?]);/)[1].replaceAll("'", '"')));
item.pubDate = parseDate(`${pb.get('actime')}:00`);
}

item.description += content('.multiText, #multi-text, .txt').html() ?? '';
content('h1').remove();
content('.u-btn6, .m-smallshare, .topic-hot').remove();

return item;
})
)
);
};
item.description = (item.description ?? '') + (content('.multiText, #multi-text, .txt').html() ?? '');

export { rootUrl, ProcessItems };
return item;
})
);
}
export { rootUrl, ProcessItems, fetchFullArticles };

0 comments on commit 8a3c8e1

Please sign in to comment.