rsshub
Version:
Make RSS Great Again!
70 lines (68 loc) • 2.88 kB
JavaScript
import "./esm-shims-CzJ_djXG.mjs";
import "./config-C37vj7VH.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 * as cheerio from "cheerio";
//#region lib/routes/wired/tag.ts
const route = {
path: "/tag/:tag",
example: "/wired/tag/facebook",
parameters: { tag: "Tag name" },
radar: [{ source: ["www.wired.com/tag/:tag/"] }],
name: "Tags",
maintainers: ["Naiqus"],
handler
};
async function handler(ctx) {
const baseUrl = "https://www.wired.com";
const { tag } = ctx.req.param();
const link = `${baseUrl}/tag/${tag}/`;
const response = await ofetch_default(link);
const $ = cheerio.load(response);
const preloadedState = JSON.parse($("script:contains(\"window.__PRELOADED_STATE__\")").text().match(/window\.__PRELOADED_STATE__ = (.*);/)?.[1] ?? "{}");
const list = preloadedState.transformed.tag.items.map((item) => ({
title: item.dangerousHed,
description: item.dangerousDek,
link: `${baseUrl}${item.url}`,
pubDate: parseDate(item.date),
author: item.contributors.author.items.map((author) => author.name).join(", "),
category: [item.rubric.name]
}));
const items = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => {
const response$1 = await ofetch_default(item.link);
const $$1 = cheerio.load(response$1);
const preloadedState$1 = JSON.parse($$1("script:contains(\"window.__PRELOADED_STATE__\")").text().match(/window\.__PRELOADED_STATE__ = (.*);/)?.[1] ?? "{}");
const headerLeadAsset = $$1("div[data-testid*=\"ContentHeaderLeadAsset\"]");
headerLeadAsset.find("button").remove();
if (headerLeadAsset.find("video")) {
headerLeadAsset.find("video").attr("src", $$1("link[rel=\"preload\"][as=\"video\"]").attr("href"));
headerLeadAsset.find("video").attr("controls", "");
headerLeadAsset.find("video").attr("preload", "metadata");
headerLeadAsset.find("video").removeAttr("autoplay");
}
const content = $$1(".body__inner-container").toArray().map((el) => {
const $el = $$1(el);
$el.find("noscript").each((_, el$1) => {
const $e = $$1(el$1);
$e.replaceWith($e.html() || "");
});
return $el.html();
}).join("");
item.description = ($$1("div[class^=ContentHeaderDek]").prop("outerHTML") || "") + headerLeadAsset.prop("outerHTML") + content;
item.category = [...new Set([...item.category, ...preloadedState$1.transformed.article.tagCloud.tags.map((t) => t.tag)])];
return item;
})));
return {
title: preloadedState.transformed["head.title"],
description: preloadedState.transformed["head.description"],
link,
image: `${baseUrl}${preloadedState.transformed.logo.sources.sm.url}`,
language: "en",
item: items
};
}
//#endregion
export { route };