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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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"
? extractor.extractFromJson(json, options)
: extractor.extractFromXml(text, options);
};
interface FeedEntry {
id: string;
link?: string;
title?: string;
description?: string;
published?: Date;
}
interface FeedData {
link?: string;
title?: string;
description?: string;
generator?: string;
language?: string;
published?: Date;
entries?: Array<FeedEntry>;
error?: true;
url: string;
}
function Entry({ feed, entry }: {
feed: FeedData;
entry: FeedEntry;
}) {
return html`<li id=${entry.id} ...${
feed.language ? { lang: feed.language } : {}
}><b>${
entry.link
? html`<a target="_blank" href=${entry.link}>${entry.title}</a>`
: entry.title
}</b><div style="opacity:0.75">${
feed.link
? html`<a target="_blank" style="color:inherit;text-decoration:none" href=${feed.link}>${feed.title}</a>`
: feed.title
}
${
entry.published &&
" - " + timeAgo.format(entry.published)
}</div></li>`;
}
const urls = [req.query.feed || []].flat();
let errorCount = 0;
const feeds = await Promise.all(urls.map(async (url) => {
let realUrl = url;
if (realUrl[0] === "@") {
realUrl = "https://api.val.town/v1/express/" +
encodeURIComponent(realUrl.slice(1));
}
let text;