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/http.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { UpstreamError } from "./errors.js";
|
|
3
|
+
async function readText(response, maxBytes) {
|
|
4
|
+
const reader = response.body?.getReader();
|
|
5
|
+
if (!reader)
|
|
6
|
+
return "";
|
|
7
|
+
const chunks = [];
|
|
8
|
+
let size = 0;
|
|
9
|
+
for (;;) {
|
|
10
|
+
const { done, value } = await reader.read();
|
|
11
|
+
if (done)
|
|
12
|
+
break;
|
|
13
|
+
size += value.byteLength;
|
|
14
|
+
if (size > maxBytes) {
|
|
15
|
+
await reader.cancel();
|
|
16
|
+
throw new Error("too large");
|
|
17
|
+
}
|
|
18
|
+
chunks.push(value);
|
|
19
|
+
}
|
|
20
|
+
const bytes = new Uint8Array(size);
|
|
21
|
+
let offset = 0;
|
|
22
|
+
for (const chunk of chunks) {
|
|
23
|
+
bytes.set(chunk, offset);
|
|
24
|
+
offset += chunk.byteLength;
|
|
25
|
+
}
|
|
26
|
+
return new TextDecoder().decode(bytes);
|
|
27
|
+
}
|
|
28
|
+
/** HTTP through the platform's fetch, with a timeout and a response size cap. */
|
|
29
|
+
export function fetchHttp(options = {}) {
|
|
30
|
+
const { timeout = 10_000, maxBytes = 8_000_000, userAgent = "everyrss" } = options;
|
|
31
|
+
const request = (url, init, headers) => Effect.tryPromise({
|
|
32
|
+
try: async (signal) => {
|
|
33
|
+
const response = await fetch(url, {
|
|
34
|
+
...init,
|
|
35
|
+
headers: { "user-agent": userAgent, ...headers },
|
|
36
|
+
redirect: "follow",
|
|
37
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(timeout)]),
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
status: response.status,
|
|
41
|
+
url: response.url || url,
|
|
42
|
+
headers: Object.fromEntries(response.headers),
|
|
43
|
+
text: await readText(response, maxBytes),
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
catch: () => new UpstreamError({ message: `Could not read ${new URL(url).host}` }),
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
get: (url, headers = {}) => request(url, { method: "GET" }, headers),
|
|
50
|
+
post: (url, body, headers = {}) => request(url, { method: "POST", body }, headers),
|
|
51
|
+
};
|
|
52
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { InvalidInput, NotFound, RateLimited, StorageError, Unavailable, UpstreamError, type EveryRssError, } from "./errors.ts";
|
|
2
|
+
export { Item, Source, type Feed, type StoredSource } from "./schema.ts";
|
|
3
|
+
export { fetchHttp, type FetchHttpOptions, type Http, type HttpReply } from "./http.ts";
|
|
4
|
+
export { definePlugin, filter, transform, type Fetched, type Format, type Plugin, type PluginContext, type Provider, type Resolved, type Step, type StepOptions, } from "./plugin.ts";
|
|
5
|
+
export { memoryStore, type Store } from "./store.ts";
|
|
6
|
+
export { createEngine, type Engine, type EngineOptions, type OpenOptions } from "./engine.ts";
|
package/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { InvalidInput, NotFound, RateLimited, StorageError, Unavailable, UpstreamError, } from "./errors.js";
|
|
2
|
+
export { Item, Source } from "./schema.js";
|
|
3
|
+
export { fetchHttp } from "./http.js";
|
|
4
|
+
export { definePlugin, filter, transform, } from "./plugin.js";
|
|
5
|
+
export { memoryStore } from "./store.js";
|
|
6
|
+
export { createEngine } from "./engine.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "everyrss",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Turn YouTube channels and Spotify shows into RSS and JSON feeds, from the command line or as a library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"feed",
|
|
7
|
+
"json-feed",
|
|
8
|
+
"podcast",
|
|
9
|
+
"rss",
|
|
10
|
+
"spotify",
|
|
11
|
+
"youtube"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://everyrss.com",
|
|
14
|
+
"bugs": "https://github.com/vimtor/everyrss/issues",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/vimtor/everyrss.git",
|
|
19
|
+
"directory": "packages/core"
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"everyrss": "bin.js"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./index.d.ts",
|
|
28
|
+
"default": "./index.js"
|
|
29
|
+
},
|
|
30
|
+
"./youtube": {
|
|
31
|
+
"types": "./youtube.d.ts",
|
|
32
|
+
"default": "./youtube.js"
|
|
33
|
+
},
|
|
34
|
+
"./spotify": {
|
|
35
|
+
"types": "./spotify.d.ts",
|
|
36
|
+
"default": "./spotify.js"
|
|
37
|
+
},
|
|
38
|
+
"./formats": {
|
|
39
|
+
"types": "./formats.d.ts",
|
|
40
|
+
"default": "./formats.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"fast-xml-parser": "5.11.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"effect": "4.2.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"effect": "^4.2.0"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/plugin.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { EveryRssError } from "./errors.ts";
|
|
3
|
+
import type { Http } from "./http.ts";
|
|
4
|
+
import type { Feed, Item, Source } from "./schema.ts";
|
|
5
|
+
export interface PluginContext {
|
|
6
|
+
readonly http: Http;
|
|
7
|
+
readonly now: Effect.Effect<number>;
|
|
8
|
+
}
|
|
9
|
+
export interface Resolved {
|
|
10
|
+
readonly key: string;
|
|
11
|
+
readonly url: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly image: string | null;
|
|
14
|
+
readonly links?: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
export interface Fetched {
|
|
17
|
+
readonly items: readonly Item[];
|
|
18
|
+
/** Updated source details, when upstream reports them. */
|
|
19
|
+
readonly name?: string;
|
|
20
|
+
readonly image?: string | null;
|
|
21
|
+
}
|
|
22
|
+
/** Where content comes from. Its `id` is also the first path segment of its feeds. */
|
|
23
|
+
export interface Provider {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
/** Items kept per source, newest first, so feeds outlive short upstream windows. */
|
|
26
|
+
readonly retain?: number;
|
|
27
|
+
matches(url: URL): boolean;
|
|
28
|
+
/** The path reference for a URL when it's known without network access. */
|
|
29
|
+
ref(url: URL): string | undefined;
|
|
30
|
+
/** The URL a path reference stands for. */
|
|
31
|
+
url(ref: string): URL | undefined;
|
|
32
|
+
resolve(url: URL, context: PluginContext): Effect.Effect<Resolved, EveryRssError>;
|
|
33
|
+
fetch(source: Source, context: PluginContext): Effect.Effect<Fetched, EveryRssError>;
|
|
34
|
+
}
|
|
35
|
+
/** Runs on every item after a refresh. Return null to drop the item. */
|
|
36
|
+
export interface Step {
|
|
37
|
+
readonly id: string;
|
|
38
|
+
/** Set it to save each item's result, for expensive steps. Change it to recompute. */
|
|
39
|
+
readonly version?: string;
|
|
40
|
+
appliesTo?(source: Source): boolean;
|
|
41
|
+
apply(item: Item, source: Source, context: PluginContext): Effect.Effect<Item | null, EveryRssError>;
|
|
42
|
+
}
|
|
43
|
+
export interface Format {
|
|
44
|
+
/** The file name feeds are served under, such as `rss.xml`. */
|
|
45
|
+
readonly file: string;
|
|
46
|
+
readonly contentType: string;
|
|
47
|
+
/** Renders differently on every request, so it must not be cached. */
|
|
48
|
+
readonly live?: boolean;
|
|
49
|
+
render(feed: Feed): string;
|
|
50
|
+
}
|
|
51
|
+
export interface Plugin {
|
|
52
|
+
readonly id: string;
|
|
53
|
+
readonly providers?: readonly Provider[];
|
|
54
|
+
readonly steps?: readonly Step[];
|
|
55
|
+
readonly formats?: readonly Format[];
|
|
56
|
+
}
|
|
57
|
+
export declare const definePlugin: <const P extends Plugin>(plugin: P) => P;
|
|
58
|
+
export declare function registry(plugins: readonly Plugin[]): {
|
|
59
|
+
providers: Map<string, Provider>;
|
|
60
|
+
steps: {
|
|
61
|
+
version?: string;
|
|
62
|
+
appliesTo?(source: Source): boolean;
|
|
63
|
+
apply(item: Item, source: Source, context: PluginContext): Effect.Effect<Item | null, EveryRssError>;
|
|
64
|
+
id: string;
|
|
65
|
+
}[];
|
|
66
|
+
formats: Map<string, Format>;
|
|
67
|
+
defaultFormat: Format;
|
|
68
|
+
};
|
|
69
|
+
export type Registry = ReturnType<typeof registry>;
|
|
70
|
+
export interface StepOptions {
|
|
71
|
+
readonly id?: string;
|
|
72
|
+
/** Save each item's result under this version, for expensive callbacks such as AI calls. */
|
|
73
|
+
readonly version?: string;
|
|
74
|
+
readonly appliesTo?: (source: Source) => boolean;
|
|
75
|
+
}
|
|
76
|
+
type Callback<A> = (item: Item, source: Source) => A | Promise<A>;
|
|
77
|
+
/** Keep the items a callback returns true for. */
|
|
78
|
+
export declare const filter: (predicate: Callback<boolean>, options?: StepOptions) => Plugin;
|
|
79
|
+
/** Replace each item with what a callback returns, or drop it by returning null. */
|
|
80
|
+
export declare const transform: (callback: Callback<Item | null>, options?: StepOptions) => Plugin;
|
|
81
|
+
export {};
|
package/plugin.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { InvalidInput, UpstreamError } from "./errors.js";
|
|
3
|
+
export const definePlugin = (plugin) => plugin;
|
|
4
|
+
export function registry(plugins) {
|
|
5
|
+
const unique = (entries, name, kind) => {
|
|
6
|
+
const map = new Map();
|
|
7
|
+
for (const entry of entries) {
|
|
8
|
+
if (map.has(name(entry)))
|
|
9
|
+
throw new InvalidInput({ message: `Duplicate ${kind}: ${name(entry)}` });
|
|
10
|
+
map.set(name(entry), entry);
|
|
11
|
+
}
|
|
12
|
+
return map;
|
|
13
|
+
};
|
|
14
|
+
unique(plugins, (plugin) => plugin.id, "plugin");
|
|
15
|
+
const providers = unique(plugins.flatMap((plugin) => plugin.providers ?? []), (provider) => provider.id, "provider");
|
|
16
|
+
const steps = [
|
|
17
|
+
...unique(plugins.flatMap((plugin) => (plugin.steps ?? []).map((step) => ({ ...step, id: `${plugin.id}/${step.id}` }))), (step) => step.id, "step").values(),
|
|
18
|
+
];
|
|
19
|
+
const formats = unique(plugins.flatMap((plugin) => plugin.formats ?? []), (format) => format.file, "format");
|
|
20
|
+
const first = formats.values().next();
|
|
21
|
+
if (first.done)
|
|
22
|
+
throw new InvalidInput({ message: "Install a format plugin, for example rss()" });
|
|
23
|
+
return { providers, steps, formats, defaultFormat: first.value };
|
|
24
|
+
}
|
|
25
|
+
function step(name, options, apply) {
|
|
26
|
+
const id = options.id ?? name;
|
|
27
|
+
return {
|
|
28
|
+
id,
|
|
29
|
+
steps: [
|
|
30
|
+
{
|
|
31
|
+
id: name,
|
|
32
|
+
...(options.version === undefined ? {} : { version: options.version }),
|
|
33
|
+
...(options.appliesTo === undefined ? {} : { appliesTo: options.appliesTo }),
|
|
34
|
+
apply,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const call = (id, callback, item, source) => Effect.tryPromise({
|
|
40
|
+
try: () => Promise.resolve(callback(item, source)),
|
|
41
|
+
catch: () => new UpstreamError({ message: `Step ${id} failed` }),
|
|
42
|
+
});
|
|
43
|
+
/** Keep the items a callback returns true for. */
|
|
44
|
+
export const filter = (predicate, options = {}) => step("filter", options, (item, source) => call(options.id ?? "filter", predicate, item, source).pipe(Effect.map((keep) => (keep ? item : null))));
|
|
45
|
+
/** Replace each item with what a callback returns, or drop it by returning null. */
|
|
46
|
+
export const transform = (callback, options = {}) => step("transform", options, (item, source) => call(options.id ?? "transform", callback, item, source));
|
package/schema.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export declare const JsonObject: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
|
|
3
|
+
export type JsonObject = typeof JsonObject.Type;
|
|
4
|
+
export declare const Item: Schema.Struct<{
|
|
5
|
+
/** Provider-scoped identity, such as `youtube:dQw4w9WgXcQ`. */
|
|
6
|
+
readonly key: Schema.String;
|
|
7
|
+
readonly url: Schema.String;
|
|
8
|
+
readonly title: Schema.String;
|
|
9
|
+
readonly description: Schema.String;
|
|
10
|
+
readonly author: Schema.String;
|
|
11
|
+
readonly publishedAt: Schema.Finite;
|
|
12
|
+
/** Milliseconds, or null when the provider doesn't say. */
|
|
13
|
+
readonly duration: Schema.NullOr<Schema.Finite>;
|
|
14
|
+
readonly image: Schema.NullOr<Schema.String>;
|
|
15
|
+
/** Provider-defined, such as `video`, `short`, `live` or `episode`. */
|
|
16
|
+
readonly kind: Schema.String;
|
|
17
|
+
readonly metadata: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
|
|
18
|
+
}>;
|
|
19
|
+
export type Item = typeof Item.Type;
|
|
20
|
+
export declare const Source: Schema.Struct<{
|
|
21
|
+
/** `provider:key`, such as `youtube:UCYO_jab_esuFRV4b17AJtAw`. */
|
|
22
|
+
readonly id: Schema.String;
|
|
23
|
+
readonly provider: Schema.String;
|
|
24
|
+
readonly key: Schema.String;
|
|
25
|
+
readonly url: Schema.String;
|
|
26
|
+
readonly name: Schema.String;
|
|
27
|
+
readonly image: Schema.NullOr<Schema.String>;
|
|
28
|
+
/** Links the source publishes about itself, such as its podcast on Spotify. */
|
|
29
|
+
readonly links: Schema.$Array<Schema.String>;
|
|
30
|
+
}>;
|
|
31
|
+
export type Source = typeof Source.Type;
|
|
32
|
+
/** A source with its retained upstream items and its feed after every step ran. */
|
|
33
|
+
export interface StoredSource extends Source {
|
|
34
|
+
readonly items: readonly Item[];
|
|
35
|
+
readonly feed: readonly Item[] | null;
|
|
36
|
+
readonly refreshedAt: number | null;
|
|
37
|
+
readonly createdAt: number;
|
|
38
|
+
}
|
|
39
|
+
export interface Feed {
|
|
40
|
+
readonly source: Source;
|
|
41
|
+
readonly items: readonly Item[];
|
|
42
|
+
/** Feed paths for the source's links that another provider supports, such as `spotify/4JH4tybY1zX6e5hjCwU6gF`. */
|
|
43
|
+
readonly related: readonly string[];
|
|
44
|
+
readonly selfUrl: string;
|
|
45
|
+
readonly now: number;
|
|
46
|
+
}
|
package/schema.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export const JsonObject = Schema.Record(Schema.String, Schema.Json);
|
|
3
|
+
export const Item = Schema.Struct({
|
|
4
|
+
/** Provider-scoped identity, such as `youtube:dQw4w9WgXcQ`. */
|
|
5
|
+
key: Schema.String,
|
|
6
|
+
url: Schema.String,
|
|
7
|
+
title: Schema.String,
|
|
8
|
+
description: Schema.String,
|
|
9
|
+
author: Schema.String,
|
|
10
|
+
publishedAt: Schema.Finite,
|
|
11
|
+
/** Milliseconds, or null when the provider doesn't say. */
|
|
12
|
+
duration: Schema.NullOr(Schema.Finite),
|
|
13
|
+
image: Schema.NullOr(Schema.String),
|
|
14
|
+
/** Provider-defined, such as `video`, `short`, `live` or `episode`. */
|
|
15
|
+
kind: Schema.String,
|
|
16
|
+
metadata: JsonObject,
|
|
17
|
+
});
|
|
18
|
+
export const Source = Schema.Struct({
|
|
19
|
+
/** `provider:key`, such as `youtube:UCYO_jab_esuFRV4b17AJtAw`. */
|
|
20
|
+
id: Schema.String,
|
|
21
|
+
provider: Schema.String,
|
|
22
|
+
key: Schema.String,
|
|
23
|
+
url: Schema.String,
|
|
24
|
+
name: Schema.String,
|
|
25
|
+
image: Schema.NullOr(Schema.String),
|
|
26
|
+
/** Links the source publishes about itself, such as its podcast on Spotify. */
|
|
27
|
+
links: Schema.Array(Schema.String),
|
|
28
|
+
});
|
package/spotify.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Provider } from "./plugin.ts";
|
|
2
|
+
export interface SpotifyOptions {
|
|
3
|
+
readonly clientId: string;
|
|
4
|
+
readonly clientSecret: string;
|
|
5
|
+
/** Two-letter country code for catalog availability. Default: US. */
|
|
6
|
+
readonly market?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Spotify shows through the Spotify Web API, with client credentials. */
|
|
9
|
+
export declare function spotify(options: SpotifyOptions): {
|
|
10
|
+
readonly id: "spotify";
|
|
11
|
+
readonly providers: readonly [Provider];
|
|
12
|
+
};
|
package/spotify.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { InvalidInput, NotFound, RateLimited, retryAfter, UpstreamError } from "./errors.js";
|
|
3
|
+
import { definePlugin } from "./plugin.js";
|
|
4
|
+
const idPattern = /^[a-zA-Z0-9]{22}$/;
|
|
5
|
+
const Image = Schema.Struct({ url: Schema.String });
|
|
6
|
+
const Show = Schema.Struct({ id: Schema.String, name: Schema.String, images: Schema.Array(Image) });
|
|
7
|
+
const Episode = Schema.Struct({
|
|
8
|
+
id: Schema.String,
|
|
9
|
+
name: Schema.String,
|
|
10
|
+
description: Schema.String,
|
|
11
|
+
release_date: Schema.String,
|
|
12
|
+
duration_ms: Schema.Finite,
|
|
13
|
+
explicit: Schema.Boolean,
|
|
14
|
+
images: Schema.Array(Image),
|
|
15
|
+
});
|
|
16
|
+
const Episodes = Schema.Struct({ items: Schema.Array(Schema.NullOr(Episode)) });
|
|
17
|
+
const Token = Schema.fromJsonString(Schema.Struct({ access_token: Schema.String, expires_in: Schema.Finite }));
|
|
18
|
+
function failure(response, now) {
|
|
19
|
+
if (response.status === 429)
|
|
20
|
+
return new RateLimited({
|
|
21
|
+
message: "Spotify is limiting requests",
|
|
22
|
+
retryAfter: retryAfter(response.headers["retry-after"], now),
|
|
23
|
+
});
|
|
24
|
+
if (response.status === 404 || response.status === 400)
|
|
25
|
+
return new NotFound({ message: "Show not found on Spotify" });
|
|
26
|
+
return new UpstreamError({ message: `Spotify returned ${response.status}` });
|
|
27
|
+
}
|
|
28
|
+
/** "2026", "2026-09" and "2026-09-04" all become midnight UTC on their first day. */
|
|
29
|
+
const releaseDate = (value) => Date.parse(`${value}-01-01`.slice(0, 10) + "T00:00:00Z");
|
|
30
|
+
/** Spotify shows through the Spotify Web API, with client credentials. */
|
|
31
|
+
export function spotify(options) {
|
|
32
|
+
if (!options.clientId || !options.clientSecret)
|
|
33
|
+
throw new InvalidInput({ message: "Spotify needs a client ID and secret" });
|
|
34
|
+
const market = options.market ?? "US";
|
|
35
|
+
let token;
|
|
36
|
+
const accessToken = Effect.fn("spotify.token")(function* (context) {
|
|
37
|
+
const now = yield* context.now;
|
|
38
|
+
if (token && token.expiresAt > now)
|
|
39
|
+
return token.value;
|
|
40
|
+
const response = yield* context.http.post("https://accounts.spotify.com/api/token", "grant_type=client_credentials", {
|
|
41
|
+
authorization: `Basic ${btoa(`${options.clientId}:${options.clientSecret}`)}`,
|
|
42
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
43
|
+
});
|
|
44
|
+
if (response.status !== 200)
|
|
45
|
+
return yield* failure(response, now);
|
|
46
|
+
const result = yield* Schema.decodeEffect(Token)(response.text).pipe(Effect.mapError(() => new UpstreamError({ message: "Spotify returned an invalid token" })));
|
|
47
|
+
token = { value: result.access_token, expiresAt: now + result.expires_in * 1000 - 60_000 };
|
|
48
|
+
return token.value;
|
|
49
|
+
});
|
|
50
|
+
const get = (path, schema, context) => Effect.gen(function* () {
|
|
51
|
+
const url = `https://api.spotify.com/v1/${path}${path.includes("?") ? "&" : "?"}market=${market}`;
|
|
52
|
+
let response = yield* context.http.get(url, {
|
|
53
|
+
authorization: `Bearer ${yield* accessToken(context)}`,
|
|
54
|
+
});
|
|
55
|
+
if (response.status === 401) {
|
|
56
|
+
token = undefined;
|
|
57
|
+
response = yield* context.http.get(url, {
|
|
58
|
+
authorization: `Bearer ${yield* accessToken(context)}`,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (response.status !== 200)
|
|
62
|
+
return yield* failure(response, yield* context.now);
|
|
63
|
+
return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(response.text).pipe(Effect.mapError(() => new UpstreamError({ message: "Spotify returned unexpected data" })));
|
|
64
|
+
});
|
|
65
|
+
const reference = (url) => /^\/(?:intl-[a-zA-Z-]+\/)?(show|episode)\/([a-zA-Z0-9]{22})\/?$/.exec(url.pathname);
|
|
66
|
+
const provider = {
|
|
67
|
+
id: "spotify",
|
|
68
|
+
retain: 50,
|
|
69
|
+
matches: (url) => url.protocol === "https:" && url.hostname === "open.spotify.com" && reference(url) !== null,
|
|
70
|
+
ref: (url) => {
|
|
71
|
+
const match = reference(url);
|
|
72
|
+
return match?.[1] === "show" ? match[2] : undefined;
|
|
73
|
+
},
|
|
74
|
+
url: (ref) => idPattern.test(ref) ? new URL(`https://open.spotify.com/show/${ref}`) : undefined,
|
|
75
|
+
resolve: Effect.fn("spotify.resolve")(function* (url, context) {
|
|
76
|
+
const match = reference(url);
|
|
77
|
+
if (!match?.[2])
|
|
78
|
+
return yield* new NotFound({ message: "Not a Spotify show or episode link" });
|
|
79
|
+
const show = match[1] === "show"
|
|
80
|
+
? yield* get(`shows/${match[2]}`, Show, context)
|
|
81
|
+
: (yield* get(`episodes/${match[2]}`, Schema.Struct({ show: Show }), context)).show;
|
|
82
|
+
return {
|
|
83
|
+
key: show.id,
|
|
84
|
+
url: `https://open.spotify.com/show/${show.id}`,
|
|
85
|
+
name: show.name,
|
|
86
|
+
image: show.images[0]?.url ?? null,
|
|
87
|
+
};
|
|
88
|
+
}),
|
|
89
|
+
fetch: Effect.fn("spotify.fetch")(function* (source, context) {
|
|
90
|
+
const page = yield* get(`shows/${source.key}/episodes?limit=50`, Episodes, context);
|
|
91
|
+
const items = page.items.flatMap((episode) => episode === null
|
|
92
|
+
? []
|
|
93
|
+
: [
|
|
94
|
+
{
|
|
95
|
+
key: `spotify:${episode.id}`,
|
|
96
|
+
url: `https://open.spotify.com/episode/${episode.id}`,
|
|
97
|
+
title: episode.name,
|
|
98
|
+
description: episode.description,
|
|
99
|
+
author: source.name,
|
|
100
|
+
publishedAt: releaseDate(episode.release_date) || 0,
|
|
101
|
+
duration: episode.duration_ms,
|
|
102
|
+
image: episode.images[0]?.url ?? null,
|
|
103
|
+
kind: "episode",
|
|
104
|
+
metadata: { spotify: { episodeId: episode.id, explicit: episode.explicit } },
|
|
105
|
+
},
|
|
106
|
+
]);
|
|
107
|
+
return { items };
|
|
108
|
+
}),
|
|
109
|
+
};
|
|
110
|
+
return definePlugin({ id: "spotify", providers: [provider] });
|
|
111
|
+
}
|
package/store.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { NotFound, StorageError } from "./errors.ts";
|
|
3
|
+
import type { StoredSource } from "./schema.ts";
|
|
4
|
+
/** What the engine persists. Hosting concerns such as scheduling live outside it. */
|
|
5
|
+
export interface Store {
|
|
6
|
+
getAlias(alias: string): Effect.Effect<string | undefined, StorageError>;
|
|
7
|
+
putAlias(alias: string, sourceId: string): Effect.Effect<void, StorageError>;
|
|
8
|
+
/** Fails with NotFound for sources taken down, so they aren't added again. */
|
|
9
|
+
getSource(id: string): Effect.Effect<StoredSource | undefined, StorageError | NotFound>;
|
|
10
|
+
putSource(source: StoredSource): Effect.Effect<void, StorageError>;
|
|
11
|
+
/** Saved step results by key; missing keys are absent. */
|
|
12
|
+
getResults(keys: readonly string[]): Effect.Effect<ReadonlyMap<string, string>, StorageError>;
|
|
13
|
+
putResults(entries: ReadonlyMap<string, string>): Effect.Effect<void, StorageError>;
|
|
14
|
+
}
|
|
15
|
+
export declare function memoryStore(): Store;
|
package/store.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export function memoryStore() {
|
|
3
|
+
const aliases = new Map();
|
|
4
|
+
const sources = new Map();
|
|
5
|
+
const results = new Map();
|
|
6
|
+
return {
|
|
7
|
+
getAlias: (alias) => Effect.sync(() => aliases.get(alias)),
|
|
8
|
+
putAlias: (alias, id) => Effect.sync(() => void aliases.set(alias, id)),
|
|
9
|
+
getSource: (id) => Effect.sync(() => sources.get(id)),
|
|
10
|
+
putSource: (source) => Effect.sync(() => void sources.set(source.id, source)),
|
|
11
|
+
getResults: (keys) => Effect.sync(() => new Map(keys.flatMap((key) => (results.has(key) ? [[key, results.get(key) ?? ""]] : [])))),
|
|
12
|
+
putResults: (entries) => Effect.sync(() => {
|
|
13
|
+
for (const [key, value] of entries)
|
|
14
|
+
results.set(key, value);
|
|
15
|
+
}),
|
|
16
|
+
};
|
|
17
|
+
}
|
package/youtube.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Provider } from "./plugin.ts";
|
|
2
|
+
/** YouTube's generated playlists per channel: long-form uploads, Shorts and livestreams. */
|
|
3
|
+
declare const lists: {
|
|
4
|
+
readonly videos: "UULF";
|
|
5
|
+
readonly shorts: "UUSH";
|
|
6
|
+
readonly live: "UULV";
|
|
7
|
+
};
|
|
8
|
+
export type YouTubeList = keyof typeof lists;
|
|
9
|
+
export interface YouTubeOptions {
|
|
10
|
+
/** Which of a channel's lists to fetch. Each list is one request per refresh. Default: all. */
|
|
11
|
+
readonly lists?: readonly YouTubeList[];
|
|
12
|
+
/**
|
|
13
|
+
* A YouTube Data API key, used only to look up a handle when YouTube answers the channel page
|
|
14
|
+
* with a captcha. Optional: everything else uses public feeds.
|
|
15
|
+
*/
|
|
16
|
+
readonly apiKey?: string;
|
|
17
|
+
}
|
|
18
|
+
/** YouTube channels through their public feeds. An API key is an optional fallback. */
|
|
19
|
+
export declare function youtube(options?: YouTubeOptions): {
|
|
20
|
+
readonly id: "youtube";
|
|
21
|
+
readonly providers: readonly [Provider];
|
|
22
|
+
};
|
|
23
|
+
export declare const hideShorts: () => import("./plugin.ts").Plugin;
|
|
24
|
+
export declare const hideLiveStreams: () => import("./plugin.ts").Plugin;
|
|
25
|
+
export {};
|