iakovos-fetchandparsefeeds.web.val.run
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
import { extractContent } from "https://esm.town/v/iakovos/extractContent";
import { fetchText } from "https://esm.town/v/iakovos/fetchText";
import { getLink } from "https://esm.town/v/iakovos/getLink";
import { getMediaAttributeUrl } from "https://esm.town/v/iakovos/getMediaAttributeUrl";
export const fetchAndParseFeeds = async (url: string): Promise<FeedItem[]> => {
try {
const { xml2js } = await import("https://deno.land/x/xml2js@1.0.0/mod.ts");
let { parse } = await import("npm:node-html-parser");
const xml = await fetchText(url);
if (!xml) {
return [];
}
const json = xml2js(xml, { compact: true });
const items = json.rss?.channel?.item || json.feed?.entry;
const parsedItems: FeedItem[] = items?.map((item) => {
const content = extractContent(item);
const document = content && parse(content);
const mediaThumbnail = getMediaAttributeUrl(
item["media:thumbnail"],
);
const mediaContent = getMediaAttributeUrl(
item["media:content"],
);
const paragraph = document?.querySelector("p:not(.caption)")?.textContent?.trim() ?? "";
const imgTag = document?.querySelector("img");
const imgSrc = imgTag?.getAttribute("src");
const image = imgSrc || mediaThumbnail || mediaContent || null;
const link = getLink(item);
const { _text, _cdata } = item.title ?? {};
const title = _text ?? _cdata ?? "";
const pubDate = item.pubDate?._text ?? item.updated?._text ?? "";
const description = paragraph || item.description?._text
|| item.description?._cdata || "";
return { title, description, image, pubDate, link };
}) ?? [];
return parsedItems;
} catch (error) {
console.error("Error while fetching and parsing feeds:", error);
return [];
}
};
interface FeedItem {
title: string;
description: string;
image: string | null;
pubDate: string;
link: string;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 24, 2023