forked from elliotBraem/efizzybot
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuse-rss-feed.ts
More file actions
132 lines (113 loc) · 3.62 KB
/
use-rss-feed.ts
File metadata and controls
132 lines (113 loc) · 3.62 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useFeed } from "@/lib/api";
import { RssFeedItem } from "@/types/rss";
import { useQuery } from "@tanstack/react-query";
interface RssFeedData {
title: string;
description: string;
link: string;
items: RssFeedItem[];
}
async function fetchRssFeed(serviceUrl: string): Promise<RssFeedData> {
const response = await fetch(serviceUrl);
if (!response.ok) {
throw new Error(`Failed to fetch RSS feed: ${response.status}`);
}
const xmlText = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, "text/xml");
// Check for parsing errors
const parseError = xmlDoc.querySelector("parsererror");
if (parseError) {
throw new Error("Invalid XML format");
}
const channel = xmlDoc.querySelector("channel");
if (!channel) {
throw new Error("Invalid RSS feed format");
}
const title = channel.querySelector("title")?.textContent || "";
const description = channel.querySelector("description")?.textContent || "";
const link = channel.querySelector("link")?.textContent || "";
const items = Array.from(channel.querySelectorAll("item")).map((item) => {
const link = item.querySelector("link")?.textContent || "";
const description = item.querySelector("description")?.textContent || "";
// Extract image from various sources
let image =
item.querySelector("enclosure[type^='image']")?.getAttribute("url") || "";
if (!image) {
image =
item
.querySelector("media\\:content[type^='image']")
?.getAttribute("url") || "";
}
if (!image) {
image =
item.querySelector("media\\:thumbnail")?.getAttribute("url") || "";
}
if (!image && description) {
const imgMatch = description.match(/<img[^>]+src="([^"]+)"/);
if (imgMatch) {
image = imgMatch[1];
}
}
// Determine platform from link
let platform = "other";
if (link.includes("twitter.com") || link.includes("x.com")) {
platform = "twitter";
} else if (link.includes("youtube.com") || link.includes("youtu.be")) {
platform = "youtube";
} else if (link.includes("github.com")) {
platform = "github";
} else if (link.includes("reddit.com")) {
platform = "reddit";
}
// Extract categories
const categories = Array.from(item.querySelectorAll("category"))
.map((cat) => cat.textContent?.trim() || "")
.filter(Boolean);
return {
title: item.querySelector("title")?.textContent || "",
link,
description,
pubDate: item.querySelector("pubDate")?.textContent || "",
guid: item.querySelector("guid")?.textContent || "",
image,
platform,
categories,
};
});
return { title, description, link, items };
}
export function useRssFeed(feedId: string) {
const { data: feedData } = useFeed(feedId);
const rssFeed = feedData?.config.outputs.stream?.distribute?.find(
(distribute) => distribute.plugin === "@curatedotfun/rss",
);
const hasRssFeed = Boolean(rssFeed);
const serviceUrl = `${rssFeed?.config?.serviceUrl}/rss.xml`;
const {
data: rssData,
error,
isLoading,
isError,
} = useQuery({
queryKey: ["rss-feed", feedId, serviceUrl],
queryFn: () => fetchRssFeed(serviceUrl!),
enabled: hasRssFeed && Boolean(serviceUrl),
staleTime: 5 * 60 * 1000, // 5 minutes
retry: 2,
});
return {
hasRssFeed,
rssData: rssData?.items || [],
feedInfo: rssData
? {
title: rssData.title,
description: rssData.description,
link: rssData.link,
}
: null,
isLoading,
isError,
error: error?.message,
};
}