forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): Update route OpenAI (DIYgod#12113)
* 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
1 parent
6449836
commit 9b6594b
Showing
9 changed files
with
132 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'/blog/:tag?': ['StevenRCE0', 'nczitzk'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function (router) { | ||
router.get('/blog/:tag?', require('./blog')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<img src="{{ imageSrc }}" alt="{{ imageAlt }}"/> | ||
{{@ content }} |