rsshub
Version:
Make RSS Great Again!
79 lines (77 loc) • 2.52 kB
JavaScript
import "./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 { load } from "cheerio";
//#region lib/routes/cognition/blog.ts
const route = {
path: "/blog",
name: "Blog",
url: "cognition.ai/blog",
maintainers: ["Loongphy"],
example: "/cognition/blog",
categories: ["programming"],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{
source: ["cognition.ai/blog/1"],
target: "/blog"
}],
view: ViewType.Articles,
handler
};
const splitAuthors = (text) => {
if (!text) return;
const names = text.split(",").map((name) => name.trim()).filter(Boolean);
if (names.length === 0) return;
return names.map((name) => ({ name }));
};
async function handler() {
const baseUrl = "https://cognition.ai";
const targetUrl = new URL("/blog/1", baseUrl).href;
const $ = load(await ofetch_default(targetUrl));
const items = $("#blog-post-list__list li.blog-post-list__list-item").toArray().map((el) => {
const linkElement = $(el).find("a.o-blog-preview").first();
const href = linkElement.attr("href");
const link = href ? new URL(href, baseUrl).href : void 0;
if (!link) return;
const title = linkElement.find("h3.o-blog-preview__title").text().trim();
if (!title) return;
const summary = linkElement.find("p.o-blog-preview__intro").text().trim();
const dateNode = linkElement.find(".o-blog-preview__meta-date").clone();
dateNode.find(".o-blog-preview__meta").remove();
const dateText = dateNode.text().trim();
const authorText = linkElement.find(".o-blog-preview__meta-author").text().trim();
const dataItem = {
title,
link,
pubDate: parseDate(dateText)
};
if (summary) dataItem.description = summary;
const authors = splitAuthors(authorText);
if (authors) dataItem.author = authors;
return dataItem;
}).filter((item) => item !== void 0);
const imageAttr = $("meta[property=\"og:image\"]").attr("content");
const image = imageAttr ? new URL(imageAttr, baseUrl).href : void 0;
return {
title: $("title").text(),
description: $("meta[name=\"description\"]").attr("content"),
link: targetUrl,
allowEmpty: true,
item: items,
image
};
}
//#endregion
export { handler, route };