UNPKG

rsshub

Version:
66 lines (64 loc) 2.37 kB
import { t as config } from "./config-CCmw1BNE.mjs"; import { t as got_default } from "./got-BTowW50G.mjs"; import { t as ConfigNotFoundError } from "./config-not-found-fQ5mxvDU.mjs"; import { load } from "cheerio"; //#region lib/routes/rsshub/transform/sitemap.ts const route = { path: "/transform/sitemap/:url/:routeParams?", categories: ["other"], example: "/rsshub/transform/sitemap/https%3A%2F%2Fwww.sitemaps.org%2Fsitemap.xml", parameters: { url: "`encodeURIComponent`ed URL address", routeParams: "Transformation rules, requires URL encode" }, features: { requireConfig: [{ name: "ALLOW_USER_SUPPLY_UNSAFE_DOMAIN", description: "" }], requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, name: "Transformation - Sitemap", maintainers: ["flrngel"], description: `Specify options (in the format of query string) in parameter \`routeParams\` parameter to extract data from Sitemap. (Follows Sitemap Protocol 0.9) | Key | Meaning | Accepted Values | Default | | ------- | -------------------- | --------------- | -------------------------------- | | \`title\` | The title of the RSS | \`string\` | The first \`<loc>\` in the sitemap |`, handler }; async function handler(ctx) { if (!config.feature.allow_user_supply_unsafe_domain) throw new ConfigNotFoundError(`This RSS is disabled unless 'ALLOW_USER_SUPPLY_UNSAFE_DOMAIN' is set to 'true'.`); const url = ctx.req.param("url"); const response = await got_default({ method: "get", url }); const routeParams = new URLSearchParams(ctx.req.param("routeParams")); const $ = load(response.data, { xmlMode: true }); const rssTitle = routeParams.get("title") || ($("urlset url").length && $("urlset url").first().find("loc").text() ? $("urlset url").first().find("loc").text() : "Sitemap"); const urls = $("urlset url").toArray(); const items = urls && urls.length ? urls.map((item) => { try { return { title: $(item).find("loc").text(), link: $(item).find("loc").text(), description: $(item).find("loc").text(), pubDate: $(item).find("lastmod").text() || void 0 }; } catch { return null; } }).filter(Boolean) : []; return { title: rssTitle, link: url, description: `Proxy ${url}`, item: items }; } //#endregion export { route };