@boy-offi9-inc/tidepull 0.1.0-rc.0 → 0.1.0-rc.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 +1 -1
- package/README.md +1 -0
- package/dist/core/downloader.js +3 -3
- package/dist/core/events.d.ts +1 -1
- package/dist/core/http.js +1 -1
- package/dist/core/pipeline.d.ts +2 -2
- package/dist/core/pipeline.js +5 -3
- package/dist/core/selector.d.ts +1 -1
- package/dist/core/template.d.ts +1 -1
- package/dist/core/template.js +1 -1
- package/dist/core/types.d.ts +1 -1
- package/dist/dash/index.js +2 -2
- package/dist/dash/xml.js +1 -1
- package/dist/extractors/generic.js +2 -1
- package/dist/main.d.ts +1 -1
- package/dist/main.js +3 -2
- package/dist/tidepull.d.ts +1 -1
- package/dist/tidepull.js +2 -3
- package/dist/ui/render.d.ts +2 -2
- package/dist/ui/render.js +14 -3
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -85,6 +85,7 @@ From a clone, `node src/cli.ts <args>` runs the TypeScript source directly (Node
|
|
|
85
85
|
| Format selector | `b bv bv* ba ba* w wv wa`, `+` merge, `/` fallback, `()` groups, `[height<=720]` `[filesize<?50M]` `[ext=mp4]` |
|
|
86
86
|
| Output templates | `%(title)s`, `%(x\|default)s`, `%(title).50s`, sanitized |
|
|
87
87
|
| Merge / remux | ffmpeg |
|
|
88
|
+
| Site-specific extractors | none built in. Platforms such as YouTube hide their media behind scripts and tokens and need a plugin (see Plugins) |
|
|
88
89
|
| Not yet | live HLS/DASH, multi-period DASH, SAMPLE-AES, byte-range HLS/DASH, playlists, subtitles, cookies |
|
|
89
90
|
|
|
90
91
|
## Footprint (enforced in CI)
|
package/dist/core/downloader.js
CHANGED
|
@@ -16,7 +16,7 @@ export async function downloadHttp(url, dest, o) {
|
|
|
16
16
|
headers.Range = `bytes=${offset}-`;
|
|
17
17
|
const res = await o.http.request(url, { headers, signal: o.signal });
|
|
18
18
|
if (res.status === 416) {
|
|
19
|
-
// Range not satisfiable: the .part is either
|
|
19
|
+
// Range not satisfiable: the .part is either complete or stale.
|
|
20
20
|
await res.body?.cancel();
|
|
21
21
|
const total = /\*\/(\d+)/.exec(res.headers.get("content-range") ?? "")?.[1];
|
|
22
22
|
if (total && Number(total) === offset) {
|
|
@@ -40,7 +40,7 @@ export async function downloadHttp(url, dest, o) {
|
|
|
40
40
|
total = m ? Number(m[2]) : undefined;
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
43
|
-
offset = 0; //
|
|
43
|
+
offset = 0; // no usable Range response: start over
|
|
44
44
|
const cl = res.headers.get("content-length");
|
|
45
45
|
total = cl ? Number(cl) : undefined;
|
|
46
46
|
}
|
|
@@ -105,7 +105,7 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
105
105
|
const inflight = new Array(n);
|
|
106
106
|
const start = (i) => {
|
|
107
107
|
inflight[i] = fetchSeg(i);
|
|
108
|
-
inflight[i].catch(() => { }); //
|
|
108
|
+
inflight[i].catch(() => { }); // handled when awaited in order
|
|
109
109
|
};
|
|
110
110
|
let ext = "ts";
|
|
111
111
|
let lastMap;
|
package/dist/core/events.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export type TidepullEvent = {
|
|
|
42
42
|
url?: string;
|
|
43
43
|
};
|
|
44
44
|
export type Emit = (e: TidepullEvent) => void;
|
|
45
|
-
/**
|
|
45
|
+
/** Logging interface exposed to extractors. */
|
|
46
46
|
export interface Log {
|
|
47
47
|
debug(msg: string): void;
|
|
48
48
|
info(tag: string, msg: string): void;
|
package/dist/core/http.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TidepullError, RedirectError, errMsg } from "./errors.js";
|
|
2
2
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
3
3
|
const REDIRECT = new Set([301, 302, 303, 307, 308]);
|
|
4
|
-
/**
|
|
4
|
+
/** Sent to every host. All other custom headers are scoped (see HttpOptions.headerHosts). */
|
|
5
5
|
const OPEN_HEADERS = new Set(["user-agent", "accept", "accept-language", "accept-encoding", "range", "referer", "origin"]);
|
|
6
6
|
const hostMatches = (pattern, host) => pattern.startsWith("*.") ? host === pattern.slice(2) || host.endsWith(pattern.slice(1)) : host === pattern;
|
|
7
7
|
export class Http {
|
package/dist/core/pipeline.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ export declare const DEFAULT_OUTPUT = "%(title)s [%(id)s].%(ext)s";
|
|
|
3
3
|
export interface DownloadOptions {
|
|
4
4
|
/** format selector, default "bv*+ba/b" */
|
|
5
5
|
format: string;
|
|
6
|
-
/** output template
|
|
7
|
-
output
|
|
6
|
+
/** output template. Default: "%(title)s [%(id)s].%(ext)s", or just the title when the id is identical (typical for direct files) */
|
|
7
|
+
output?: string;
|
|
8
8
|
/** base directory */
|
|
9
9
|
paths: string;
|
|
10
10
|
/** resume from .part files */
|
package/dist/core/pipeline.js
CHANGED
|
@@ -26,7 +26,7 @@ export async function extractInfo(rawUrl, ctx) {
|
|
|
26
26
|
throw new TidepullError(`Extractor "${ex.name}" returned no formats`);
|
|
27
27
|
return info;
|
|
28
28
|
}
|
|
29
|
-
/**
|
|
29
|
+
/** Drops undefined keys so events serialize cleanly. */
|
|
30
30
|
const compact = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
31
31
|
const scalars = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => ["string", "number", "boolean"].includes(typeof v)));
|
|
32
32
|
/** Selects formats per opts.format, downloads them, merges/remuxes with ffmpeg if needed. Returns the final file path. */
|
|
@@ -47,7 +47,9 @@ export async function downloadInfo(info, opts, ctx) {
|
|
|
47
47
|
resolution: chosen[0].height ? `${chosen[0].width ?? "?"}x${chosen[0].height}` : "audio only",
|
|
48
48
|
ext: predictedExt,
|
|
49
49
|
};
|
|
50
|
-
|
|
50
|
+
// Default name: just the title when it equals the id (direct files).
|
|
51
|
+
const template = opts.output ?? (String(info.title) === String(info.id) ? "%(title)s.%(ext)s" : DEFAULT_OUTPUT);
|
|
52
|
+
let target = path.resolve(opts.paths, renderTemplate(template, fields));
|
|
51
53
|
await mkdir(path.dirname(target), { recursive: true });
|
|
52
54
|
const exists = await stat(target).then(() => true, () => false);
|
|
53
55
|
if (exists && !opts.overwrite) {
|
|
@@ -87,7 +89,7 @@ export async function downloadInfo(info, opts, ctx) {
|
|
|
87
89
|
if (!merging) {
|
|
88
90
|
const ext = await fetchOne(chosen[0], target);
|
|
89
91
|
if (ext !== path.extname(target).slice(1)) {
|
|
90
|
-
const fixed = `${base}.${ext}`; //
|
|
92
|
+
const fixed = `${base}.${ext}`; // the real container can differ from the predicted one (e.g. fMP4 HLS)
|
|
91
93
|
await rename(target, fixed);
|
|
92
94
|
target = fixed;
|
|
93
95
|
}
|
package/dist/core/selector.d.ts
CHANGED
package/dist/core/template.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Output templates: "%(title)s [%(id)s].%(ext)s", "%(uploader|unknown)s", "%(title).50s". */
|
|
2
2
|
export declare function sanitize(s: string): string;
|
|
3
3
|
export declare function renderTemplate(tpl: string, fields: Record<string, unknown>): string;
|
package/dist/core/template.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Output templates: "%(title)s [%(id)s].%(ext)s", "%(uploader|unknown)s", "%(title).50s". */
|
|
2
2
|
export function sanitize(s) {
|
|
3
3
|
const cleaned = s
|
|
4
4
|
.replace(/[\\/:*?"<>|\x00-\x1f]/g, "_")
|
package/dist/core/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Emit, Log } from "./events.js";
|
|
2
2
|
import type { Http } from "./http.js";
|
|
3
3
|
export type Protocol = "https" | "hls" | "dash";
|
|
4
|
-
/** One downloadable stream.
|
|
4
|
+
/** One downloadable stream. */
|
|
5
5
|
export interface Format {
|
|
6
6
|
format_id: string;
|
|
7
7
|
url: string;
|
package/dist/dash/index.js
CHANGED
|
@@ -17,7 +17,7 @@ export const dashExtractor = {
|
|
|
17
17
|
export async function extractDash(url, ctx) {
|
|
18
18
|
const { text, finalUrl } = await ctx.http.text(url.href);
|
|
19
19
|
if (text.length > MAX_MPD_BYTES)
|
|
20
|
-
throw new TidepullError("Manifest is
|
|
20
|
+
throw new TidepullError("Manifest is too large");
|
|
21
21
|
const mpd = parseMpd(text, finalUrl);
|
|
22
22
|
if (mpd.dynamic)
|
|
23
23
|
throw new TidepullError("Live DASH streams are not supported yet");
|
|
@@ -36,7 +36,7 @@ export async function extractDash(url, ctx) {
|
|
|
36
36
|
const formats = [];
|
|
37
37
|
for (const r of mpd.reps) {
|
|
38
38
|
if (r.kind === "other")
|
|
39
|
-
continue; // subtitles
|
|
39
|
+
continue; // subtitles and other tracks are not supported
|
|
40
40
|
const { v, a } = splitCodecs(r.codecs);
|
|
41
41
|
const video = r.kind === "video";
|
|
42
42
|
const f = {
|
package/dist/dash/xml.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Minimal
|
|
1
|
+
/** Minimal XML parser: elements, attributes and text. DTDs and custom entities are not expanded; namespace prefixes are dropped. */
|
|
2
2
|
import { TidepullError } from "../core/errors.js";
|
|
3
3
|
const ENT = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
4
4
|
const decode = (s) => s.replace(/&(?:#x([0-9a-f]+)|#(\d+)|(\w+));/gi, (m, hex, dec, name) => {
|
|
@@ -92,7 +92,8 @@ export const genericExtractor = {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
if (!formats.length)
|
|
95
|
-
throw new TidepullError(`
|
|
95
|
+
throw new TidepullError(`No downloadable media found on ${url.hostname}. This looks like a site that needs its own extractor plugin (see README, Plugins). ` +
|
|
96
|
+
"Built in: direct media files, HLS, DASH and pages that embed them.");
|
|
96
97
|
const id = (await sha(pageUrl)).slice(0, 10);
|
|
97
98
|
return { id, title: title || id, extractor: "generic", webpage_url: pageUrl, thumbnail, formats };
|
|
98
99
|
},
|
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -8,7 +8,7 @@ import { defaultPluginDir, loadPlugins } from "./loader.js";
|
|
|
8
8
|
import { table, fmtBytes } from "./ui/format.js";
|
|
9
9
|
import { Logger, shouldUseColor } from "./ui/log.js";
|
|
10
10
|
import { humanRenderer, jsonRenderer } from "./ui/render.js";
|
|
11
|
-
/**
|
|
11
|
+
/** Package version, read from package.json. */
|
|
12
12
|
export const VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
13
13
|
const HELP = `tidepull ${VERSION}: a minimal media downloader
|
|
14
14
|
|
|
@@ -20,7 +20,8 @@ Selection:
|
|
|
20
20
|
-F, --list-formats List available formats and exit
|
|
21
21
|
|
|
22
22
|
Output:
|
|
23
|
-
-o, --output TEMPLATE Output template (default: "%(title)s [%(id)s].%(ext)s"
|
|
23
|
+
-o, --output TEMPLATE Output template (default: "%(title)s [%(id)s].%(ext)s",
|
|
24
|
+
or just the title when the id is the same)
|
|
24
25
|
-P, --paths DIR Download directory (default: .)
|
|
25
26
|
-w, --overwrite Overwrite existing files
|
|
26
27
|
--no-continue Do not resume partial downloads
|
package/dist/tidepull.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare class Tidepull {
|
|
|
38
38
|
private ctx;
|
|
39
39
|
private guard;
|
|
40
40
|
extract(url: string, o?: CallOptions): Promise<Info>;
|
|
41
|
-
/**
|
|
41
|
+
/** Which format(s) the selector picks (two or more means merge), or undefined if none match. */
|
|
42
42
|
select(info: Info, selector?: string): Format[] | undefined;
|
|
43
43
|
/** Downloads the selected format(s). Resolves to the final file path. */
|
|
44
44
|
download(info: Info, o?: Partial<DownloadOptions> & CallOptions): Promise<string>;
|
package/dist/tidepull.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { downloadInfo, extractInfo
|
|
1
|
+
import { downloadInfo, extractInfo } from "./core/pipeline.js";
|
|
2
2
|
import { abortError } from "./core/errors.js";
|
|
3
3
|
import { makeLog } from "./core/events.js";
|
|
4
4
|
import { Http } from "./core/http.js";
|
|
@@ -73,7 +73,7 @@ export class Tidepull {
|
|
|
73
73
|
}
|
|
74
74
|
return this.guard(o.signal, () => extractInfo(url, this.ctx(o.signal)));
|
|
75
75
|
}
|
|
76
|
-
/**
|
|
76
|
+
/** Which format(s) the selector picks (two or more means merge), or undefined if none match. */
|
|
77
77
|
select(info, selector = DEFAULT_FORMAT) {
|
|
78
78
|
return selectFormats(selector, info.formats);
|
|
79
79
|
}
|
|
@@ -82,7 +82,6 @@ export class Tidepull {
|
|
|
82
82
|
const { signal, ...rest } = o;
|
|
83
83
|
const opts = {
|
|
84
84
|
format: DEFAULT_FORMAT,
|
|
85
|
-
output: DEFAULT_OUTPUT,
|
|
86
85
|
paths: ".",
|
|
87
86
|
resume: true,
|
|
88
87
|
overwrite: false,
|
package/dist/ui/render.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { TidepullEvent } from "../core/events.js";
|
|
2
2
|
import type { Logger } from "./log.js";
|
|
3
|
-
/**
|
|
3
|
+
/** Renders engine events as terminal output on stderr, redrawing one progress line on a TTY. */
|
|
4
4
|
export declare function humanRenderer(log: Logger, o: {
|
|
5
5
|
progress: boolean;
|
|
6
6
|
stream?: NodeJS.WriteStream;
|
|
7
7
|
}): (e: TidepullEvent) => void;
|
|
8
|
-
/**
|
|
8
|
+
/** Writes one JSON event per line to stdout (NDJSON). */
|
|
9
9
|
export declare const jsonRenderer: () => ((e: TidepullEvent) => void);
|
package/dist/ui/render.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { fmtBytes, fmtTime } from "./format.js";
|
|
2
|
-
/**
|
|
2
|
+
/** Renders engine events as terminal output on stderr, redrawing one progress line on a TTY. */
|
|
3
3
|
export function humanRenderer(log, o) {
|
|
4
4
|
const stream = o.stream ?? process.stderr;
|
|
5
5
|
const live = o.progress && !!stream.isTTY;
|
|
@@ -29,11 +29,22 @@ export function humanRenderer(log, o) {
|
|
|
29
29
|
case "progress": {
|
|
30
30
|
if (!live)
|
|
31
31
|
break;
|
|
32
|
+
const cols = stream.columns || 80;
|
|
32
33
|
const pct = e.total ? `${((e.downloaded / e.total) * 100).toFixed(1).padStart(5)}%` : "";
|
|
33
34
|
const size = e.total ? ` of ${e.estimated ? "~" : ""}${fmtBytes(e.total)}` : ` ${fmtBytes(e.downloaded)}`;
|
|
34
35
|
const rate = e.speed ? ` at ${fmtBytes(e.speed)}/s` : "";
|
|
35
36
|
const eta = e.eta !== undefined ? ` ETA ${fmtTime(e.eta)}` : "";
|
|
36
|
-
|
|
37
|
+
// Keep the line narrower than the terminal: a wrapped line cannot be redrawn in place.
|
|
38
|
+
let line = `[download] ${pct}${size}${rate}${eta}`;
|
|
39
|
+
if (line.length >= cols)
|
|
40
|
+
line = `[download] ${pct}${size}${rate}`;
|
|
41
|
+
if (line.length >= cols)
|
|
42
|
+
line = `${pct}${size}${rate}`.trim();
|
|
43
|
+
if (line.length >= cols)
|
|
44
|
+
line = `${pct}${size}`.trim();
|
|
45
|
+
if (line.length >= cols)
|
|
46
|
+
line = (pct || fmtBytes(e.downloaded)).trim();
|
|
47
|
+
stream.write(`\r\x1b[K${line.slice(0, Math.max(1, cols - 1))}`);
|
|
37
48
|
drawn = true;
|
|
38
49
|
break;
|
|
39
50
|
}
|
|
@@ -56,5 +67,5 @@ export function humanRenderer(log, o) {
|
|
|
56
67
|
}
|
|
57
68
|
};
|
|
58
69
|
}
|
|
59
|
-
/**
|
|
70
|
+
/** Writes one JSON event per line to stdout (NDJSON). */
|
|
60
71
|
export const jsonRenderer = () => (e) => void process.stdout.write(JSON.stringify(e) + "\n");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boy-offi9-inc/tidepull",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.1",
|
|
4
4
|
"description": "A minimal, from-scratch media downloader library: extractor plugins, yt-dlp-style format selection, resumable downloads.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|