add: ui.cn and woshipm.com popular channel (#1616)

This commit is contained in:
WenryXu
2019-02-26 12:09:59 +08:00
committed by DIYgod
parent 28360e54ba
commit f4398691fd
5 changed files with 174 additions and 0 deletions

View File

@@ -3015,6 +3015,8 @@ board 和 build 可在[这里](http://api.ineal.me/tss/status)查看
### 人人都是产品经理
<route name="热门文章" author="WenryXu" example="/woshipm/popular" path="/woshipm/popular"/>
<route name="用户收藏" author="LogicJake" example="/woshipm/bookmarks/324696" path="/woshipm/bookmarks/:id" :paramsDesc="['用户 id']"/>
<route name="用户文章" author="LogicJake" example="/woshipm/user_article/324696" path="/woshipm/user_article/:id" :paramsDesc="['用户 id']"/>
@@ -3036,3 +3038,9 @@ board 和 build 可在[这里](http://api.ineal.me/tss/status)查看
| (空) | company | business | ent | technology | idonews |
</route>
### UI 中国
<route name="推荐文章" author="WenryXu" example="/ui-cn/article" path="/ui-cn/article"/>
<route name="个人作品" author="WenryXu" example="/ui-cn/user/85974" path="/ui-cn/user/:id" :paramsDesc="['用户id']"/>

View File

@@ -1045,6 +1045,7 @@ router.get('/duozhi', require('./routes/duozhi'));
router.get('/dockerhub/build/:owner/:image/:tag?', require('./routes/dockerhub/build'));
// 人人都是产品经理
router.get('/woshipm/popular', require('./routes/woshipm/popular'));
router.get('/woshipm/bookmarks/:id', require('./routes/woshipm/bookmarks'));
router.get('/woshipm/user_article/:id', require('./routes/woshipm/user_article'));
@@ -1082,4 +1083,8 @@ router.get('/security/pulses', require('./routes/security/pulses'));
// DoNews
router.get('/donews/:column?', require('./routes/donews/index'));
// UI 中国
router.get('/ui-cn/article', require('./routes/ui-cn/article'));
router.get('/ui-cn/user/:id', require('./routes/ui-cn/user'));
module.exports = router;

View File

@@ -0,0 +1,62 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const response = await axios('https://www.ui.cn/');
const $ = cheerio.load(response.data);
const postList = $('#article')
.find('.h-article-list')
.find('li')
.get();
const result = await Promise.all(
postList.map(async (item) => {
const title = $(item)
.find('.ellipsis')
.text();
const link =
'https://www.ui.cn' +
$(item)
.find('.ellipsis')
.attr('href');
const guid = link;
const single = {
title: title,
link: link,
guid: guid,
pubDate: '',
description: '',
};
const description_key = 'ui-cn_description' + guid;
const description_value = await ctx.cache.get(description_key);
const pubDate_key = 'ui-cn_pubDate' + guid;
const pubDate_value = await ctx.cache.get(pubDate_key);
if (description_value && pubDate_value) {
single.description = description_value;
single.pubDate = pubDate_value;
} else {
const temp = await axios(link);
single.description = $(temp.data)
.find('.works-cont')
.html();
single.pubDate = new Date(
$(temp.data)
.find('.works-top')
.find('.time')
.find('em')
.text()
.replace(/更新于:/g, '')
).toUTCString();
ctx.cache.set(description_key, single.description, 60 * 60);
ctx.cache.set(pubDate_key, single.pubDate, 60 * 60);
}
return Promise.resolve(single);
})
);
ctx.state.data = { title: '推荐文章 - UI 中国', link: 'https://www.ui.cn/', item: result };
};

61
lib/routes/ui-cn/user.js Normal file
View File

@@ -0,0 +1,61 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const id = ctx.params.id;
const response = await axios(`https://i.ui.cn/ucenter/${id}.html`);
const $ = cheerio.load(response.data);
const postList = $('#works-list li').get();
const name = $('.user-pad .us-name .n1').text();
const result = await Promise.all(
postList.map(async (item) => {
const title = $(item)
.find('.iInspir-title')
.find('a')
.text();
const link = $(item)
.find('.iInspir-title')
.find('a')
.attr('href');
const guid = link;
const single = {
title: title,
link: link,
guid: guid,
pubDate: '',
description: '',
};
const description_key = 'ui-cn_description' + guid;
const description_value = await ctx.cache.get(description_key);
const pubDate_key = 'ui-cn_pubDate' + guid;
const pubDate_value = await ctx.cache.get(pubDate_key);
if (description_value && pubDate_value) {
single.description = description_value;
single.pubDate = pubDate_value;
} else {
const temp = await axios(link);
single.description = $(temp.data)
.find('.works-cont')
.html();
single.pubDate = new Date(
$(temp.data)
.find('.works-top')
.find('.time')
.find('em')
.text()
.replace(/更新于:/g, '')
).toUTCString();
ctx.cache.set(description_key, single.description, 60 * 60);
ctx.cache.set(pubDate_key, single.pubDate, 60 * 60);
}
return Promise.resolve(single);
})
);
ctx.state.data = { title: `${name} 的设计作品 - UI 中国`, link: `https://i.ui.cn/ucenter/${id}.html`, item: result };
};

View File

@@ -0,0 +1,38 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const response = await axios('http://www.woshipm.com/__api/v1/browser/popular');
const result = await Promise.all(
response.data.payload.map(async (item) => {
const title = item.title;
const link = item.permalink;
const guid = item.id;
const pubDate = new Date(item.date).toUTCString();
const single = {
title: title,
link: link,
guid: guid,
pubDate: pubDate,
description: '',
};
const key = 'woshipm' + guid;
const value = await ctx.cache.get(key);
if (value) {
single.description = value;
} else {
const temp = await axios(link);
const $ = cheerio.load(temp.data);
single.description = $('.grap').html();
ctx.cache.set(key, single.description, 60 * 60);
}
return Promise.resolve(single);
})
);
ctx.state.data = { title: '热门文章 - 人人都是产品经理', link: 'http://www.woshipm.com/', item: result };
};