feat: 新增「法律白話文運動」 (#2913)

This commit is contained in:
Enoch Ma
2019-08-23 10:50:29 +08:00
committed by DIYgod
parent f7e8682ba8
commit 7b14cc1696
3 changed files with 51 additions and 0 deletions

View File

@@ -515,6 +515,12 @@ type 为 all 时category 参数不支持 cost 和 free
<Route author="WenryXu" example="/duozhi" path="/duozhi"/> <Route author="WenryXu" example="/duozhi" path="/duozhi"/>
## 法律白話文運動
### 最新文章
<Route author="emdoe" example="/plainlaw/archives" path="/plainlaw/archives"/>
## 飞地 ## 飞地
### 分类 ### 分类

View File

@@ -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/people/:userid/status', require('./routes/douban/people/status.js'));
router.get('/douban/topic/:id/:sort?', require('./routes/douban/topic.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')); router.get('/jandan/:sub_model', require('./routes/jandan/pic'));

View File

@@ -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,
};
};