From 749f2fef991efe70d39008465ec99e0b2e65df90 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 9 Jul 2022 09:18:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=2036kr=E8=B5=84=E8=AE=AF=20(#10179)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 36 ++++++++++------- lib/v2/36kr/index.js | 75 +++++++++++++++++++++++++++++++++++ lib/v2/36kr/maintainer.js | 8 ++-- lib/v2/36kr/motif.js | 57 -------------------------- lib/v2/36kr/news.js | 56 -------------------------- lib/v2/36kr/newsflashes.js | 37 ----------------- lib/v2/36kr/radar.js | 42 +++++++++++++------- lib/v2/36kr/router.js | 6 +-- lib/v2/36kr/search/article.js | 27 ------------- lib/v2/36kr/user.js | 61 ---------------------------- 10 files changed, 130 insertions(+), 275 deletions(-) create mode 100644 lib/v2/36kr/index.js delete mode 100644 lib/v2/36kr/motif.js delete mode 100644 lib/v2/36kr/news.js delete mode 100644 lib/v2/36kr/newsflashes.js delete mode 100644 lib/v2/36kr/search/article.js delete mode 100644 lib/v2/36kr/user.js diff --git a/docs/new-media.md b/docs/new-media.md index afebb0845..8c69926a1 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -28,37 +28,45 @@ pageClass: routes ### 资讯 - + -| 最新 | 推荐 | 创投 | Markets | -| ------ | --------- | ------- | ------- | -| latest | recommend | contact | ccs | +| 最新 | 推荐 | 创投 | 财经 | +| -------- | ------------- | ------- | --- | +| web_news | web_recommend | contact | ccs | -| 汽车 | 科技 | 企服 | 生活 | -| ------ | ---------- | ----------------- | ---- | -| travel | technology | enterpriseservice | life | +| 汽车 | 科技 | 企服 | 生活 | +| ------ | ---------- | ----------------- | ---------- | +| travel | technology | enterpriseservice | happy_life | -| 创新 | 房产 | 职场 | 企业号 | 其他 | -| -------- | ----------- | --------- | ------- | ----- | -| innovate | real_estate | workplace | qiyehao | other | +| 创新 | 房产 | 职场 | 企业号 | 其他 | +| -------- | ----------- | ------------ | ------- | ----- | +| innovate | real_estate | web_zhichang | qiyehao | other | ### 快讯 - + ### 用户文章 - + ### 主题文章 - + + +### 专题文章 + + ### 搜索文章 - + + +### 搜索快讯 + + ## 52hrtt 华人头条 diff --git a/lib/v2/36kr/index.js b/lib/v2/36kr/index.js new file mode 100644 index 000000000..463224ade --- /dev/null +++ b/lib/v2/36kr/index.js @@ -0,0 +1,75 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const CryptoJS = require('crypto-js'); + +const shortcuts = { + '/information': '/information/web_news', + '/information/latest': '/information/web_news', + '/information/recommend': '/information/web_recommend', + '/information/life': '/information/happy_life', + '/information/estate': '/information/real_estate', + '/information/workplace': '/information/web_zhichang', +}; + +module.exports = async (ctx) => { + const path = ctx.path.replace(/^\/news(?!flashes)/, '/information').replace(/\/search\/article/, '/search/articles'); + + const rootUrl = 'https://www.36kr.com'; + const currentUrl = `${rootUrl}${shortcuts.hasOwnProperty(path) ? shortcuts[path] : path}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const data = JSON.parse(response.data.match(/"itemList":(\[.*?\])/)[1]); + + let items = data.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 30).map((item) => { + item = item.templateMaterial ?? item; + return { + title: item.widgetTitle.replace(/<\/?em>/g, ''), + author: item.author, + pubDate: parseDate(item.publishTime), + link: `${rootUrl}/${path === '/newsflashes' ? 'newsflashes' : 'p'}/${item.itemId}`, + description: item.widgetContent ?? item.content, + }; + }); + + if (!/^\/(search|newsflashes)/.test(path)) { + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const key = CryptoJS.enc.Utf8.parse('efabccee-b754-4c'); + const cipherText = detailResponse.data.match(/{"state":"(.*)","isEncrypt":true}/)[1]; + + const content = JSON.parse( + CryptoJS.AES.decrypt(cipherText, key, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7, + }) + .toString(CryptoJS.enc.Utf8) + .toString() + ).articleDetail.articleDetailData.data; + + item.description = content.widgetContent; + + return item; + }) + ) + ); + } + + ctx.state.data = { + title: `36氪 - ${$('title').text().split('_')[0]}`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/36kr/maintainer.js b/lib/v2/36kr/maintainer.js index 9a9c95a57..d72d4d414 100644 --- a/lib/v2/36kr/maintainer.js +++ b/lib/v2/36kr/maintainer.js @@ -1,7 +1,9 @@ module.exports = { + '/information/:category?': ['nczitzk'], '/motif/:id': ['nczitzk'], - '/news/:category?': ['nczitzk'], - '/newsflashes': ['hillerliao'], - '/search/article/:keyword': ['xyqfer', 'kt286', 'nczitzk'], + '/newsflashes': ['hillerliao', 'nczitzk'], + '/search/articles/:keyword': ['xyqfer', 'kt286', 'nczitzk'], + '/search/newsflashes/:keyword': ['nczitzk'], + '/topics/:id': ['nczitzk'], '/user/:id': ['nczitzk'], }; diff --git a/lib/v2/36kr/motif.js b/lib/v2/36kr/motif.js deleted file mode 100644 index 7b9b90986..000000000 --- a/lib/v2/36kr/motif.js +++ /dev/null @@ -1,57 +0,0 @@ -const got = require('@/utils/got'); -const { parseDate } = require('@/utils/parse-date'); -const CryptoJS = require('crypto-js'); - -module.exports = async (ctx) => { - const id = ctx.params.id; - - const rootUrl = 'https://36kr.com'; - const currentUrl = `${rootUrl}/motif/${id}`; - - const response = await got({ - method: 'get', - url: currentUrl, - }); - - const data = JSON.parse(response.data.match(/"motifDetailData":{"code":0,"data":(.*)},"channel"/)[1]); - - let items = data.motifArticleList.data.itemList.map((item) => ({ - title: item.templateMaterial.widgetTitle, - link: `${rootUrl}/p/${item.itemId}`, - pubDate: parseDate(item.templateMaterial.publishTime), - })); - - items = await Promise.all( - items.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - - const key = CryptoJS.enc.Utf8.parse('efabccee-b754-4c'); - const cipherText = detailResponse.data.match(/{"state":"(.*)","isEncrypt":true}/)[1]; - - const content = JSON.parse( - CryptoJS.AES.decrypt(cipherText, key, { - mode: CryptoJS.mode.ECB, - padding: CryptoJS.pad.Pkcs7, - }) - .toString(CryptoJS.enc.Utf8) - .toString() - ).articleDetail.articleDetailData.data; - - item.author = content.author; - item.description = content.widgetContent; - - return item; - }) - ) - ); - - ctx.state.data = { - title: `36kr - ${data.motifInfo.data.categoryTitle}`, - link: currentUrl, - item: items, - }; -}; diff --git a/lib/v2/36kr/news.js b/lib/v2/36kr/news.js deleted file mode 100644 index 2c4c4c2d8..000000000 --- a/lib/v2/36kr/news.js +++ /dev/null @@ -1,56 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); - -const links = { - latest: 'information/web_news', - recommend: 'information/web_recommend', - life: 'information/happy_life', - estate: 'information/real_estate', - workplace: 'information/web_zhichang', -}; - -module.exports = async (ctx) => { - const category = ctx.params.category ?? 'latest'; - - const rootUrl = 'https://36kr.com'; - const currentUrl = `${rootUrl}/${links.hasOwnProperty(category) ? links[category] : `information/${category}`}`; - - const response = await got({ - method: 'get', - url: currentUrl, - }); - - const data = JSON.parse(response.data.match(/