rsshub
Version:
Make RSS Great Again!
79 lines (78 loc) • 2.61 kB
JavaScript
import { t as rofetch } from "./ofetch-eUBdfMpp.mjs";
import { t as parseDate } from "./parse-date-CjhZE69i.mjs";
//#region lib/routes/forwardfuture/originals.ts
function extractPostsArray(rscPayload) {
let searchStart = 0;
while (searchStart < rscPayload.length) {
const postsIdx = rscPayload.indexOf("\"posts\":", searchStart);
if (postsIdx === -1) return null;
const arrayStart = postsIdx + 8;
if (rscPayload[arrayStart] !== "[") {
searchStart = arrayStart;
continue;
}
let depth = 0;
let inString = false;
let escape = false;
let i = arrayStart;
for (; i < rscPayload.length; i++) {
const c = rscPayload[i];
if (escape) escape = false;
else if (c === "\\" && inString) escape = true;
else if (c === "\"" && !escape) inString = !inString;
else if (!inString) {
if (c === "[") depth++;
else if (c === "]") {
depth--;
if (depth === 0) return JSON.parse(rscPayload.slice(arrayStart, i + 1));
}
}
}
searchStart = arrayStart + 1;
}
return null;
}
const route = {
name: "Originals",
categories: ["other"],
path: "/originals",
example: "/forwardfuture/originals",
radar: [{ source: ["forwardfuture.com/originals", "forwardfuture.com/"] }],
handler,
maintainers: ["ovo-Tim"],
description: "Original essays, columns, and analysis on AI from Forward Future contributors."
};
async function handler(ctx) {
const limit = ctx.req.query("limit") ? Number(ctx.req.query("limit")) : 30;
const html = await rofetch("https://forwardfuture.com/originals");
const pushRegex = /__next_f\.push\(\[1,"((?:[^"\\]|\\.)*)"\]\)/g;
let match;
let posts = [];
while ((match = pushRegex.exec(html)) !== null) {
const payload = match[1];
if (!payload.includes("posts")) continue;
const extracted = extractPostsArray(JSON.parse(`"${payload}"`));
if (extracted) {
posts = extracted;
break;
}
}
if (posts.length === 0) throw new Error("Failed to extract posts from the Next.js RSC payload");
return {
title: "Forward Future - Originals",
link: "https://forwardfuture.com/originals",
description: "Original essays, columns, and analysis on AI from Forward Future contributors.",
item: posts.slice(0, limit).map((post) => ({
title: post.title,
description: post.summary || void 0,
link: post.url,
pubDate: parseDate(post.dateUnix, "X"),
author: post.authors.join(", "),
image: post.thumbnail,
category: post.categories.length > 0 ? post.categories : post.category === "General" ? void 0 : [post.category]
})),
image: "https://forwardfuture.com/images/logos/ff-icon.svg"
};
}
//#endregion
export { route };