guessit-js 4.5.3 → 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
@@ -27,16 +27,28 @@ npm install guessit-js
27
27
 
28
28
  ## CLI
29
29
 
30
+ Mirrors the Python `guessit` command:
31
+
30
32
  ```bash
31
33
  npx guessit-js "The.Dark.Knight.2008.1080p.BluRay.x264-GROUP.mkv"
32
34
  # title: The Dark Knight
33
35
  # year: 2008
34
36
  # ...
35
37
 
36
- npx guessit-js -j "Mob Psycho 100 - 09.mkv" # JSON output
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
37
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)
38
45
  ```
39
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
+
40
52
  ## Usage
41
53
 
42
54
  ```typescript
package/bin/cli.mjs CHANGED
@@ -1,51 +1,220 @@
1
1
  #!/usr/bin/env node
2
- // guessit-js CLI — mirrors the Python `guessit` command.
3
- // guessit-js <filename> [<filename> ...] property: value lines
4
- // guessit-js -j <filename> JSON output
5
- // guessit-js -t movie <filename> force type
6
- import { guessit, version } from '../dist/guessit-js.js';
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';
7
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 -------------------------------------------------------
8
59
  const args = process.argv.slice(2);
9
60
  const filenames = [];
10
- let json = false;
11
- let type = null;
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
+ };
12
75
 
13
76
  for (let i = 0; i < args.length; i++) {
14
77
  const a = args[i];
15
- if (a === '-j' || a === '--json') json = true;
16
- else if (a === '-t' || a === '--type') type = args[++i];
17
- else if (a === '-v' || a === '--version') {
18
- console.log(version);
19
- process.exit(0);
20
- } else if (a === '-h' || a === '--help') {
21
- console.log(`guessit-js ${version} — extract metadata from media filenames
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
+ }
22
118
 
23
- Usage: guessit-js [options] <filename> [<filename> ...]
119
+ if (options.type && options.type !== 'movie' && options.type !== 'episode') {
120
+ fail(`invalid --type "${options.type}": must be movie or episode`);
121
+ }
24
122
 
25
- Options:
26
- -j, --json output JSON (single object, or array for multiple files)
27
- -t, --type <type> suggested type: movie or episode
28
- -v, --version print version
29
- -h, --help this help`);
30
- process.exit(0);
31
- } else filenames.push(a);
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);
32
146
  }
33
147
 
34
- if (!filenames.length) {
35
- console.error('usage: guessit-js [options] <filename> — see --help');
36
- process.exit(2);
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;
37
183
  }
38
184
 
39
- const options = type ? { type } : {};
40
- const results = filenames.map((f) => guessit(f, options));
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
+ });
41
198
 
42
- if (json) {
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') {
43
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
+ });
44
213
  } else {
45
214
  results.forEach((r, i) => {
46
- if (filenames.length > 1) console.log(`${i ? '\n' : ''}For: ${filenames[i]}`);
47
- for (const [k, v] of Object.entries(r)) {
48
- console.log(`${k}: ${Array.isArray(v) ? JSON.stringify(v) : v}`);
49
- }
215
+ if (i) console.log('');
216
+ printLines(filenames[i], r, filenames.length > 1);
50
217
  });
51
218
  }
219
+
220
+ process.exit(hadError ? 1 : 0);
@@ -14516,7 +14516,7 @@ function configure(options, rulesBuilder, force = false) {
14516
14516
  function reset() {
14517
14517
  defaultApi.reset();
14518
14518
  }
14519
- const version$1 = "4.5.3";
14519
+ const version$1 = "4.5.4";
14520
14520
  const version = version$1;
14521
14521
  exports.GUESSIT_SCHEMA = GUESSIT_SCHEMA;
14522
14522
  exports.GuessItApi = GuessItApi;
@@ -14514,7 +14514,7 @@ function configure(options, rulesBuilder, force = false) {
14514
14514
  function reset() {
14515
14515
  defaultApi.reset();
14516
14516
  }
14517
- const version$1 = "4.5.3";
14517
+ const version$1 = "4.5.4";
14518
14518
  const version = version$1;
14519
14519
  export {
14520
14520
  GUESSIT_SCHEMA,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "guessit-js",
3
- "version": "4.5.3",
3
+ "version": "4.5.4",
4
4
  "description": "Extract metadata (title, year, season, episode, codec, language, etc.) from media filenames — TypeScript port of Python guessit",
5
5
  "keywords": [
6
6
  "guessit",