Skip to content

Commit

Permalink
feat: add 中国科学院青年创新促进会最新博文 (DIYgod#6519)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Shen authored Dec 26, 2020
1 parent a28aebb commit c72f9d4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,12 @@ column 为 third 时可选的 category:

</Route>

## 中国科学院青年创新促进会

### 最新博文

<Route author="nczitzk" example="/yicas/blog" path="/yicas/blog"/>

## 中国劳工观察

### 调查报告
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3744,6 +3744,9 @@ router.get('/idaily/today', require('./routes/idaily/index'));
// 北屋
router.get('/northhouse/:category?', require('./routes/northhouse/index'));

// 中国科学院青年创新促进会
router.get('/yicas/blog', require('./routes/yicas/blog'));

// 九三学社
router.get('/93/:category?', require('./routes/93/index'));

Expand Down
53 changes: 53 additions & 0 deletions lib/routes/yicas/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');

module.exports = async (ctx) => {
const rootUrl = 'http://yicas.cn';
const currentUrl = `${rootUrl}/portal.php?mod=blog`;
const response = await got({
method: 'get',
url: currentUrl,
responseType: 'buffer',
});

const $ = cheerio.load(iconv.decode(response.data, 'gbk'));

const list = $('tr td a[title]')
.slice(0, 15)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
pubDate: new Date(item.next().text()).toUTCString(),
};
})
.get();

const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
responseType: 'buffer',
});
const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk'));

item.author = content('.xs2').text();
item.description = content('#blog_article').html();
item.pubDate = new Date(content('.xg1').eq(5).text()).toUTCString();

return item;
})
)
);

ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};

0 comments on commit c72f9d4

Please sign in to comment.