guessit-js 4.5.2 → 4.5.4

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/README.md CHANGED
@@ -25,6 +25,30 @@ Extract metadata (title, year, season, episode, codec, language, etc.) from medi
25
25
  npm install guessit-js
26
26
  ```
27
27
 
28
+ ## CLI
29
+
30
+ Mirrors the Python `guessit` command:
31
+
32
+ ```bash
33
+ npx guessit-js "The.Dark.Knight.2008.1080p.BluRay.x264-GROUP.mkv"
34
+ # title: The Dark Knight
35
+ # year: 2008
36
+ # ...
37
+
38
+ npx guessit-js -j "Mob Psycho 100 - 09.mkv" # JSON (-y YAML, --jsonl streaming)
39
+ npx guessit-js -P title "Movie.2020.mkv" # single property
40
+ npx guessit-js -t episode "ambiguous.file.mkv" # force type
41
+ npx guessit-js @list.txt # filenames from file (also -f list.txt)
42
+ find . -name '*.mkv' | npx guessit-js --jsonl # from stdin, one JSON per line
43
+ npx guessit-js -a "Movie.2020.mkv" # advanced: value/raw/start/end
44
+ npx guessit-js -p # list detectable properties (-V with values)
45
+ ```
46
+
47
+ Parsing options match the Python CLI: `-n` name-only, `-Y`/`-D` date order,
48
+ `-L`/`-C` allowed languages/countries, `-E` episode-prefer-number,
49
+ `-T`/`-G` expected title/group, `--includes`/`--excludes`, `-s` single-value,
50
+ `-c` JSON config file. See `npx guessit-js --help`.
51
+
28
52
  ## Usage
29
53
 
30
54
  ```typescript
@@ -67,6 +91,10 @@ guessit('file.mkv', { allowed_languages: ['en', 'fr'] });
67
91
  guessit('file.mkv', { excludes: ['release_group'] });
68
92
  ```
69
93
 
94
+ ```typescript
95
+ import { version } from 'guessit-js'; // package version string
96
+ ```
97
+
70
98
  ## Detected Properties
71
99
 
72
100
  | Category | Properties |
@@ -118,7 +146,7 @@ echo '{"filename":"Movie.2024.1080p.mkv"}' | wasmtime wasm/guessit.wasm
118
146
  ```
119
147
 
120
148
  The WASM build is **bit-identical to the JS build** across the entire test corpus
121
- (1026/1026, including accented titles) — verified by `test/wasm-full.test.ts`.
149
+ (1035/1035, including accented titles) — verified by `test/wasm-full.test.ts`.
122
150
 
123
151
  ## Differences from Python guessit
124
152
 
package/bin/cli.mjs ADDED
@@ -0,0 +1,220 @@
1
+ #!/usr/bin/env node
2
+ // guessit-js CLI — mirrors the Python `guessit` command-line interface.
3
+ // Zero dependencies; the library is bundled in dist/.
4
+ import { readFileSync } from 'fs';
5
+ import { guessit, properties as apiProperties, version } from '../dist/guessit-js.js';
6
+
7
+ const HELP = `guessit-js ${version} — extract metadata from media filenames
8
+
9
+ Usage: guessit-js [options] <filename> [<filename> ...]
10
+ guessit-js [options] @list.txt filenames from file (one per line)
11
+ guessit-js [options] -f list.txt same as @list.txt (use - for stdin)
12
+ echo "Movie.mkv" | guessit-js [options] filenames from piped stdin
13
+
14
+ Output:
15
+ -j, --json JSON (single object, or array for multiple inputs)
16
+ --jsonl one JSON object per line (streaming-friendly)
17
+ -y, --yaml YAML
18
+ -P, --show-property <name> print only this property's value
19
+ -i, --output-input-string include the input string in the result
20
+
21
+ Parsing options:
22
+ -t, --type <type> suggested type: movie or episode
23
+ -n, --name-only parse as a name without extension
24
+ -Y, --date-year-first ambiguous dates: year first
25
+ -D, --date-day-first ambiguous dates: day first
26
+ -L, --allowed-languages comma-separated language codes (e.g. en,fr)
27
+ -C, --allowed-countries comma-separated country codes (e.g. us,gb)
28
+ -E, --episode-prefer-number prefer episode over season for lone numbers
29
+ -T, --expected-title expected title (repeatable)
30
+ -G, --expected-group expected release group (repeatable)
31
+ --includes <props> comma-separated properties to include (repeatable)
32
+ --excludes <props> comma-separated properties to exclude (repeatable)
33
+ -s, --single-value only the first value per property
34
+ -a, --advanced raw match data (start/end/raw per property)
35
+ -c, --config <file> JSON file with extra options (merged in)
36
+
37
+ Introspection:
38
+ -p, --properties list all detectable property names
39
+ -V, --values list all property names with their possible values
40
+
41
+ Misc:
42
+ -v, --version print version
43
+ -h, --help this help
44
+ -- end of options; everything after is a filename
45
+
46
+ Exit codes: 0 success, 1 processing error, 2 usage error.`;
47
+
48
+ function fail(msg) {
49
+ console.error(msg);
50
+ process.exit(2);
51
+ }
52
+
53
+ function readLines(path) {
54
+ const text = path === '-' ? readFileSync(0, 'utf8') : readFileSync(path, 'utf8');
55
+ return text.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
56
+ }
57
+
58
+ // ---- argument parsing -------------------------------------------------------
59
+ const args = process.argv.slice(2);
60
+ const filenames = [];
61
+ const options = {};
62
+ let output = 'lines';
63
+ let showProperty = null;
64
+ let listProperties = false;
65
+ let listValues = false;
66
+ let endOfOptions = false;
67
+
68
+ const needsValue = (a, i) => {
69
+ if (i + 1 >= args.length) fail(`option ${a} requires a value`);
70
+ return args[i + 1];
71
+ };
72
+ const pushList = (key, raw) => {
73
+ options[key] = (options[key] || []).concat(raw.split(',').map((s) => s.trim()).filter(Boolean));
74
+ };
75
+
76
+ for (let i = 0; i < args.length; i++) {
77
+ const a = args[i];
78
+ if (endOfOptions || !a.startsWith('-') || a === '-') {
79
+ if (a.startsWith('@') && a.length > 1 && !endOfOptions) filenames.push(...readLines(a.slice(1)));
80
+ else if (a === '-') filenames.push(...readLines('-'));
81
+ else filenames.push(a);
82
+ continue;
83
+ }
84
+ switch (a) {
85
+ case '--': endOfOptions = true; break;
86
+ case '-j': case '--json': output = 'json'; break;
87
+ case '--jsonl': output = 'jsonl'; break;
88
+ case '-y': case '--yaml': output = 'yaml'; break;
89
+ case '-P': case '--show-property': showProperty = needsValue(a, i); i++; break;
90
+ case '-f': case '--input-file': filenames.push(...readLines(needsValue(a, i))); i++; break;
91
+ case '-i': case '--output-input-string': options.output_input_string = true; break;
92
+ case '-t': case '--type': options.type = needsValue(a, i); i++; break;
93
+ case '-n': case '--name-only': options.name_only = true; break;
94
+ case '-Y': case '--date-year-first': options.date_year_first = true; break;
95
+ case '-D': case '--date-day-first': options.date_day_first = true; break;
96
+ case '-L': case '--allowed-languages': pushList('allowed_languages', needsValue(a, i)); i++; break;
97
+ case '-C': case '--allowed-countries': pushList('allowed_countries', needsValue(a, i)); i++; break;
98
+ case '-E': case '--episode-prefer-number': options.episode_prefer_number = true; break;
99
+ case '-T': case '--expected-title': pushList('expected_title', needsValue(a, i)); i++; break;
100
+ case '-G': case '--expected-group': pushList('expected_group', needsValue(a, i)); i++; break;
101
+ case '--includes': pushList('includes', needsValue(a, i)); i++; break;
102
+ case '--excludes': pushList('excludes', needsValue(a, i)); i++; break;
103
+ case '-s': case '--single-value': options.single_value = true; break;
104
+ case '-a': case '--advanced': options.advanced = true; break;
105
+ case '-c': case '--config': {
106
+ const path = needsValue(a, i); i++;
107
+ try { Object.assign(options, JSON.parse(readFileSync(path, 'utf8'))); }
108
+ catch (e) { fail(`cannot read config ${path}: ${e.message}`); }
109
+ break;
110
+ }
111
+ case '-p': case '--properties': listProperties = true; break;
112
+ case '-V': case '--values': listValues = true; break;
113
+ case '-v': case '--version': console.log(version); process.exit(0); break;
114
+ case '-h': case '--help': console.log(HELP); process.exit(0); break;
115
+ default: fail(`unknown option: ${a} — see --help`);
116
+ }
117
+ }
118
+
119
+ if (options.type && options.type !== 'movie' && options.type !== 'episode') {
120
+ fail(`invalid --type "${options.type}": must be movie or episode`);
121
+ }
122
+
123
+ // ---- output helpers ---------------------------------------------------------
124
+ function toYaml(value, indent = '') {
125
+ if (Array.isArray(value)) {
126
+ return value.map((v) =>
127
+ typeof v === 'object' && v !== null
128
+ ? `${indent}-\n${toYaml(v, indent + ' ')}`
129
+ : `${indent}- ${yamlScalar(v)}`
130
+ ).join('\n');
131
+ }
132
+ if (typeof value === 'object' && value !== null) {
133
+ return Object.entries(value).map(([k, v]) =>
134
+ typeof v === 'object' && v !== null
135
+ ? `${indent}${k}:\n${toYaml(v, indent + ' ')}`
136
+ : `${indent}${k}: ${yamlScalar(v)}`
137
+ ).join('\n');
138
+ }
139
+ return indent + yamlScalar(value);
140
+ }
141
+ function yamlScalar(v) {
142
+ if (typeof v !== 'string') return String(v);
143
+ return /^[\w][\w .+-]*$/.test(v) && !/^(true|false|null|yes|no|on|off|[\d.]+)$/i.test(v)
144
+ ? v
145
+ : JSON.stringify(v);
146
+ }
147
+
148
+ function printLines(name, result, many) {
149
+ if (many) console.log(`For: ${name}`);
150
+ for (const [k, v] of Object.entries(result)) {
151
+ console.log(`${many ? ' ' : ''}${k}: ${typeof v === 'object' && v !== null ? JSON.stringify(v) : v}`);
152
+ }
153
+ }
154
+
155
+ // ---- introspection modes ----------------------------------------------------
156
+ if (listProperties || listValues) {
157
+ const props = apiProperties(options);
158
+ for (const name of Object.keys(props).sort()) {
159
+ if (listValues) {
160
+ const vals = (props[name] || []).filter((v) => v !== null && v !== undefined);
161
+ console.log(`${name}${vals.length ? ': ' + vals.join(', ') : ''}`);
162
+ } else {
163
+ console.log(name);
164
+ }
165
+ }
166
+ process.exit(0);
167
+ }
168
+
169
+ // ---- main -------------------------------------------------------------------
170
+ if (!process.stdin.isTTY && !filenames.length) {
171
+ filenames.push(...readLines('-'));
172
+ }
173
+ if (!filenames.length) fail('usage: guessit-js [options] <filename> — see --help');
174
+
175
+ // Advanced mode returns live Match objects; reduce them to the Python-CLI shape
176
+ // ({value, raw, start, end}) instead of dumping engine internals.
177
+ function simplifyAdvanced(v) {
178
+ if (Array.isArray(v)) return v.map(simplifyAdvanced);
179
+ if (v && typeof v === 'object' && 'start' in v && 'end' in v) {
180
+ return { value: v.value, raw: v.raw, start: v.start, end: v.end };
181
+ }
182
+ return v;
183
+ }
184
+
185
+ let hadError = false;
186
+ const results = filenames.map((f) => {
187
+ try {
188
+ const r = guessit(f, options);
189
+ if (!options.advanced) return r;
190
+ const out = {};
191
+ for (const [k, v] of Object.entries(r)) out[k] = simplifyAdvanced(v);
192
+ return out;
193
+ } catch (e) {
194
+ hadError = true;
195
+ return { error: String(e && e.message || e) };
196
+ }
197
+ });
198
+
199
+ if (showProperty) {
200
+ results.forEach((r) => {
201
+ const v = r[showProperty];
202
+ console.log(v === undefined ? '' : typeof v === 'object' && v !== null ? JSON.stringify(v) : String(v));
203
+ });
204
+ } else if (output === 'json') {
205
+ console.log(JSON.stringify(filenames.length === 1 ? results[0] : results, null, 2));
206
+ } else if (output === 'jsonl') {
207
+ results.forEach((r) => console.log(JSON.stringify(r)));
208
+ } else if (output === 'yaml') {
209
+ results.forEach((r, i) => {
210
+ if (filenames.length > 1) console.log(`${i ? '\n' : ''}? ${yamlScalar(filenames[i])}\n:`);
211
+ console.log(toYaml(r, filenames.length > 1 ? ' ' : ''));
212
+ });
213
+ } else {
214
+ results.forEach((r, i) => {
215
+ if (i) console.log('');
216
+ printLines(filenames[i], r, filenames.length > 1);
217
+ });
218
+ }
219
+
220
+ process.exit(hadError ? 1 : 0);
@@ -14516,6 +14516,8 @@ function configure(options, rulesBuilder, force = false) {
14516
14516
  function reset() {
14517
14517
  defaultApi.reset();
14518
14518
  }
14519
+ const version$1 = "4.5.4";
14520
+ const version = version$1;
14519
14521
  exports.GUESSIT_SCHEMA = GUESSIT_SCHEMA;
14520
14522
  exports.GuessItApi = GuessItApi;
14521
14523
  exports.GuessItException = GuessItException;
@@ -14528,4 +14530,5 @@ exports.parseOptions = parseOptions;
14528
14530
  exports.properties = properties;
14529
14531
  exports.rebulkBuilder = rebulkBuilder;
14530
14532
  exports.reset = reset;
14533
+ exports.version = version;
14531
14534
  //# sourceMappingURL=guessit-js.cjs.map