From c2287e29b04235bb0c988413e1fc162256c22d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=B0=B4=E6=BA=B6?= Date: Thu, 11 Mar 2021 13:59:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E6=AD=AA=E8=84=91?= =?UTF-8?q?=E8=AF=BB=20(#7129)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 6 +++++ lib/router.js | 3 +++ lib/routes/wainao/index.js | 46 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/routes/wainao/index.js diff --git a/docs/new-media.md b/docs/new-media.md index b47ee7b05d..f935a8b138 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1891,6 +1891,12 @@ column 为 third 时可选的 category: +## 歪脑 wainao.me + +### 所有文章 + + + ## 晚点 LatePost diff --git a/lib/router.js b/lib/router.js index 9104088b82..bb362c1de5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4024,6 +4024,9 @@ router.get('/gf-cn/news/:category?', require('./routes/gf-cn/news')); // Eagle router.get('/eagle/changelog/:language?', require('./routes/eagle/changelog')); +// 歪脑读 +router.get('/wainao-reads/all-articles', require('./routes/wainao/index')); + // react router.get('/react/react-native-weekly', require('./routes/react/react-native-weekly')); module.exports = router; diff --git a/lib/routes/wainao/index.js b/lib/routes/wainao/index.js new file mode 100644 index 0000000000..b48c0c292d --- /dev/null +++ b/lib/routes/wainao/index.js @@ -0,0 +1,46 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +// 找到那个对象存储的是需要的数据 +const findData = (list) => { + const target = list.filter((item) => item.method === 'replaceWith' || item.method === 'infiniteScrollInsertView'); + if (!target.length) { + return {}; + } + return target[0]; +}; + +module.exports = async (ctx) => { + // 如果在国内运行,需要在这里配置代理(agent)方可请求歪脑读接口。 + const response = await got({ + method: 'post', + url: `https://www.wainao.me/views/ajax?_wrapper_format=drupal_ajax`, + body: `view_name=wainao_reads_all_articles&view_display_id=page_2&view_args=&view_path=%2Fwainao-reads%2Fall-articles&view_base_path=wainao-reads%2Fall-articles&view_dom_id=0ae93d7cdc29110b720a459d259ee376b5cdfd8caa02ce79d3468a5d3084b1a1&pager_element=0&sort_by=created&page=0&_drupal_ajax=1&ajax_page_state%5Btheme%5D=wainaome&ajax_page_state%5Btheme_token%5D=&ajax_page_state%5Blibraries%5D=addtoany%2Faddtoany%2Cbetter_exposed_filters%2Fauto_submit%2Cbetter_exposed_filters%2Fgeneral%2Cbootstrap%2Fpopover%2Cbootstrap%2Ftooltip%2Ccore%2Fhtml5shiv%2Ccore%2Fpicturefill%2Cextlink%2Fdrupal.extlink%2Cfontawesome%2Ffontawesome.svg.shim%2Cgoogle_analytics%2Fgoogle_analytics%2Cmasonry%2Fmasonry.layout%2Csystem%2Fbase%2Cviews%2Fviews.module%2Cviews_infinite_scroll%2Fviews-infinite-scroll%2Cwainao_votes%2Fwainao_votes%2Cwainaome%2Fdefault-layout%2Cwn_custom%2Fwn_custom`, + headers: { + accept: 'application/json, text/javascript, */*; q=0.01', + 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8', + }, + }); + const _body = response.data; + const data = findData(_body).data; + if (!data) { + return; + } + const $ = cheerio.load(data); + + const titles = $('.view-content .views-row').map((_, item) => { + const $item = $(item); + return { + title: $item.find('.field_title a').text(), + description: $item.find('.field_teaser').text().trim(), + link: 'https://www.wainao.me' + $item.find('.field_title a').attr('href'), + pubDate: $item.find('.field_date').text().trim(), + }; + }); + ctx.state.data = { + title: `歪脑读 - 所有文章`, + link: 'https://www.wainao.me/wainao-reads/all-articles', + description: '歪脑读 - 所有文章...', + item: titles.toArray(), + }; +};