rsshub
Version:
Make RSS Great Again!
134 lines (132 loc) • 4.39 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs";
import "./config-C37vj7VH.mjs";
import { t as ViewType } from "./types-D84BRIt4.mjs";
import "./dist-BInvbO1W.mjs";
import "./logger-Czu8UMNd.mjs";
import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import path from "node:path";
import { load } from "cheerio";
//#region lib/routes/raspberrypi/magazine.ts
init_esm_shims();
const handler = async (ctx) => {
const limit = Number.parseInt(ctx.req.query("limit") ?? "12", 10);
const baseUrl = "https://magazine.raspberrypi.com";
const targetUrl = new URL("issues", baseUrl).href;
const $ = load(await ofetch_default(targetUrl));
const language = $("html").attr("lang") ?? "en";
let items = [];
const author = $("meta[property=\"og:site_name\"]").attr("content");
items = $("div.o-grid--equal div.o-grid__col").slice(0, limit).toArray().map((el) => {
const $el = $(el);
const $aEl = $el.find("h2.rspec-issue-card-heading a.c-link");
const title = $aEl.text()?.trim();
const image = $el.find("div.o-media__fixed a.c-link img").attr("src");
const description = art(path.join(__dirname, "templates/description-881baa33.art"), {
images: image ? [{
src: image,
alt: title
}] : void 0,
intro: $el.find("p.rspec-issue-card-summary").text()
});
const pubDateStr = $el.find("time").attr("datetime");
const linkUrl = $aEl.attr("href");
const upDatedStr = pubDateStr;
return {
title,
description,
pubDate: pubDateStr ? parseDate(pubDateStr) : void 0,
link: linkUrl ? new URL(linkUrl, baseUrl).href : void 0,
author,
content: {
html: description,
text: description
},
image,
banner: image,
updated: upDatedStr ? parseDate(upDatedStr) : void 0,
language
};
});
items = (await Promise.all(items.map((item) => {
if (!item.link) return item;
return cache_default.tryGet(item.link, async () => {
const $$ = load(await ofetch_default(item.link));
const title = $$("h1.rspec-issue__heading").text().split(/-/).pop()?.trim() ?? item.title;
const description = item.description + art(path.join(__dirname, "templates/description-881baa33.art"), { description: $$("div.rspec-issue__description").html() });
const pubDateStr = $$("time.rspec-issue__publication-month").attr("datetime");
const image = $$("img.c-figure__image").attr("src");
const upDatedStr = pubDateStr;
let processedItem = {
title,
description,
pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate,
author,
content: {
html: description,
text: description
},
image,
banner: image,
updated: upDatedStr ? parseDate(upDatedStr) : item.updated,
language
};
const pdfUrl = new URL("pdf/download", `${item.link}/`).href;
const $$$enclosureEl = load(await ofetch_default(pdfUrl))("a.c-link").first();
const enclosureUrl = $$$enclosureEl.attr("href") ? new URL($$$enclosureEl.attr("href"), baseUrl).href : void 0;
if (enclosureUrl) {
const enclosureType = "application/pdf";
processedItem = {
...processedItem,
enclosure_url: enclosureUrl,
enclosure_type: enclosureType,
enclosure_title: title,
enclosure_length: void 0
};
}
return {
...item,
...processedItem
};
});
}))).filter((_) => true);
return {
title: $("title").text(),
description: $("meta[property=\"og:description\"]").attr("content"),
link: targetUrl,
item: items,
allowEmpty: true,
image: $("meta[property=\"og:image\"]").attr("content"),
author: $("meta[property=\"og:site_name\"]").attr("content"),
language,
id: $("meta[property=\"og:url\"]").attr("content")
};
};
const route = {
path: "/magazine",
name: "Official Magazine",
url: "magazine.raspberrypi.com",
maintainers: ["nczitzk"],
handler,
example: "/raspberrypi/magazine",
parameters: void 0,
categories: ["programming"],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{
source: ["magazine.raspberrypi.com"],
target: "/raspberrypi/magazine"
}],
view: ViewType.Articles
};
//#endregion
export { handler, route };