UNPKG

rsshub

Version:
86 lines (84 loc) 3.17 kB
import { t as config } from "./config-MQ-7ebJ_.mjs"; import { t as ofetch_default } from "./ofetch-DKl_Ma9g.mjs"; import { t as cache_default } from "./cache-SV6W5EPy.mjs"; import { t as got_default } from "./got-CO-ndVl2.mjs"; import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime"; import { load } from "cheerio"; import { renderToString } from "hono/jsx/dom/server"; import { raw } from "hono/html"; //#region lib/routes/openai/common.tsx const BASE_URL = new URL("https://openai.com"); /** Fetch the details of an article. */ const fetchArticleDetails = async (url) => { const $ = load(await ofetch_default(url)); const $article = $("#main article"); const categories = $("h1").prev().find("a[href]").toArray().map((element) => $(element).text()); $($article.find("h1").parents().get(4)).remove(); $article.children().last().remove(); $article.find("#citations").remove(); return { content: $article.html() ?? void 0, categories, image: $("meta[property=\"og:image\"]").attr("content") }; }; /** Fetch all articles from OpenAI's RSS feed. */ const fetchArticles = async (limit) => { const $ = load(await ofetch_default("https://openai.com/news/rss.xml", { responseType: "text", headers: { "User-Agent": config.ua } }), { xml: true }); return Promise.all($("item").toArray().slice(0, limit).map((element) => { const id = $(element).find("guid").text(); return cache_default.tryGet(`openai:news:${id}`, async () => { const title = $(element).find("title").text(); const pubDate = $(element).find("pubDate").text(); const link = $(element).find("link").text(); const { content, categories } = await fetchArticleDetails(link); return { guid: id, title, link, pubDate, description: content, category: categories }; }); })); }; const getApiUrl = async () => { const apiBaseUrl = (await got_default({ method: "get", url: "https://openai.com/blog" })).data.toString().match(/(?<=TWILL_API_BASE:").+?(?=")/)[0].replaceAll(String.raw`\u002F`, "/"); return new URL(apiBaseUrl); }; const parseArticle = (ctx, rootUrl, attributes) => cache_default.tryGet(attributes.slug, async () => { const textUrl = `${rootUrl}/${attributes.slug}`; let content = load((await got_default({ method: "get", url: textUrl })).data); const authors = content("[aria-labelledby=\"metaAuthorsHeading\"] > li > a > span > span").toArray().map((entry) => content(entry).text()).join(", "); content("*").contents().filter(function() { return this.nodeType === 8; }).remove(); content = content("#content"); const imageSrc = attributes.seo.ogImageSrc; const imageAlt = attributes.seo.ogImageAlt; const article = renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("img", { src: imageSrc ?? "", alt: imageAlt ?? "" }), raw(content.toString())] })); attributes.tags = attributes.tags || []; return { title: attributes.title, author: authors, description: article, pubDate: attributes.createdAt, category: attributes.tags.map((tag) => tag.title), link: textUrl }; }); //#endregion export { parseArticle as i, fetchArticles as n, getApiUrl as r, BASE_URL as t };