Skip to content

Commit

Permalink
feat: add openai blog (#6301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Shen authored Dec 1, 2020
1 parent db0575d commit bf62cc9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/en/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ This route provides a flexible plan with full text content to subscribe specific

</RouteEn>

## OpenAI

### Blog

<RouteEm author="ncziztk" example="/openai/blog" path="/openai/blog/:tag" :paramsDesc="['Tag, see below, All by default']">

| All | Research | Announcements | Events | Milestones |
| --- | -------- | ------------- | ------ | ---------- |
| | research | announcements | events | milestones |

</RouteEn>

## Quanta Magazine

### Archive
Expand Down
12 changes: 12 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ IPFS 网关有可能失效,那时候换成其他网关。

<Route author="emdoe" example="/nautilus/topic/Art" path="/nautilus/topic/:tid" :paramsDesc="['话题 id, 可在页面上方 TOPICS 栏目处找到']"/>

## OpenAI

### Blog

<Route author="ncziztk" example="/openai/blog" path="/openai/blog/:tag" :paramsDesc="['标签,见下表,默认为 All']">

| All | Research | Announcements | Events | Milestones |
| --- | -------- | ------------- | ------ | ---------- |
| | research | announcements | events | milestones |

</Route>

## PMCAFF

### 今日推荐 / 精选
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3516,6 +3516,9 @@ router.get('/uisdc/news', require('./routes/uisdc/news'));
router.get('/uisdc/zt/:title?', require('./routes/uisdc/zt'));
router.get('/uisdc/topic/:title?/:sort?', require('./routes/uisdc/topic'));

// OpenAI
router.get('/openai/blog/:tag?', require('./routes/openai/blog'));

// 小木虫
router.get('/muchong/journal/:type?', require('./routes/muchong/journal'));
router.get('/muchong/:id/:type?/:sort?', require('./routes/muchong/index'));
Expand Down
53 changes: 53 additions & 0 deletions lib/routes/openai/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');

module.exports = async (ctx) => {
const tag = ctx.params.tag || '';

const rootUrl = 'https://openai.com';
const currentUrl = `${rootUrl}/blog${tag === '' ? '' : `/tags/${tag}`}`;
const response = await got({
method: 'get',
url: currentUrl,
});

const $ = cheerio.load(response.data);

const list = $('.col-12 a')
.slice(0, 5)
.map((_, item) => {
item = $(item);
return {
link: `${rootUrl}${item.attr('href')}`,
};
})
.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,
});
const content = cheerio.load(detailResponse.data);

item.title = content('title').text();

content('.aside').remove();

item.description = content('.content').html();
item.pubDate = new Date(content('time').attr('datetime')).toUTCString();

return item;
})
)
);

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

1 comment on commit bf62cc9

@vercel
Copy link

@vercel vercel bot commented on bf62cc9 Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.