Add All Poetry (#1414)

Part of #1387
This commit is contained in:
Henry Wang
2019-01-17 03:32:32 +00:00
committed by DIYgod
parent 2f9f7a34a9
commit 18b06a8cc3
4 changed files with 70 additions and 0 deletions

View File

@@ -2361,6 +2361,10 @@ Example: `https://store.steampowered.com/search/?specials=1&term=atelier` 中的
</route>
### All Poetry
<route name="Poems" author="HenryQW" example="/allpoetry/newest" path="/allpoetry/:order?" :paramsDesc="['排序方式, `best``newest`, 缺省 `best`']"/>
## 中国驻外使领馆
### 大使馆

View File

@@ -423,3 +423,7 @@ Supported sub-sites
| Mac | Google | Toys |
</routeEn>
### All Poetry
<routeEn name="Poems" author="HenryQW" example="/allpoetry/newest" path="/allpoetry/:order?" :paramsDesc="['order by type, `best` or `newest`, default to `best`']"/>

View File

@@ -964,6 +964,9 @@ router.get('/jingdong/zhongchou/:type/:status/:sort', require('./routes/jingdong
// 淘宝众筹
router.get('/taobao/zhongchou/:type?', require('./routes/taobao/zhongchou'));
// All Poetry
router.get('/allpoetry/:order?', require('./routes/allpoetry/order'));
// 华尔街见闻
router.get('/wallstreetcn/news/global', require('./routes/wallstreetcn/news'));

View File

@@ -0,0 +1,59 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const url = require('url');
module.exports = async (ctx) => {
const { order = 'best' } = ctx.params;
const link = `https://allpoetry.com/poems/about/spotlight-oldpoem?order=${order}}`;
const host = 'https://allpoetry.com/';
const response = await axios({
method: 'get',
url: link,
});
const data = response.data;
const $ = cheerio.load(data);
const items = await Promise.all(
$('.sub')
.slice(0, 5)
.get()
.map(async (e) => {
let itemUrl = $(e)
.find('h1.title > a')
.attr('href');
itemUrl = url.resolve(host, itemUrl);
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const single = {
title: $(e)
.find('h1.title > a')
.text(),
description: $(e)
.find('div.poem_body')
.html(),
link: itemUrl,
author: $(e)
.find('div.bio >a ')
.attr('data-name'),
guid: itemUrl,
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `All Poetry - ${order.toUpperCase() + order.slice(1)}`,
link,
item: items,
};
};