rsshub
Version:
Make RSS Great Again!
148 lines (143 loc) • 5.46 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs";
import { t as config } from "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import "./logger-Czu8UMNd.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 path from "node:path";
import { load } from "cheerio";
//#region lib/routes/theinitium/app.ts
init_esm_shims();
const appUrl = "https://app.theinitium.com/";
const userAgent = "PugpigBolt v4.1.8 (iPhone, iOS 18.2.1) on phone (model iPhone15,2)";
const route = {
path: "/app/:category?",
categories: ["new-media"],
example: "/theinitium/app",
parameters: { category: "Category, see below, latest_sc by default" },
features: {
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
name: "App",
maintainers: ["quiniapiezoelectricity"],
radar: [{
source: ["app.theinitium.com/t/latest/:category"],
target: "/app/:category"
}],
handler,
description: `抓取[The Initium App](https://app.theinitium.com/)的文章列表
::: warning
此路由暂不支持登陆认证
:::
Category 栏目:
| ----- | 简体中文 | 繁體中文 |
| ----- | ----------------- | ---------------- |
| 最新 | latest_sc | latest_tc |
| 日报 | daily_brief_sc | daily_brief_tc |
| 速递 | whats_new_sc | whats_new_tc |
| 专题 | report_sc | report_tc |
| 评论 | opinion_sc | opinion_tc |
| 国际 | international_sc | international_tc |
| 大陆 | mainland_sc | mainland_tc |
| 香港 | hongkong_sc | hongkong_tc |
| 台湾 | taiwan_sc | taiwan_tc |
| 播客 | article_audio_sc | article_audio_tc |`
};
const resolveRelativeLink = ($, elem, attr, appUrl$1) => {
const $elem = $(elem);
if (appUrl$1) try {
const oldAttr = $elem.attr(attr);
if (oldAttr) $elem.attr(attr, new URL(oldAttr, appUrl$1).href);
} catch {}
};
async function getUA(url) {
return await ofetch_default(url, { headers: { "User-Agent": userAgent } });
}
async function fetchAppPage(url) {
const $ = load(await getUA(url.href));
$("a, area").each((_, elem) => {
resolveRelativeLink($, elem, "href", appUrl);
});
$("img, video, audio, source, iframe, embed, track").each((_, elem) => {
resolveRelativeLink($, elem, "src", appUrl);
$(elem).removeAttr("srcset");
});
$("video[poster]").each((_, elem) => {
resolveRelativeLink($, elem, "poster", appUrl);
});
const article = $(".pp-article__body");
article.find(".block-related-articles").remove();
article.find(".copyright").wrapInner("<small></small>").wrapInner("<figure></figure>");
article.find("figure.wp-block-pullquote").children().unwrap();
article.find("div.block-explanation-note").wrapInner("<blockquote></blockquote>");
article.find("div.wp-block-tcc-author-note").wrapInner("<em></em>").after("<hr>");
article.find("p.has-small-font-size").wrapInner("<small></small>");
return art(path.join(__dirname, "templates/description-4a91cdc4.art"), {
standfirst: $(".pp-header-group__standfirst").html(),
coverImage: $(".pp-media__image").attr("src"),
coverCaption: $(".pp-media__caption").html(),
article: article.html()
});
}
async function fetchWebPage(url) {
const $ = load(await ofetch_default(url.href));
const article = $(".ghost-content");
article.find(".kg-card, .gh-post-upgrade-cta").remove();
return art(path.join(__dirname, "templates/description-4a91cdc4.art"), {
standfirst: $("p.caption1").html(),
coverImage: $(".post-hero .object-cover").attr("src")?.replace("/size/w30", ""),
coverCaption: $(".post-hero figcaption").html(),
article: article.html()
});
}
async function handler(ctx) {
const category = ctx.req.param("category") ?? "latest_sc";
const metadata = (await cache_default.tryGet(new URL("timelines.json", appUrl).href, async () => await getUA(new URL("timelines.json", appUrl).href), config.cache.routeExpire, false)).timelines.find((timeline) => timeline.id === category);
const feed = (await getUA(new URL(metadata.feed, appUrl).href)).stories.filter((item) => item.type === "article");
const items = await Promise.all(feed.map((item) => cache_default.tryGet(item.shareurl, async () => {
const url = new URL(item.shareurl);
item.link = url.href;
item.description = item.summary;
item.pubDate = item.published;
item.category = [];
if (item.section) item.category = [...item.category, item.section];
if (item.taxonomy) {
if (item.taxonomy.collection_tag) item.category = [...item.category, ...item.taxonomy.collection_tag];
if (item.taxonomy.sections) item.category = [...item.category, ...item.taxonomy.sections];
}
item.category = [...new Set(item.category)];
try {
switch (url.hostname) {
case "app.theinitium.com":
item.description = await fetchAppPage(url) ?? item.description;
break;
case "theinitium.com":
item.description = await fetchWebPage(url) ?? item.description;
break;
default: break;
}
} catch (error) {
if (error?.response?.status === 404) {}
}
return item;
})));
let lang = "zh-hans";
let name = "端传媒";
if (metadata.timeline_sets[0] === "chinese-traditional") {
lang = "zh-hant";
name = "端傳媒";
}
return {
title: `${name} - ${metadata.title}`,
link: `https://app.theinitium.com/t/latest/${category}/`,
language: lang,
item: items
};
}
//#endregion
export { route };