Skip to content

Commit

Permalink
feat(route): Update route OpenAI (DIYgod#12113)
Browse files Browse the repository at this point in the history
* Updated route OpenAI

* style: auto format

* Improved route OpenAI

* Use cheerio to remove comments

* style: auto format

* Update lib/v2/openai/radar.js

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
StevenRCE0 and github-actions[bot] authored Mar 16, 2023
1 parent 6449836 commit 9b6594b
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 63 deletions.
8 changes: 4 additions & 4 deletions docs/en/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,11 @@ This route provides a flexible plan with full text content to subscribe specific

### Blog

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

| All | Research | Announcements | Events | Milestones |
| --- | -------- | ------------- | ------ | ---------- |
| | research | announcements | events | milestones |
| All | Announcements | Events | Safety & Alignment | Community | Product | Culture & Careers | Milestones | Research |
| --- | ------------- | ------ | ------------------ | --------- | ------- | ------------------- | ---------- | -------- |
| | announcements | events | safety-alignment | community | product | culture-and-careers | milestones | research |

</RouteEn>

Expand Down
8 changes: 4 additions & 4 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,11 @@ IPFS 网关有可能失效,那时候换成其他网关。

### Blog

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

| All | Research | Announcements | Events | Milestones |
| --- | -------- | ------------- | ------ | ---------- |
| | research | announcements | events | milestones |
| All | Announcements | Events | Safety & Alignment | Community | Product | Culture & Careers | Milestones | Research |
| --- | ------------- | ------ | ------------------ | --------- | ------- | ------------------- | ---------- | -------- |
| | announcements | events | safety-alignment | community | product | culture-and-careers | milestones | research |

</Route>

Expand Down
3 changes: 0 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3419,9 +3419,6 @@ router.get('/blizzard/news/:language?/:category?', lazyloadRouteHandler('./route
// DeepL
router.get('/deepl/blog/:lang?', lazyloadRouteHandler('./routes/deepl/blog'));

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

// 小木虫
router.get('/muchong/journal/:type?', lazyloadRouteHandler('./routes/muchong/journal'));
router.get('/muchong/:id/:type?/:sort?', lazyloadRouteHandler('./routes/muchong/index'));
Expand Down
52 changes: 0 additions & 52 deletions lib/routes/openai/blog.js

This file was deleted.

97 changes: 97 additions & 0 deletions lib/v2/openai/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { art } = require('@/utils/render');
const { toTitleCase } = require('@/utils/common-utils');
const path = require('path');

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

const rootUrl = 'https://openai.com';
const blogRootUrl = 'https://openai.com/blog';
const blogOriginUrl = `${rootUrl}/blog${tag === '' ? '' : `?topics=${tag}`}`;

// Find API base URL
const initResponse = await got({
method: 'get',
url: blogRootUrl,
});

let apiBaseUrl = initResponse.data
.toString()
.match(/(?<=TWILL_API_BASE:").+?(?=")/)[0]
.replaceAll('\\u002F', '/');
apiBaseUrl = apiBaseUrl + '/api/v1/blog-details';
const apiUrl = new URL(apiBaseUrl);

// Construct API query
apiUrl.searchParams.append('sort', '-publicationDate,-createdAt');
apiUrl.searchParams.append('page[size]', '20');
apiUrl.searchParams.append('page[number]', '1');
apiUrl.searchParams.append('include', 'media,topics,authors');
if (tag) {
apiUrl.searchParams.append('filter[topics][slugs][0]', tag);
}

const response = await got({
method: 'get',
url: apiUrl,
});

const list = response.data.data.filter((entry) => entry.type === 'blog-details');

const items = await Promise.all(
list.map((item) => {
const attributes = item.attributes;
const textUrl = `${blogRootUrl}/${attributes.slug}`;
return ctx.cache.tryGet(attributes.slug, async () => {
const detailResponse = await got({
method: 'get',
url: textUrl,
});
let content = cheerio.load(detailResponse.data);

const authors = content('[aria-labelledby="metaAuthorsHeading"] > li > a > span > span')
.toArray()
.map((entry) => content(entry).text())
.join(', ');

// Leave out comments
const comments = content('*')
.contents()
.filter(function () {
return this.nodeType === 8;
});
comments.remove();

content = content('#content');

const imageSrc = attributes.seo.ogImageSrc;
const imageAlt = attributes.seo.ogImageAlt;

const article = art(path.join(__dirname, 'templates/article.art'), {
content,
imageSrc,
imageAlt,
});

return {
title: attributes.title,
author: authors,
description: article,
pubDate: attributes.createdAt,
category: attributes.tags.map((tag) => tag.title),
link: textUrl,
};
});
})
);

const title = `OpenAI Blog${tag ? ` - ${toTitleCase(tag)}` : ''}`;

ctx.state.data = {
title,
link: blogOriginUrl,
item: items,
};
};
3 changes: 3 additions & 0 deletions lib/v2/openai/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/blog/:tag?': ['StevenRCE0', 'nczitzk'],
};
19 changes: 19 additions & 0 deletions lib/v2/openai/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
'openai.com': {
_name: 'OpenAI',
'.': [
{
title: 'Blog',
docs: 'https://docs.rsshub.app/en/new-media.html#openai',
source: '/blog',
target: (_, url) => {
const topics = new URL(url).searchParams.get('topics');
if (topics) {
return `/openai/blog/${topics}`;
}
return '/openai/blog';
},
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/openai/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/blog/:tag?', require('./blog'));
};
2 changes: 2 additions & 0 deletions lib/v2/openai/templates/article.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<img src="{{ imageSrc }}" alt="{{ imageAlt }}"/>
{{@ content }}

0 comments on commit 9b6594b

Please sign in to comment.