From 7b14cc16966bcd5e294f68fd86626494befd7c28 Mon Sep 17 00:00:00 2001 From: Enoch Ma Date: Fri, 23 Aug 2019 10:50:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E3=80=8C=E6=B3=95?= =?UTF-8?q?=E5=BE=8B=E7=99=BD=E8=A9=B1=E6=96=87=E9=81=8B=E5=8B=95=E3=80=8D?= =?UTF-8?q?=20(#2913)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/other.md | 6 +++++ lib/router.js | 3 +++ lib/routes/plainlaw/archives.js | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 lib/routes/plainlaw/archives.js diff --git a/docs/other.md b/docs/other.md index 645ec5ad25..82e34acb0f 100644 --- a/docs/other.md +++ b/docs/other.md @@ -515,6 +515,12 @@ type 为 all 时,category 参数不支持 cost 和 free +## 法律白話文運動 + +### 最新文章 + + + ## 飞地 ### 分类 diff --git a/lib/router.js b/lib/router.js index 7925d5c650..f6e43e7960 100644 --- a/lib/router.js +++ b/lib/router.js @@ -135,6 +135,9 @@ router.get('/douban/explore/column/:id', require('./routes/douban/explore_column router.get('/douban/people/:userid/status', require('./routes/douban/people/status.js')); router.get('/douban/topic/:id/:sort?', require('./routes/douban/topic.js')); +// 法律白話文運動 +router.get('/plainlaw/archives', require('./routes/plainlaw/archives.js')); + // 煎蛋 router.get('/jandan/:sub_model', require('./routes/jandan/pic')); diff --git a/lib/routes/plainlaw/archives.js b/lib/routes/plainlaw/archives.js new file mode 100644 index 0000000000..0df710cbd9 --- /dev/null +++ b/lib/routes/plainlaw/archives.js @@ -0,0 +1,42 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const currentDate = new Date(); + const year = currentDate.getFullYear(); + const url = `https://plainlaw.me/${year}`; + + const res = await got.get(url); + const $ = cheerio.load(res.data); + const list = $('article').get(); + + const out = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const title = $('h3.title > a').text(); + const address = $('h3.title > a').attr('href'); + const cache = await ctx.cache.get(address); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const res = await got.get(address); + const capture = cheerio.load(res.data); + capture('div.inline-post.clearfix').remove(); + const banner = capture('div.hero').html(); + const contents = banner + capture('div.dable-content-wrapper').html(); + const single = { + title, + description: contents, + link: address, + guid: address, + }; + ctx.cache.set(address, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + ctx.state.data = { + title: '法律白話文運動', + link: url, + item: out, + }; +};