Writing an extractor
An extractor turns one platform’s post URL into a MediaResult. Each platform
has a module in src/extractors/, and
src/router.ts selects one by hostname.
resolve(url) -> route(url) -> extractor(url, options) -> MediaResult
Add an extractor
- Create
src/extractors/<platform>.ts. - Register each supported hostname in
src/router.ts. - Add fixtures and cassettes. See Tests and cassettes.
- Add the URL forms to Using the library.
Extractor shape
An extractor default-exports a function that accepts the URL and
ResolveOptions.
import { http_get } from "../http.ts";
import { NetworkError, ParseError } from "../errors.ts";
import type { MediaResult, ResolveOptions } from "../types.ts";
export default async function resolve(
url: string,
options: ResolveOptions,
): Promise<MediaResult> {
const id = url.match(/\/post\/(\d+)/)?.[1];
if (!id) throw new ParseError("Could not parse post id", "example");
try {
const response = await http_get(
`https://api.example.com/posts/${id}`,
options,
);
const data = (await response.json()) as {
video_url?: string;
title?: string;
author?: string;
};
if (!data.video_url) throw new ParseError("No media found", "example");
return {
urls: [
{ type: "video", url: data.video_url, filename: `example-${id}.mp4` },
],
headers: {},
meta: {
title: data.title || "Example post",
author: data.author || "Unknown",
platform: "example",
},
};
} catch (error: unknown) {
if (error instanceof NetworkError || error instanceof ParseError) {
throw error;
}
const message = error instanceof Error ? error.message : "Unknown error";
throw new ParseError(message, "example");
}
}
Register the extractor with the hostname written without www.. The router
removes that prefix before it looks up the hostname.
import example from "./extractors/example.ts";
const EXTRACTORS = new Map<string, ExtractorFn>([
// ...
["example.com", example],
["ex.am", example],
]);
Contracts
- Use
http_getandhttp_postfromsrc/http.ts. Passoptionsthrough so the caller’s timeout and headers apply. - Throw
ParseErrorwith the platform name when the URL or response is not usable. RethrowNetworkErrorandParseError; wrap other errors in aParseError. - Put CDN request headers in
result.headers. Use{}when no headers are required. - Use
<platform>-<id>.<ext>filenames. Add-1,-2for additional media, and-videoor-audiofor separate tracks. Facebook uses thefb-prefix. - Assign optional metadata only when the platform returned a value. The
exactOptionalPropertyTypessetting does not allowundefinedvalues in optional fields. - Keep platform extractors independent. The Instagram modules share their GraphQL client and media parser.
Code style is in Contributing.