rsshub
Version:
Make RSS Great Again!
85 lines (83 loc) • 4.17 kB
JavaScript
import { n as parseRelativeDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import { i as getSrtAttachmentBatch, l as utils_default, o as getVideoUrl } from "./google-Z2XIyS1r.mjs";
import { Innertube } from "youtubei.js";
//#region lib/routes/youtube/api/youtubei.ts
const innertubePromise = Innertube.create({ fetch: (input, init) => {
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
return fetch(url, {
method: input?.method,
...init
});
} });
const getChannelIdByUsername = (username) => cache_default.tryGet(`youtube:getChannelIdByUsername:${username}`, async () => {
return (await (await innertubePromise).resolveURL(`https://www.youtube.com/${username}`)).payload.browseId;
});
const getDataByUsername = async ({ username, embed, filterShorts, isJsonFeed }) => {
return getDataByChannelId({
channelId: await getChannelIdByUsername(username),
embed,
filterShorts,
isJsonFeed
});
};
const getDataByChannelId = async ({ channelId, embed, isJsonFeed }) => {
const channel = await (await innertubePromise).getChannel(channelId);
const videos = await channel.getVideos();
const videoSubtitles = isJsonFeed ? await getSrtAttachmentBatch(videos.videos.filter((video) => "video_id" in video).map((video) => video.video_id)) : {};
return {
title: `${channel.metadata.title || channelId} - YouTube`,
link: `https://www.youtube.com/channel/${channelId}`,
image: channel.metadata.avatar?.[0].url,
description: channel.metadata.description,
item: await Promise.all(videos.videos.filter((video) => "video_id" in video).map((video) => {
const srtAttachments = isJsonFeed ? videoSubtitles[video.video_id] || [] : [];
const img = "best_thumbnail" in video ? video.best_thumbnail?.url : "thumbnails" in video ? video.thumbnails?.[0]?.url : void 0;
return {
title: video.title.text || `YouTube Video ${video.video_id}`,
description: "description_snippet" in video ? utils_default.renderDescription(embed, video.video_id, img, utils_default.formatDescription(video.description_snippet?.toHTML())) : null,
link: `https://www.youtube.com/watch?v=${video.video_id}`,
author: typeof video.author === "string" ? video.author : video.author.name === "N/A" ? void 0 : video.author.name,
image: img,
pubDate: "published" in video && video.published?.text ? parseRelativeDate(video.published.text) : void 0,
attachments: [{
url: getVideoUrl(video.video_id),
mime_type: "text/html",
duration_in_seconds: video.duration && "seconds" in video.duration ? video.duration.seconds : void 0
}, ...srtAttachments]
};
}))
};
};
const getDataByPlaylistId = async ({ playlistId, embed }) => {
const playlist = await (await innertubePromise).getPlaylist(playlistId);
const videos = await playlist.videos;
return {
title: `${playlist.info.title || playlistId} by ${playlist.info.author.name} - YouTube`,
link: `https://www.youtube.com/playlist?list=${playlistId}`,
image: playlist.info.thumbnails?.[0].url,
description: playlist.info.description || `${playlist.info.title} by ${playlist.info.author.name}`,
item: videos.filter((video) => "id" in video).map((video) => {
const img = "best_thumbnail" in video ? video.best_thumbnail?.url : video.thumbnails?.[0]?.url;
return {
title: video.title.text || `YouTube Video ${video.id}`,
description: utils_default.renderDescription(embed, video.id, img, ""),
link: `https://www.youtube.com/watch?v=${video.id}`,
pubDate: "published" in video && video.published?.text ? parseRelativeDate(video.published.text) : void 0,
author: "author" in video ? [{
name: video.author.name,
url: video.author.url,
avatar: video.author.thumbnails?.[0]?.url
}] : void 0,
image: img,
attachments: [{
url: getVideoUrl(video.id),
mime_type: "text/html",
duration_in_seconds: "duration" in video && video.duration && "seconds" in video.duration ? video.duration.seconds : void 0
}]
};
})
};
};
//#endregion
export { getDataByPlaylistId as n, getDataByUsername as r, getDataByChannelId as t };