rsshub
Version:
Make RSS Great Again!
58 lines (56 loc) • 2.77 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./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 { n as parseRelativeDate } from "./parse-date-BrP7mxXf.mjs";
import "./not-found-Z_3JX2qs.mjs";
import "./cache-Bo__VnGm.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import { s as isYouTubeChannelId } from "./google-Z2XIyS1r.mjs";
import path from "node:path";
import { load } from "cheerio";
//#region lib/routes/youtube/community.ts
init_esm_shims();
const route = {
path: "/community/:handle",
categories: ["social-media"],
example: "/youtube/community/@JFlaMusic",
parameters: { handle: "YouTube handles or channel id" },
name: "Community Posts",
maintainers: ["TonyRL"],
handler
};
async function handler(ctx) {
const handle = ctx.req.param("handle");
let urlPath = handle;
if (isYouTubeChannelId(handle)) urlPath = `channel/${handle}`;
const $ = load(await ofetch_default(`https://www.youtube.com/${urlPath}/posts`));
const ytInitialData = JSON.parse($("script").text().match(/ytInitialData = ({.*?});/)?.[1] ?? "{}");
const channelMetadata = ytInitialData.metadata.channelMetadataRenderer;
const username = channelMetadata.title;
const list = ytInitialData.contents.twoColumnBrowseResultsRenderer.tabs.find((tab) => tab.tabRenderer.endpoint.commandMetadata.webCommandMetadata.url.endsWith("/posts") || tab.tabRenderer.endpoint.commandMetadata.webCommandMetadata.url.endsWith("/community")).tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents;
if (list[0].messageRenderer) throw new Error(list[0].messageRenderer.text.runs[0].text);
const items = list.filter((i) => i.backstagePostThreadRenderer).map((item) => {
const post = item.backstagePostThreadRenderer.post.backstagePostRenderer || item.backstagePostThreadRenderer.post.sharedPostRenderer.originalPost.backstagePostRenderer;
const media = post.backstageAttachment?.postMultiImageRenderer?.images.map((i) => i.backstageImageRenderer.image.thumbnails.pop()) ?? [post.backstageAttachment?.backstageImageRenderer?.image.thumbnails.pop()];
return {
title: post.contentText.runs?.[0].text ?? "",
description: art(path.join(__dirname, "templates/community-2ff132c4.art"), {
runs: post.contentText.runs,
media
}),
link: `https://www.youtube.com/post/${post.postId}`,
author: post.authorText.runs[0].text,
pubDate: parseRelativeDate(post.publishedTimeText.runs[0].text.split("(")[0])
};
});
return {
title: `${username} - Community Posts- YouTube`,
link: channelMetadata.channelUrl,
description: channelMetadata.description,
item: items
};
}
//#endregion
export { route };