UNPKG

rsshub

Version:
88 lines (86 loc) 3.13 kB
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs"; import { t as config } from "./config-C37vj7VH.mjs"; import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs"; import { t as cache_default } from "./cache-Bo__VnGm.mjs"; import { t as art } from "./render-BQo6B4tL.mjs"; import { t as got_default } from "./got-KxxWdaxq.mjs"; import path from "node:path"; import { load } from "cheerio"; //#region lib/routes/openai/common.ts init_esm_shims(); 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 = art(path.join(__dirname, "templates/article-88d59063.art"), { content, imageSrc, imageAlt }); 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 };