2023-12-18 06:47:08 +00:00
|
|
|
import {
|
|
|
|
json,
|
|
|
|
LoaderFunction,
|
|
|
|
ActionFunction,
|
|
|
|
ActionFunctionArgs,
|
|
|
|
} from "@remix-run/node";
|
|
|
|
import { S5Client } from "@lumeweb/s5-js";
|
|
|
|
import xml2js from "xml2js";
|
|
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
import path from "path";
|
2023-12-24 11:25:32 +00:00
|
|
|
import { getAvailableSites } from "@/utils.js";
|
2023-12-24 13:16:23 +00:00
|
|
|
import { CID } from "@lumeweb/libs5";
|
2023-12-18 06:47:08 +00:00
|
|
|
|
|
|
|
// Action function for POST requests
|
|
|
|
export async function action({ request }: ActionFunctionArgs) {
|
|
|
|
const client = new S5Client("https://s5.web3portal.com");
|
2023-12-18 07:01:47 +00:00
|
|
|
const data = await request.json();
|
2023-12-24 11:25:32 +00:00
|
|
|
|
|
|
|
const site = data.site;
|
|
|
|
const sites = getAvailableSites();
|
|
|
|
|
|
|
|
if (!(site in sites)) {
|
|
|
|
throw new Response("Site does not exist", { status: 404 });
|
|
|
|
}
|
|
|
|
|
|
|
|
const siteInfo = sites[site];
|
|
|
|
|
2023-12-18 06:47:08 +00:00
|
|
|
const meta = (await client.getMetadata(data.cid as string)) as any;
|
|
|
|
const fileMeta = meta.metadata as any;
|
|
|
|
const paths = fileMeta.paths as {
|
|
|
|
[file: string]: {
|
|
|
|
cid: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-12-24 13:16:23 +00:00
|
|
|
// Check if the RSS feed path exists in the paths
|
|
|
|
if (!(siteInfo.rss in paths)) {
|
|
|
|
throw new Response("RSS feed not found", { status: 404 });
|
2023-12-18 06:47:08 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:16:23 +00:00
|
|
|
// Download and parse the RSS feed
|
|
|
|
const rssData = await client.downloadData(paths[siteInfo.rss].cid);
|
|
|
|
const rss = await xml2js.parseStringPromise(rssData);
|
|
|
|
|
|
|
|
// Process each item in the RSS feed
|
|
|
|
for (const item of rss.rss.channel[0].item) {
|
|
|
|
const url = item.link[0];
|
|
|
|
const title = item.title[0]; // Title is directly available from the feed
|
2023-12-18 06:47:08 +00:00
|
|
|
|
|
|
|
let pathname = new URL(url).pathname;
|
|
|
|
|
|
|
|
// Normalize and remove leading and trailing slashes from the path
|
|
|
|
pathname = path.normalize(pathname).replace(/^\/|\/$/g, "");
|
|
|
|
|
|
|
|
// Function to determine if a URL path represents a directory
|
|
|
|
const isDirectory = (pathname: string) => {
|
|
|
|
return !paths.hasOwnProperty(pathname);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the path is a directory and look for a directory index
|
|
|
|
if (isDirectory(pathname)) {
|
|
|
|
for (const file of fileMeta.tryFiles) {
|
|
|
|
const indexPath = path.join(pathname, file);
|
|
|
|
if (paths.hasOwnProperty(indexPath)) {
|
|
|
|
pathname = indexPath;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cid = paths[pathname]?.cid;
|
|
|
|
|
|
|
|
if (cid) {
|
|
|
|
const exists = await prisma.article.findUnique({
|
|
|
|
where: { cid },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
const record = {
|
|
|
|
title,
|
|
|
|
url,
|
2023-12-24 13:16:23 +00:00
|
|
|
cid: CID.decode(cid).toString(),
|
2023-12-18 06:47:08 +00:00
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
2023-12-24 13:18:30 +00:00
|
|
|
site: data.site,
|
2023-12-18 06:47:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Insert a new record into the database
|
|
|
|
await prisma.article.create({
|
|
|
|
data: record,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 07:02:06 +00:00
|
|
|
|
|
|
|
return new Response("", { status: 200 });
|
2023-12-18 06:47:08 +00:00
|
|
|
}
|