everyrss 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +138 -2
- package/bin.d.ts +2 -0
- package/bin.js +5 -0
- package/cli.d.ts +4 -0
- package/cli.js +66 -0
- package/engine.d.ts +34 -0
- package/engine.js +165 -0
- package/errors.d.ts +36 -0
- package/errors.js +35 -0
- package/formats.d.ts +25 -0
- package/formats.js +104 -0
- package/http.d.ts +19 -0
- package/http.js +52 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/package.json +53 -4
- package/plugin.d.ts +81 -0
- package/plugin.js +46 -0
- package/schema.d.ts +46 -0
- package/schema.js +28 -0
- package/spotify.d.ts +12 -0
- package/spotify.js +111 -0
- package/store.d.ts +15 -0
- package/store.js +17 -0
- package/youtube.d.ts +25 -0
- package/youtube.js +223 -0
package/youtube.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { DateTime, Effect, Schema } from "effect";
|
|
2
|
+
import { XMLParser, XMLValidator } from "fast-xml-parser";
|
|
3
|
+
import { NotFound, RateLimited, retryAfter, UpstreamError } from "./errors.js";
|
|
4
|
+
import { definePlugin, filter } from "./plugin.js";
|
|
5
|
+
/** YouTube's generated playlists per channel: long-form uploads, Shorts and livestreams. */
|
|
6
|
+
const lists = { videos: "UULF", shorts: "UUSH", live: "UULV" };
|
|
7
|
+
const kinds = { videos: "video", shorts: "short", live: "live" };
|
|
8
|
+
const channelPattern = /^UC[\w-]{22}$/;
|
|
9
|
+
const hosts = ["youtube.com", "www.youtube.com", "m.youtube.com", "music.youtube.com"];
|
|
10
|
+
// Skips the cookie consent interstitial that some regions get instead of the page.
|
|
11
|
+
const pageHeaders = { cookie: "SOCS=CAI", "accept-language": "en" };
|
|
12
|
+
const Atom = Schema.Struct({
|
|
13
|
+
feed: Schema.Struct({
|
|
14
|
+
title: Schema.optionalKey(Schema.String),
|
|
15
|
+
entry: Schema.optionalKey(Schema.Array(Schema.Struct({
|
|
16
|
+
videoId: Schema.String,
|
|
17
|
+
title: Schema.String,
|
|
18
|
+
published: Schema.DateTimeUtcFromString,
|
|
19
|
+
author: Schema.Struct({ name: Schema.String }),
|
|
20
|
+
group: Schema.Struct({
|
|
21
|
+
description: Schema.optionalKey(Schema.String),
|
|
22
|
+
thumbnail: Schema.optionalKey(Schema.Struct({ url: Schema.String })),
|
|
23
|
+
}),
|
|
24
|
+
}))),
|
|
25
|
+
}),
|
|
26
|
+
});
|
|
27
|
+
const OEmbed = Schema.fromJsonString(Schema.Struct({ author_url: Schema.String }));
|
|
28
|
+
const Thumbnail = Schema.optionalKey(Schema.Struct({ url: Schema.String }));
|
|
29
|
+
const Channels = Schema.fromJsonString(Schema.Struct({
|
|
30
|
+
items: Schema.optionalKey(Schema.Array(Schema.Struct({
|
|
31
|
+
id: Schema.String,
|
|
32
|
+
snippet: Schema.Struct({
|
|
33
|
+
title: Schema.String,
|
|
34
|
+
thumbnails: Schema.optionalKey(Schema.Struct({ high: Thumbnail, default: Thumbnail })),
|
|
35
|
+
}),
|
|
36
|
+
}))),
|
|
37
|
+
}));
|
|
38
|
+
const parser = new XMLParser({
|
|
39
|
+
ignoreAttributes: false,
|
|
40
|
+
attributeNamePrefix: "",
|
|
41
|
+
removeNSPrefix: true,
|
|
42
|
+
parseTagValue: false,
|
|
43
|
+
parseAttributeValue: false,
|
|
44
|
+
isArray: (_name, path) => path === "feed.entry",
|
|
45
|
+
});
|
|
46
|
+
function failure(response, now, what) {
|
|
47
|
+
if (response.status === 429 || response.status === 403)
|
|
48
|
+
return new RateLimited({
|
|
49
|
+
message: `YouTube is limiting requests (${response.status} on ${URL.canParse(response.url) ? new URL(response.url).pathname : "?"})`,
|
|
50
|
+
retryAfter: retryAfter(response.headers["retry-after"], now),
|
|
51
|
+
});
|
|
52
|
+
if (response.status === 404)
|
|
53
|
+
return new NotFound({ message: `${what} not found on YouTube` });
|
|
54
|
+
return new UpstreamError({ message: `YouTube returned ${response.status}` });
|
|
55
|
+
}
|
|
56
|
+
const decodeEntities = (value) => value
|
|
57
|
+
.replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)))
|
|
58
|
+
.replace(/&#x([\da-f]+);/gi, (_, code) => String.fromCodePoint(Number.parseInt(code, 16)))
|
|
59
|
+
.replace(/"/g, '"')
|
|
60
|
+
.replace(/'/g, "'")
|
|
61
|
+
.replace(/</g, "<")
|
|
62
|
+
.replace(/>/g, ">")
|
|
63
|
+
.replace(/&/g, "&");
|
|
64
|
+
const meta = (html, property) => {
|
|
65
|
+
const value = new RegExp(`<meta property="${property}" content="([^"]*)"`).exec(html)?.[1];
|
|
66
|
+
return value === undefined ? undefined : decodeEntities(value);
|
|
67
|
+
};
|
|
68
|
+
const parseChannelPage = (html) => {
|
|
69
|
+
const id = /<link rel="canonical" href="https:\/\/www\.youtube\.com\/channel\/(UC[\w-]{22})"/.exec(html)?.[1] ?? /"externalId":"(UC[\w-]{22})"/.exec(html)?.[1];
|
|
70
|
+
const name = meta(html, "og:title");
|
|
71
|
+
if (id === undefined || name === undefined)
|
|
72
|
+
return undefined;
|
|
73
|
+
const image = meta(html, "og:image");
|
|
74
|
+
return { id, name, image: image?.startsWith("https://") ? image : null, links: sameAs(html) };
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* The links in the channel's "More info", from the page's schema.org data. Some pages embed
|
|
78
|
+
* that data as an escaped string, so quotes and slashes may be backslash-escaped.
|
|
79
|
+
*/
|
|
80
|
+
function sameAs(html) {
|
|
81
|
+
const list = /\\?"sameAs\\?":\[([^\]]*)\]/.exec(html)?.[1];
|
|
82
|
+
if (list === undefined)
|
|
83
|
+
return [];
|
|
84
|
+
return [...list.matchAll(/https:(?:\\?\/){2}(?:\\\/|[^"\\\s])+/g)].map((match) => match[0].replaceAll("\\/", "/"));
|
|
85
|
+
}
|
|
86
|
+
const parsePlaylist = Effect.fn("youtube.parsePlaylist")(function* (text, list) {
|
|
87
|
+
if (XMLValidator.validate(text) !== true)
|
|
88
|
+
return yield* new UpstreamError({ message: "YouTube returned an invalid feed" });
|
|
89
|
+
const feed = yield* Schema.decodeUnknownEffect(Atom)(parser.parse(text)).pipe(Effect.mapError(() => new UpstreamError({ message: "YouTube returned an unexpected feed" })));
|
|
90
|
+
return (feed.feed.entry ?? []).map((entry) => ({
|
|
91
|
+
key: `youtube:${entry.videoId}`,
|
|
92
|
+
url: list === "shorts"
|
|
93
|
+
? `https://www.youtube.com/shorts/${entry.videoId}`
|
|
94
|
+
: `https://www.youtube.com/watch?v=${entry.videoId}`,
|
|
95
|
+
title: entry.title,
|
|
96
|
+
description: entry.group.description ?? "",
|
|
97
|
+
author: entry.author.name,
|
|
98
|
+
publishedAt: DateTime.toEpochMillis(entry.published),
|
|
99
|
+
duration: null,
|
|
100
|
+
image: entry.group.thumbnail?.url ?? `https://i.ytimg.com/vi/${entry.videoId}/hqdefault.jpg`,
|
|
101
|
+
kind: kinds[list],
|
|
102
|
+
metadata: { youtube: { videoId: entry.videoId } },
|
|
103
|
+
}));
|
|
104
|
+
});
|
|
105
|
+
const videoId = (url) => {
|
|
106
|
+
if (url.hostname === "youtu.be")
|
|
107
|
+
return url.pathname.slice(1).split("/")[0] || undefined;
|
|
108
|
+
if (url.pathname === "/watch")
|
|
109
|
+
return url.searchParams.get("v") ?? undefined;
|
|
110
|
+
return /^\/(?:shorts|live|embed)\/([\w-]+)/.exec(url.pathname)?.[1];
|
|
111
|
+
};
|
|
112
|
+
const channelUrl = (id) => `https://www.youtube.com/channel/${id}`;
|
|
113
|
+
const fromFeed = Effect.fn("youtube.fromFeed")(function* (id, context) {
|
|
114
|
+
const response = yield* context.http.get(`https://www.youtube.com/feeds/videos.xml?channel_id=${id}`);
|
|
115
|
+
if (response.status !== 200)
|
|
116
|
+
return yield* failure(response, yield* context.now, "Channel");
|
|
117
|
+
if (XMLValidator.validate(response.text) !== true)
|
|
118
|
+
return yield* new UpstreamError({ message: "YouTube returned an invalid feed" });
|
|
119
|
+
const feed = yield* Schema.decodeUnknownEffect(Atom)(parser.parse(response.text)).pipe(Effect.mapError(() => new UpstreamError({ message: "YouTube returned an unexpected feed" })));
|
|
120
|
+
const name = feed.feed.title ?? feed.feed.entry?.[0]?.author.name ?? id;
|
|
121
|
+
return { key: id, url: channelUrl(id), name, image: null, links: [] };
|
|
122
|
+
});
|
|
123
|
+
/** A handle through the YouTube Data API, for when the channel page is behind a captcha. */
|
|
124
|
+
const fromApi = Effect.fn("youtube.fromApi")(function* (handle, apiKey, context) {
|
|
125
|
+
const response = yield* context.http.get(`https://www.googleapis.com/youtube/v3/channels?part=snippet&forHandle=${encodeURIComponent(handle)}&key=${encodeURIComponent(apiKey)}`);
|
|
126
|
+
if (response.status !== 200)
|
|
127
|
+
return yield* failure(response, yield* context.now, "Channel");
|
|
128
|
+
const result = yield* Schema.decodeEffect(Channels)(response.text).pipe(Effect.mapError(() => new UpstreamError({ message: "YouTube returned an unexpected channel" })));
|
|
129
|
+
const channel = result.items?.[0];
|
|
130
|
+
if (channel === undefined || !channelPattern.test(channel.id))
|
|
131
|
+
return yield* new NotFound({ message: "Channel not found on YouTube" });
|
|
132
|
+
const thumbnails = channel.snippet.thumbnails;
|
|
133
|
+
const image = thumbnails?.high?.url ?? thumbnails?.default?.url;
|
|
134
|
+
return {
|
|
135
|
+
key: channel.id,
|
|
136
|
+
url: channelUrl(channel.id),
|
|
137
|
+
name: channel.snippet.title,
|
|
138
|
+
image: image?.startsWith("https://") ? image : null,
|
|
139
|
+
links: [],
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
const resolve = Effect.fn("youtube.resolve")(function* (url, context, apiKey) {
|
|
143
|
+
let page = url;
|
|
144
|
+
const video = videoId(url);
|
|
145
|
+
if (video !== undefined) {
|
|
146
|
+
const watch = `https://www.youtube.com/watch?v=${encodeURIComponent(video)}`;
|
|
147
|
+
const response = yield* context.http.get(`https://www.youtube.com/oembed?format=json&url=${encodeURIComponent(watch)}`);
|
|
148
|
+
if (response.status !== 200)
|
|
149
|
+
return yield* failure(response, yield* context.now, "Video");
|
|
150
|
+
const embed = yield* Schema.decodeEffect(OEmbed)(response.text).pipe(Effect.mapError(() => new UpstreamError({ message: "YouTube returned an unexpected video" })));
|
|
151
|
+
page = new URL(embed.author_url);
|
|
152
|
+
}
|
|
153
|
+
const response = yield* context.http.get(`https://www.youtube.com${page.pathname.replace(/\/(videos|shorts|streams|featured)\/?$/, "")}`, pageHeaders);
|
|
154
|
+
const known = /^\/channel\/(UC[\w-]{22})/.exec(page.pathname)?.[1];
|
|
155
|
+
// YouTube sometimes answers servers with a captcha. The channel's feed still names it.
|
|
156
|
+
if (response.status !== 200 && known !== undefined && response.status !== 404)
|
|
157
|
+
return yield* fromFeed(known, context);
|
|
158
|
+
// A handle has no feed of its own, but the Data API can look it up.
|
|
159
|
+
const handle = /^\/(@[^/]+)/.exec(page.pathname)?.[1];
|
|
160
|
+
const fallback = apiKey !== undefined && handle !== undefined && response.status !== 404;
|
|
161
|
+
if (response.status !== 200)
|
|
162
|
+
return yield* fallback
|
|
163
|
+
? fromApi(decodeURIComponent(handle), apiKey, context)
|
|
164
|
+
: failure(response, yield* context.now, "Channel");
|
|
165
|
+
const channel = parseChannelPage(response.text);
|
|
166
|
+
if (!channel && fallback)
|
|
167
|
+
return yield* fromApi(decodeURIComponent(handle), apiKey, context);
|
|
168
|
+
if (!channel)
|
|
169
|
+
return yield* new UpstreamError({ message: "Could not read the YouTube channel page" });
|
|
170
|
+
return {
|
|
171
|
+
key: channel.id,
|
|
172
|
+
url: channelUrl(channel.id),
|
|
173
|
+
name: channel.name,
|
|
174
|
+
image: channel.image,
|
|
175
|
+
links: channel.links,
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
/** YouTube channels through their public feeds. An API key is an optional fallback. */
|
|
179
|
+
export function youtube(options = {}) {
|
|
180
|
+
const selected = options.lists ?? ["videos", "shorts", "live"];
|
|
181
|
+
const fetchList = Effect.fn("youtube.fetchList")(function* (source, list, context) {
|
|
182
|
+
const response = yield* context.http.get(`https://www.youtube.com/feeds/videos.xml?playlist_id=${lists[list]}${source.key.slice(2)}`);
|
|
183
|
+
// A channel that never posted to a list has no playlist for it.
|
|
184
|
+
if (response.status === 404)
|
|
185
|
+
return [];
|
|
186
|
+
if (response.status !== 200)
|
|
187
|
+
return yield* failure(response, yield* context.now, "Channel");
|
|
188
|
+
return yield* parsePlaylist(response.text, list);
|
|
189
|
+
});
|
|
190
|
+
const provider = {
|
|
191
|
+
id: "youtube",
|
|
192
|
+
matches: (url) => (url.protocol === "https:" || url.protocol === "http:") &&
|
|
193
|
+
(url.hostname === "youtu.be" || hosts.includes(url.hostname)),
|
|
194
|
+
ref: (url) => {
|
|
195
|
+
if (url.hostname === "youtu.be")
|
|
196
|
+
return undefined;
|
|
197
|
+
const handle = /^\/(@[^/]+)/.exec(url.pathname)?.[1];
|
|
198
|
+
if (handle !== undefined)
|
|
199
|
+
return decodeURIComponent(handle).toLowerCase();
|
|
200
|
+
const id = url.pathname === "/feeds/videos.xml"
|
|
201
|
+
? url.searchParams.get("channel_id")
|
|
202
|
+
: /^\/channel\/([^/]+)/.exec(url.pathname)?.[1];
|
|
203
|
+
return id && channelPattern.test(id) ? id : undefined;
|
|
204
|
+
},
|
|
205
|
+
url: (ref) => {
|
|
206
|
+
if (channelPattern.test(ref))
|
|
207
|
+
return new URL(channelUrl(ref));
|
|
208
|
+
if (/^@[^/?#\s]{1,100}$/.test(ref))
|
|
209
|
+
return new URL(`https://www.youtube.com/@${encodeURIComponent(ref.slice(1))}`);
|
|
210
|
+
return undefined;
|
|
211
|
+
},
|
|
212
|
+
resolve: (url, context) => resolve(url, context, options.apiKey),
|
|
213
|
+
fetch: (source, context) => Effect.forEach(selected, (list) => fetchList(source, list, context), {
|
|
214
|
+
concurrency: "unbounded",
|
|
215
|
+
}).pipe(Effect.map((results) => {
|
|
216
|
+
const items = results.flat();
|
|
217
|
+
return { items, ...(items[0] ? { name: items[0].author } : {}) };
|
|
218
|
+
})),
|
|
219
|
+
};
|
|
220
|
+
return definePlugin({ id: "youtube", providers: [provider] });
|
|
221
|
+
}
|
|
222
|
+
export const hideShorts = () => filter((item) => item.kind !== "short", { id: "hide-shorts" });
|
|
223
|
+
export const hideLiveStreams = () => filter((item) => item.kind !== "live", { id: "hide-live-streams" });
|