From afe6a78939dca0c1ef7f4bea89a1bd777c854b0b Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:46:58 +0800 Subject: [PATCH] feat(route/caixin): Fetch all blog articles. (#13522) * feat(route/caixin): Fetch all blog articles. * . * Update doc. * Update lib/v2/caixin/blog.js * . --------- --- lib/v2/caixin/blog.js | 120 +++++++++++++++++++++--------------- lib/v2/caixin/router.js | 2 +- website/docs/routes/blog.md | 2 +- 3 files changed, 73 insertions(+), 51 deletions(-) diff --git a/lib/v2/caixin/blog.js b/lib/v2/caixin/blog.js index b4aa1cc082..6f9c6dc053 100644 --- a/lib/v2/caixin/blog.js +++ b/lib/v2/caixin/blog.js @@ -5,54 +5,76 @@ const { parseDate } = require('@/utils/parse-date'); const { parseBlogArticle } = require('./utils'); module.exports = async (ctx) => { - const { column } = ctx.params; - if (!isValidHost(column)) { - throw Error('Invalid column'); + const { column, limit = 20 } = ctx.params; + if (!column) { + const { data } = await got('https://blog.caixin.com/blog-api/post/index', { + searchParams: { + page: 1, + size: limit, + }, + }).json(); + const posts = data.map((item) => ({ + title: item.title, + description: item.brief, + author: item.authorName, + link: item.postUrl.replace('http://', 'https://'), + pubDate: parseDate(item.publishTime, 'x'), + })); + const items = await Promise.all(posts.map((item) => parseBlogArticle(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `财新博客 - 全部`, + link: 'https://blog.caixin.com', + // description: introduce, + // image: avatar, + item: items, + }; + } else { + if (!isValidHost(column)) { + throw Error('Invalid column'); + } + const link = `https://${column}.blog.caixin.com`; + const { data: response } = await got(link); + const $ = cheerio.load(response); + const user = $('div.indexMainConri > script[type="text/javascript"]') + .text() + .substring('window.user = '.length + 1) + .split(';')[0] + .replaceAll(/\s/g, ''); + const authorId = user.match(/id:"(\d+)"/)[1]; + const authorName = user.match(/name:"(.*?)"/)[1]; + const avatar = user.match(/avatar:"(.*?)"/)[1]; + const introduce = user.match(/introduce:"(.*?)"/)[1]; + + const { + data: { data }, + } = await got('https://blog.caixin.com/blog-api/post/posts', { + searchParams: { + page: 1, + size: ctx.query.limit ? parseInt(ctx.query.limit) : 20, + content: '', + authorId, + sort: 'publishTime', + direction: 'DESC', + }, + }); + + const posts = data.map((item) => ({ + title: item.title, + description: item.brief, + author: item.displayName, + link: item.guid.replace('http://', 'https://'), + pubDate: parseDate(item.publishTime, 'x'), + })); + + const items = await Promise.all(posts.map((item) => parseBlogArticle(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `财新博客 - ${authorName}`, + link, + description: introduce, + image: avatar, + item: items, + }; } - const link = `https://${column}.blog.caixin.com`; - const { data: response } = await got(link); - const $ = cheerio.load(response); - const authorId = $('script[type="text/javascript"]') - .text() - .match(/window\.authorId = (\d+);/)[1]; - const authorName = $('script[type="text/javascript"]') - .text() - .match(/window\.authorName = "(.*)";/)[1]; - const avatar = $('script[type="text/javascript"]') - .text() - .match(/avatar: "(.*?)",/)[1]; - const introduce = $('script[type="text/javascript"]') - .text() - .match(/introduce: "(.*?)"\n/)[1]; - - const { - data: { data }, - } = await got('https://blog.caixin.com/blog-api/post/posts', { - searchParams: { - page: 1, - size: ctx.query.limit ? parseInt(ctx.query.limit) : 20, - content: '', - authorId, - sort: 'publishTime', - direction: 'DESC', - }, - }); - - const posts = data.map((item) => ({ - title: item.title, - description: item.brief, - author: item.displayName, - link: item.guid.replace('http://', 'https://'), - pubDate: parseDate(item.publishTime, 'x'), - })); - - const items = await Promise.all(posts.map((item) => parseBlogArticle(item, ctx.cache.tryGet))); - - ctx.state.data = { - title: `财新博客 - ${authorName}`, - link, - description: introduce, - image: avatar, - item: items, - }; }; diff --git a/lib/v2/caixin/router.js b/lib/v2/caixin/router.js index 1ec1c676c4..962a36a4f7 100644 --- a/lib/v2/caixin/router.js +++ b/lib/v2/caixin/router.js @@ -1,6 +1,6 @@ module.exports = (router) => { router.get('/article', require('./article')); - router.get('/blog/:column', require('./blog')); + router.get('/blog/:column?', require('./blog')); router.get('/database', require('./database')); router.get('/k', require('./k')); router.get('/latest', require('./latest')); diff --git a/website/docs/routes/blog.md b/website/docs/routes/blog.md index 1fa2692325..5265ee5f1a 100644 --- a/website/docs/routes/blog.md +++ b/website/docs/routes/blog.md @@ -373,7 +373,7 @@ Limit the number of entries to be retrieved by adding `?limit=x` to the end of t ### 用户博客 {#cai-xin-bo-ke-yong-hu-bo-ke} - + 通过提取文章全文,以提供比官方源更佳的阅读体验.