-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route/cookbook): Add OpenAICookbook (#17196)
* feat(route/cookbook): Add OpenAICookbook fix(route/cookbook): add missing example & follow the guidelines fix(route/cookbook): use ofetch fix(route/cookbook): delete duplicated namespace.ts fix(route/cookbook): lint issues * fix: use namespace `openai` ---------
- Loading branch information
1 parent
374c865
commit c724465
Showing
4 changed files
with
80 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Route } from '@/types'; | ||
import cache from '@/utils/cache'; | ||
import { load } from 'cheerio'; | ||
import logger from '@/utils/logger'; | ||
import ofetch from '@/utils/ofetch'; | ||
|
||
export const route: Route = { | ||
path: '/cookbook', | ||
categories: ['programming'], | ||
description: | ||
'OpenAI Cookbook 提供了大量使用 OpenAI API 的实用指南和示例代码,涵盖了从基础到高级的各种主题,包括 GPT 模型、嵌入、函数调用、微调等。这里汇集了最新的 API 功能介绍和流行的应用案例,是开发者学习和应用 OpenAI 技术的宝贵资源。', | ||
maintainers: ['liyaozhong'], | ||
radar: [ | ||
{ | ||
source: ['cookbook.openai.com/'], | ||
}, | ||
], | ||
url: 'cookbook.openai.com/', | ||
handler, | ||
example: '/openai/cookbook', | ||
name: 'Cookbook', | ||
}; | ||
|
||
async function handler() { | ||
const rootUrl = 'https://cookbook.openai.com'; | ||
const currentUrl = `${rootUrl}/`; | ||
|
||
try { | ||
const response = await ofetch(currentUrl); | ||
const $ = load(response); | ||
|
||
let items = $('[class="min-h-[90vh]"] .grid a') | ||
.toArray() | ||
.map((element) => { | ||
const $element = $(element); | ||
const $title = $element.find('div.font-semibold.text-sm.text-primary.line-clamp-1.overflow-ellipsis'); | ||
const $date = $element.find(String.raw`span.text-xs.text-muted-foreground.md\:w-24.text-end`); | ||
const $author = $element.find('p:contains("OpenAI")'); | ||
const $tags = $element.find('span[style^="color:"]'); | ||
|
||
return { | ||
title: $title.text().trim(), | ||
link: `${rootUrl}/${$element.attr('href')}`, | ||
pubDate: $date.text().trim(), | ||
author: $author.text().replace('OpenAI', '').trim(), | ||
category: $tags.toArray().map((tag) => $(tag).text().trim()), | ||
}; | ||
}); | ||
|
||
items = ( | ||
await Promise.all( | ||
items.map((item) => | ||
cache.tryGet(item.link, async () => { | ||
try { | ||
const detailResponse = await ofetch(item.link); | ||
const $ = load(detailResponse); | ||
|
||
item.description = $(String.raw`article.prose.prose-sm.sm\:prose-base.max-w-none.dark\:prose-invert`).html(); | ||
return item; | ||
} catch { | ||
return { ...item, description: '' }; | ||
} | ||
}) | ||
) | ||
) | ||
).filter((item) => item?.description); | ||
|
||
return { | ||
title: 'OpenAI Cookbook', | ||
link: currentUrl, | ||
item: items, | ||
}; | ||
} catch (error) { | ||
logger.error(`处理 OpenAI Cookbook 请求时发生错误: ${error}`); | ||
throw error; | ||
} | ||
} |
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