-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrss-feed-fetcher.ts
More file actions
92 lines (80 loc) · 2.9 KB
/
rss-feed-fetcher.ts
File metadata and controls
92 lines (80 loc) · 2.9 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
import type { HassEntities } from 'home-assistant-js-websocket';
import { resolveAddonLocalEndpointUrl } from '@/app/utils/home-assistant-connection-target';
import { parseRSSDocument, toRSSItem } from './rss-feed-parser';
import type { RSSItem, RSSProvider } from './types';
const RSS_PROXY_PATH = '/__navet_rss_proxy__';
export function getRSSProxyRequestUrl(feedUrl: string) {
const baseUrl =
typeof window !== 'undefined' && window.location.origin
? window.location.origin
: 'http://localhost';
const proxyUrl = new URL(resolveAddonLocalEndpointUrl(RSS_PROXY_PATH), baseUrl);
proxyUrl.searchParams.set('url', feedUrl);
return proxyUrl.toString();
}
export async function fetchUrlProviderItems(
provider: RSSProvider,
fallbackRecentLabel: string,
formatRelativeTime: (value: number, unit: Intl.RelativeTimeFormatUnit) => string
): Promise<RSSItem[]> {
if (provider.demoItems) {
return provider.demoItems;
}
if (!provider.feedUrl) {
return [];
}
const response = await fetch(getRSSProxyRequestUrl(provider.feedUrl), {
cache: 'no-store',
});
if (!response.ok) {
throw new Error(`unable-to-load:${provider.name}`);
}
const xml = await response.text();
return parseRSSDocument(xml, provider.name, fallbackRecentLabel, formatRelativeTime);
}
export function getHomeAssistantProviderItems(
provider: RSSProvider,
entities: HassEntities | null,
fallbackRecentLabel: string,
formatRelativeTime: (value: number, unit: Intl.RelativeTimeFormatUnit) => string
): RSSItem[] {
if (provider.type !== 'home-assistant-feedreader' || !provider.entityId || !entities) {
return [];
}
const entity = entities[provider.entityId];
if (!entity) {
return [];
}
const attributes = entity.attributes as Record<string, unknown> | undefined;
const link = typeof attributes?.link === 'string' ? attributes.link : undefined;
const title =
(typeof attributes?.title === 'string' && attributes.title) ||
(typeof attributes?.friendly_name === 'string' && attributes.friendly_name) ||
undefined;
if (!link || !title) {
return [];
}
return [
toRSSItem({
id: provider.entityId,
title,
providerName: provider.name,
link,
fallbackRecentLabel,
formatRelativeTime,
publishedAtRaw:
(typeof attributes?.published === 'string' && attributes.published) ||
(typeof attributes?.pubDate === 'string' && attributes.pubDate) ||
(typeof attributes?.updated === 'string' && attributes.updated) ||
undefined,
excerpt:
(typeof attributes?.summary === 'string' && attributes.summary) ||
(typeof attributes?.description === 'string' && attributes.description) ||
undefined,
imageUrl:
(typeof attributes?.image === 'string' && attributes.image) ||
(typeof attributes?.entity_picture === 'string' && attributes.entity_picture) ||
undefined,
}),
];
}