feat: add 扇贝精选文章 (#3277)

This commit is contained in:
qiwihui 2019-10-18 12:05:35 +08:00 committed by DIYgod
parent a02cb73943
commit a12ef8d4dc
3 changed files with 57 additions and 0 deletions

View File

@ -29,6 +29,20 @@ pageClass: routes
<Route author="DIYgod" example="/shanbay/checkin/ddwej" path="/shanbay/checkin/:id" :paramsDesc="['用户 id']" />
### 精选文章
<Route author="qiwihui" example="/shanbay/footprints" path="/shanbay/footprints/:category?" :paramsDesc="['分类 id']">
| 用户故事 | 地道表达法 | 实用口语 | 语法教室 | 读新闻学英语 | 单词鸡汤 | 扇贝理念 | 英语知识 | 原汁英文 |
| -------- | ---------- | -------- | -------- | ------------ | -------- | -------- | -------- | -------- |
| 1 | 31 | 46 | 34 | 40 | 43 | 16 | 7 | 10 |
| 学习方法 | 影视剧讲义 | 产品更新 | 精读文章 | 他山之石 | Quora 翻译 | TED 推荐 | 大耳狐小课堂 | 互动话题 |
| -------- | ---------- | -------- | -------- | -------- | ---------- | -------- | ------------ | -------- |
| 13 | 22 | 28 | 4 | 19 | 25 | 37 | 49 | 52 |
</Route>
## 下厨房
### 用户作品

View File

@ -1002,6 +1002,7 @@ router.get('/steamgifts/discussions/:category?', require('./routes/steam/steamgi
// 扇贝
router.get('/shanbay/checkin/:id', require('./routes/shanbay/checkin'));
router.get('/shanbay/footprints/:category?', require('./routes/shanbay/footprints'));
// Facebook
router.get('/facebook/page/:id', require('./routes/facebook/page'));

View File

@ -0,0 +1,42 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const cat = ctx.params.category;
const host = 'https://www.shanbay.com';
const url = cat ? `${host}/footprints/?category=${cat}` : `${host}/footprints/`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('div.articles div.article-item').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const time = $('p.article-publish-day').text() + $('span.article-publish-month-year').text();
const title = $('h3.article-title a').text();
const itemUrl = host + $('h3.article-title a').attr('href');
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const responses = await got.get(itemUrl);
const $d = cheerio.load(responses.data);
const single = {
title,
pubDate: new Date(time).toUTCString(),
link: itemUrl,
guid: itemUrl,
description: $d('#content .content').html(),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: '扇贝精选文章',
link: url,
item: out,
};
};