Fork
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { fetch } from "https://esm.town/v/std/fetch";
import { basicCss } from "https://esm.town/v/easrng/basicCss";
import { preactWebApp } from "https://esm.town/v/easrng/preactWebApp";
export const rssViewer = preactWebApp(
async function App(
{ html, req, setHeaders }: typeof $easrng.preactWebApp.props,
) {
const { default: TimeAgo } = await import("npm:javascript-time-ago");
const { default: en } = await import("npm:javascript-time-ago/locale/en");
TimeAgo.addDefaultLocale(en);
const timeAgo = new TimeAgo("en-US");
const extractor = await import(
"https://esm.sh/@extractus/feed-extractor@6.2.3/src/main.js"
);
const extract = async (url, options = {}, fetchOptions = {}) => {
const retrieve_default = async (url, options: any = {}) => {
const {
headers = {
"user-agent":
"Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0",
},
} = options;
const res = await fetch(url, { headers, redirect: "follow" });
const status = res.status;
if (status >= 400) {
throw new Error(`Request failed with error code ${status}`);
}
const contentType = res.headers.get("content-type");
const text = await res.text();
if (/(\+|\/)(xml|html)/.test(contentType)) {
return { type: "xml", text: text.trim(), status, contentType };
}
if (/(\+|\/)json/.test(contentType)) {
try {
const data = JSON.parse(text);
return { type: "json", json: data, status, contentType };
}
catch (err) {
throw new Error("Failed to convert data to JSON object");
}
}
throw new Error(`Invalid content type: ${contentType}`);
};
const data = await retrieve_default(url, fetchOptions);
if (!data.text && !data.json) {
throw new Error(`Failed to load content from "${url}"`);
}
const { type, json, text } = data;
return type === "json"