guessit-js 4.2.0 → 4.3.0

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.
@@ -3289,6 +3289,8 @@ function episodesSeasonChainBreaker(matches, config) {
3289
3289
  return false;
3290
3290
  }
3291
3291
  function seasonEpisodeConflictSolver(match, other2) {
3292
+ const weakFamily = (m) => !!(m?.tags?.includes("weak-episode") || ["weak_episode", "weak_duplicate"].includes(m?.initiator?.name) || ["weak_episode", "weak_duplicate"].includes(m?.name));
3293
+ if (weakFamily(match) && weakFamily(other2)) return null;
3292
3294
  const famChild = (p) => {
3293
3295
  if (p?.name) return null;
3294
3296
  const ch = [...p?.children ?? []];
@@ -3312,6 +3314,10 @@ function seasonEpisodeConflictSolver(match, other2) {
3312
3314
  "container",
3313
3315
  "date"
3314
3316
  ].includes(other2.name)) {
3317
+ if (other2.name === "video_codec" && /^[hx][\W_]\d/i.test(String(other2.raw ?? ""))) {
3318
+ const before = (other2.inputString ?? "")[other2.start - 1] ?? "";
3319
+ if (/[a-z]/i.test(before)) return other2;
3320
+ }
3315
3321
  return match;
3316
3322
  }
3317
3323
  if (other2.name === "audio_channels" && !other2.tags?.includes("weak-audio_channels") && !match.initiator?.children?.named(match.name + "Marker")?.length || other2.name === "screen_size" && !intCoercable(other2.raw)) {
@@ -3355,7 +3361,7 @@ function seasonEpisodeConflictSolver(match, other2) {
3355
3361
  const matchIsWeak = !!(match.tags?.includes("weak-episode") || ["weak_episode", "weak_duplicate"].includes(match.initiator?.name));
3356
3362
  const otherIsWeak = !!(other2.tags?.includes("weak-episode") || ["weak_episode", "weak_duplicate"].includes(other2.initiator?.name));
3357
3363
  if (matchIsWeak && otherIsWeak) {
3358
- return "__default__";
3364
+ return null;
3359
3365
  }
3360
3366
  if (matchIsWeak) return match;
3361
3367
  if (otherIsWeak) return other2;
@@ -3528,7 +3534,7 @@ function episodes(config) {
3528
3534
  },
3529
3535
  disabled: isSeasonEpisodeDisabled
3530
3536
  }).defaults({ tags: ["SxxExx"] }).regex(
3531
- `(?<season>\\d+)` + asymmetricSeasonEpMarker + `@?` + seasonEpMarkerPattern + `@?(?<episode>\\d+)`
3537
+ `(?!0x[0-9a-fA-F])(?<season>\\d+)` + asymmetricSeasonEpMarker + `@?` + seasonEpMarkerPattern + `@?(?<episode>\\d+)`
3532
3538
  ).repeater("+");
3533
3539
  rebulk.chain({
3534
3540
  tags: ["SxxExx"],
@@ -3537,7 +3543,7 @@ function episodes(config) {
3537
3543
  __parent__: and_(sepsSurround, orderingValidator)
3538
3544
  },
3539
3545
  disabled: isSeasonEpisodeDisabled
3540
- }).defaults({ tags: ["SxxExx"] }).regex(`(?<season>\\d+)` + asymmetricSeasonEpMarker + `@?` + seasonEpMarkerPattern + `@?(?<episode>\\d+)`).regex(
3546
+ }).defaults({ tags: ["SxxExx"] }).regex(`(?!0x[0-9a-fA-F])(?<season>\\d+)` + asymmetricSeasonEpMarker + `@?` + seasonEpMarkerPattern + `@?(?<episode>\\d+)`).regex(
3541
3547
  buildOrPattern(
3542
3548
  [...config.season_ep_markers, ...discreteSeparators, ...config.range_separators],
3543
3549
  "episodeSeparator",
@@ -3548,6 +3554,14 @@ function episodes(config) {
3548
3554
  `t(?<season>\\d{1,2})@?` + buildOrPattern(config.episode_markers, "episodeMarker") + `@?(?<episode>\\d{1,4})`,
3549
3555
  { tags: ["SxxExx"], disabled: isSeasonEpisodeDisabled }
3550
3556
  );
3557
+ rebulk.regex(
3558
+ `(?<![^\\W_])(?:SP|EX|OVA|OAV)-?(?<episode>\\d{1,3})(?![^\\W_])`,
3559
+ { tags: ["SxxExx", "special-episode"], disabled: (context) => isDisabled(context, "episode") }
3560
+ );
3561
+ rebulk.regex(
3562
+ `(?<season>\\d{1,2})(?:st|nd|rd|th)@?(?:season)(?![^\\W_])`,
3563
+ { tags: ["SxxExx", "numfirst"], disabled: isSeasonEpisodeDisabled }
3564
+ );
3551
3565
  const seasonOnlySepPattern = buildOrPattern(
3552
3566
  [...config.season_markers, ...discreteSeparators, ...config.range_separators],
3553
3567
  "seasonSeparator",
@@ -3723,6 +3737,9 @@ function episodes(config) {
3723
3737
  disabled: (context) => isDisabled(context, "episode_format")
3724
3738
  });
3725
3739
  rebulk.rules(
3740
+ AnimeTrailingEpisodeRule,
3741
+ PreferAnchoredWeakEpisodeRule,
3742
+ SeasonWordDashedEpisodeRule,
3726
3743
  RemoveNumfirstMarkerCollision,
3727
3744
  CountValidator,
3728
3745
  new DiscMarkerRule(config),
@@ -3738,6 +3755,178 @@ function episodes(config) {
3738
3755
  );
3739
3756
  return rebulk;
3740
3757
  }
3758
+ const _AnimeTrailingEpisodeRule = class _AnimeTrailingEpisodeRule extends Rule {
3759
+ constructor() {
3760
+ super(...arguments);
3761
+ this.priority = -64;
3762
+ this.consequence = [RemoveMatch, AppendMatch];
3763
+ }
3764
+ when(matches, _context) {
3765
+ const input = matches.inputString ?? "";
3766
+ const toRemove = [];
3767
+ const toAppend = [];
3768
+ const episodes2 = matches.named("episode") ?? [];
3769
+ if (episodes2.length) return false;
3770
+ for (const filepart of matches.markers.named("path") ?? []) {
3771
+ const crc2 = matches.range(filepart.start, filepart.end, (m2) => m2.name === "crc32", 0);
3772
+ const animeRg = matches.range(
3773
+ filepart.start,
3774
+ filepart.end,
3775
+ (m2) => m2.name === "release_group" && m2.tags?.includes("anime"),
3776
+ 0
3777
+ );
3778
+ if (!crc2 && !animeRg) continue;
3779
+ let done = false;
3780
+ for (const group of matches.markers.range(filepart.start, filepart.end, (m2) => m2.name === "group") ?? []) {
3781
+ const core = input.slice(group.start + 1, group.end - 1).trim();
3782
+ if (!/^\d{1,3}$/.test(core)) continue;
3783
+ const inner = matches.range(group.start, group.end, (m2) => !m2.private) ?? [];
3784
+ if (inner.some((m2) => !["alternative_title", "episode_title"].includes(m2.name ?? "") && !m2.tags?.includes("weak-episode"))) continue;
3785
+ toRemove.push(...inner);
3786
+ toAppend.push(new Match(group.start + 1, group.end - 1, {
3787
+ name: "episode",
3788
+ value: parseInt(core, 10),
3789
+ inputString: input,
3790
+ tags: ["anime"]
3791
+ }));
3792
+ done = true;
3793
+ break;
3794
+ }
3795
+ if (done) continue;
3796
+ const title2 = matches.range(filepart.start, filepart.end, (m2) => m2.name === "title", 0);
3797
+ if (!title2) continue;
3798
+ const m = /^(.*[^\W\d_])[\s._-]+(\d{1,3})$/.exec(input.slice(title2.start, title2.end));
3799
+ if (!m) continue;
3800
+ const numStart = title2.start + m[1].length + (m[0].length - m[1].length - m[2].length);
3801
+ toRemove.push(title2);
3802
+ const newTitle = new Match(title2.start, title2.start + m[1].length, {
3803
+ name: "title",
3804
+ value: cleanup(m[1]),
3805
+ inputString: input
3806
+ });
3807
+ toAppend.push(newTitle);
3808
+ toAppend.push(new Match(numStart, title2.end, {
3809
+ name: "episode",
3810
+ value: parseInt(m[2], 10),
3811
+ inputString: input,
3812
+ tags: ["anime"]
3813
+ }));
3814
+ }
3815
+ return toRemove.length || toAppend.length ? [toRemove, toAppend] : false;
3816
+ }
3817
+ };
3818
+ _AnimeTrailingEpisodeRule.priority = -64;
3819
+ let AnimeTrailingEpisodeRule = _AnimeTrailingEpisodeRule;
3820
+ const _PreferAnchoredWeakEpisodeRule = class _PreferAnchoredWeakEpisodeRule extends Rule {
3821
+ constructor() {
3822
+ super(...arguments);
3823
+ this.priority = 40;
3824
+ this.consequence = RemoveMatch;
3825
+ }
3826
+ when(matches, _context) {
3827
+ const input = matches.inputString ?? "";
3828
+ const toRemove = [];
3829
+ const isWeakEp = (m) => m.name === "episode" && !m.private && (m.tags?.includes("weak-episode") || ["weak_episode", "weak_duplicate"].includes(m.initiator?.name));
3830
+ const removeGroup = (init) => {
3831
+ for (const c of [...init.children ?? [], init]) {
3832
+ if (!toRemove.includes(c)) toRemove.push(c);
3833
+ }
3834
+ if (!init.children?.length && !toRemove.includes(init)) toRemove.push(init);
3835
+ };
3836
+ for (const filepart of matches.markers.named("path") ?? []) {
3837
+ const weaks = matches.range(filepart.start, filepart.end, isWeakEp) ?? [];
3838
+ if (!weaks.length) continue;
3839
+ const strong = matches.range(filepart.start, filepart.end, (m) => m.name === "episode" && !m.private && !isWeakEp(m) && !!m.initiator?.children?.named?.("episodeMarker")?.length, 0);
3840
+ const byInit = /* @__PURE__ */ new Map();
3841
+ for (const w of weaks) {
3842
+ const init = w.initiator ?? w;
3843
+ byInit.set(init, [...byInit.get(init) ?? [], w]);
3844
+ }
3845
+ if (strong) {
3846
+ for (const init of byInit.keys()) removeGroup(init);
3847
+ continue;
3848
+ }
3849
+ if (byInit.size < 2) continue;
3850
+ const score = (init) => {
3851
+ let sc = 0;
3852
+ const raw = String(init.raw ?? "");
3853
+ if (/^0\d/.test(raw)) sc += 4;
3854
+ let i = init.start - 1;
3855
+ while (i >= 0 && (input[i] === " " || input[i] === "." || input[i] === "_")) i--;
3856
+ if (input[i] === "-") sc += 1;
3857
+ if (matches.markers.atMatch(init, (m) => m.name === "group", 0)) sc -= 2;
3858
+ return sc;
3859
+ };
3860
+ const scored = [...byInit.keys()].map((init) => [init, score(init)]);
3861
+ const top = Math.max(...scored.map(([, sc]) => sc));
3862
+ const winners = scored.filter(([, sc]) => sc === top);
3863
+ if (winners.length === scored.length) continue;
3864
+ const winnerValues = new Set(winners.flatMap(([init]) => (byInit.get(init) ?? []).map((w) => Number(w.value))));
3865
+ for (const [init, sc] of scored) {
3866
+ if (sc >= top) continue;
3867
+ const eps = byInit.get(init) ?? [];
3868
+ if (eps.length > 1) continue;
3869
+ if (eps.some((w) => winnerValues.has(Number(w.value)))) continue;
3870
+ removeGroup(init);
3871
+ }
3872
+ }
3873
+ return toRemove.length ? toRemove : false;
3874
+ }
3875
+ };
3876
+ _PreferAnchoredWeakEpisodeRule.priority = 40;
3877
+ let PreferAnchoredWeakEpisodeRule = _PreferAnchoredWeakEpisodeRule;
3878
+ const _SeasonWordDashedEpisodeRule = class _SeasonWordDashedEpisodeRule extends Rule {
3879
+ constructor() {
3880
+ super(...arguments);
3881
+ this.priority = 32;
3882
+ this.consequence = [RemoveMatch, AppendMatch];
3883
+ }
3884
+ when(matches, _context) {
3885
+ const input = matches.inputString ?? "";
3886
+ const toRemove = [];
3887
+ const toAppend = [];
3888
+ const seasons = matches.named("season") ?? [];
3889
+ const seen = /* @__PURE__ */ new Set();
3890
+ for (const season of seasons) {
3891
+ const init = season.initiator ?? season;
3892
+ if (seen.has(init)) continue;
3893
+ seen.add(init);
3894
+ const children = [...init.children ?? []];
3895
+ const marker = children.find((c) => c.name === "seasonMarker");
3896
+ if (!marker) continue;
3897
+ const markerRaw = String(marker.raw ?? "");
3898
+ const isWordMarker = /^[a-zà-ÿа-я]{3,}$/i.test(markerRaw);
3899
+ const inBracket = !!matches.markers.atMatch(season, (m) => m.name === "group", 0);
3900
+ if (!isWordMarker && !(markerRaw.toLowerCase() === "s" && inBracket)) continue;
3901
+ if (isWordMarker && /s$/i.test(markerRaw)) continue;
3902
+ const nums = children.filter((c) => c.name === "season");
3903
+ if (nums.length !== 2) continue;
3904
+ const sep = children.find((c) => c.name === "seasonSeparator");
3905
+ if (!sep || String(sep.raw ?? "").trim() !== "-") continue;
3906
+ if (isWordMarker && (input[sep.start - 1] !== " " || input[sep.end] !== " ")) continue;
3907
+ const filepart = matches.markers.atMatch(season, (m) => m.name === "path", 0);
3908
+ if (filepart) {
3909
+ const complete = matches.range(
3910
+ filepart.start,
3911
+ filepart.end,
3912
+ (m) => m.name === "other" && m.value === "Complete",
3913
+ 0
3914
+ );
3915
+ if (complete) continue;
3916
+ }
3917
+ const episodeMatch = new Match(nums[1].start, nums[1].end, {
3918
+ name: "episode",
3919
+ value: nums[1].value,
3920
+ inputString: input
3921
+ });
3922
+ toRemove.push(nums[1]);
3923
+ toAppend.push(episodeMatch);
3924
+ }
3925
+ return toRemove.length || toAppend.length ? [toRemove, toAppend] : false;
3926
+ }
3927
+ };
3928
+ _SeasonWordDashedEpisodeRule.priority = 32;
3929
+ let SeasonWordDashedEpisodeRule = _SeasonWordDashedEpisodeRule;
3741
3930
  const _RemoveNumfirstMarkerCollision = class _RemoveNumfirstMarkerCollision extends Rule {
3742
3931
  constructor() {
3743
3932
  super(...arguments);
@@ -3767,7 +3956,8 @@ const _RemoveNumfirstMarkerCollision = class _RemoveNumfirstMarkerCollision exte
3767
3956
  });
3768
3957
  if (!wordFirst) continue;
3769
3958
  const claimed = all.some((m) => !m.private && (m.name === "screen_size" || m.name === "year") && m.start < wordFirst.end && wordFirst.start < m.end);
3770
- if (claimed) removeWithParent(wordFirst);
3959
+ const gluedOrdinal = /^(?:st|nd|rd|th|ª|º|°)/i.test(String(matches.inputString ?? "").slice(nf.end, nf.end + 2));
3960
+ if (claimed || gluedOrdinal) removeWithParent(wordFirst);
3771
3961
  else removeWithParent(nf);
3772
3962
  }
3773
3963
  return toRemove.length ? toRemove : false;
@@ -4717,12 +4907,13 @@ const _ValidateVideoCodec = class _ValidateVideoCodec extends Rule {
4717
4907
  when(matches, _context) {
4718
4908
  const ret = [];
4719
4909
  const codecs = matches.named("video_codec");
4910
+ const hasAny = (v) => Array.isArray(v) ? v.length > 0 : !!v;
4720
4911
  for (const codec of codecs) {
4721
- if (!sepsBefore(codec) && !matches.atIndex(codec.start - 1, (m) => m.tags?.includes("video-codec-prefix"))) {
4912
+ if (!sepsBefore(codec) && !hasAny(matches.atIndex(codec.start - 1, (m) => m.tags?.includes("video-codec-prefix")))) {
4722
4913
  ret.push(codec);
4723
4914
  continue;
4724
4915
  }
4725
- if (!sepsAfter(codec) && !matches.atIndex(codec.end + 1, (m) => m.tags?.includes("video-codec-suffix"))) {
4916
+ if (!sepsAfter(codec) && !hasAny(matches.atIndex(codec.end + 1, (m) => m.tags?.includes("video-codec-suffix")))) {
4726
4917
  ret.push(codec);
4727
4918
  continue;
4728
4919
  }
@@ -6652,6 +6843,97 @@ const _SplitOriginalScriptTitle = class _SplitOriginalScriptTitle extends Rule {
6652
6843
  };
6653
6844
  _SplitOriginalScriptTitle.priority = POST_PROCESS;
6654
6845
  let SplitOriginalScriptTitle = _SplitOriginalScriptTitle;
6846
+ const _PreBracketJunkTitle = class _PreBracketJunkTitle extends Rule {
6847
+ constructor() {
6848
+ super(...arguments);
6849
+ this.priority = POST_PROCESS;
6850
+ this.consequence = RemoveMatch;
6851
+ }
6852
+ when(matches, context) {
6853
+ if (isDisabled(context, "title")) return false;
6854
+ const input = matches.inputString ?? "";
6855
+ const toRemove = [];
6856
+ for (const filepart of matches.markers.named("path")) {
6857
+ const titles = (matches.range(filepart.start, filepart.end, (m) => m.name === "title") ?? []).sort((a, b) => a.start - b.start);
6858
+ if (titles.length < 2) continue;
6859
+ const first = titles[0];
6860
+ if (first.start !== filepart.start) continue;
6861
+ if (/[\s._-]/.test(String(first.value ?? "").trim())) continue;
6862
+ const group = matches.markers.range(first.end, filepart.end, (m) => m.name === "group", 0);
6863
+ if (!group || group.start >= titles[1].start) continue;
6864
+ if (![...input.slice(first.end, group.start)].every((c) => seps.includes(c))) continue;
6865
+ const rg = matches.range(
6866
+ group.start,
6867
+ group.end,
6868
+ (m) => m.name === "release_group" && !!m.tags?.includes("anime"),
6869
+ 0
6870
+ );
6871
+ if (!rg) continue;
6872
+ toRemove.push(first);
6873
+ }
6874
+ return toRemove.length ? toRemove : false;
6875
+ }
6876
+ };
6877
+ _PreBracketJunkTitle.priority = POST_PROCESS;
6878
+ let PreBracketJunkTitle = _PreBracketJunkTitle;
6879
+ const _BracketedTitleFallback = class _BracketedTitleFallback extends Rule {
6880
+ constructor() {
6881
+ super(...arguments);
6882
+ this.priority = POST_PROCESS;
6883
+ this.consequence = [RemoveMatch, AppendMatch];
6884
+ }
6885
+ when(matches, context) {
6886
+ if (isDisabled(context, "title")) return false;
6887
+ const input = matches.inputString ?? "";
6888
+ const toRemove = [];
6889
+ const toAppend = [];
6890
+ for (const filepart of matches.markers.named("path")) {
6891
+ const hasTitle = matches.range(filepart.start, filepart.end, (m) => m.name === "title", 0);
6892
+ if (hasTitle) continue;
6893
+ const groups2 = matches.markers.range(filepart.start, filepart.end, (m) => m.name === "group") ?? [];
6894
+ if (!groups2.length) continue;
6895
+ const rg = matches.range(filepart.start, filepart.end, (m) => m.name === "release_group", 0);
6896
+ let candidate;
6897
+ for (const g of groups2) {
6898
+ if (rg && g.start <= rg.start && g.end >= rg.end) continue;
6899
+ const core = input.slice(g.start + 1, g.end - 1);
6900
+ if (!/[a-zA-Z]{2,}/.test(core)) continue;
6901
+ const inner = matches.range(g.start, g.end, (m) => !m.private) ?? [];
6902
+ if (inner.length) continue;
6903
+ candidate = g;
6904
+ break;
6905
+ }
6906
+ if (candidate) {
6907
+ toAppend.push(new Match(candidate.start + 1, candidate.end - 1, {
6908
+ name: "title",
6909
+ value: cleanup(input.slice(candidate.start + 1, candidate.end - 1)),
6910
+ inputString: input
6911
+ }));
6912
+ continue;
6913
+ }
6914
+ if (rg && rg.tags?.includes("anime")) {
6915
+ const outside = input.slice(filepart.start, filepart.end).replace(input.slice(rg.start - 1 >= filepart.start ? rg.start - 1 : rg.start, Math.min(rg.end + 1, filepart.end)), "");
6916
+ const outsideMatches = matches.range(
6917
+ filepart.start,
6918
+ filepart.end,
6919
+ (m) => !m.private && m.name !== "release_group" && m.start >= filepart.start
6920
+ ) ?? [];
6921
+ const wordyOutside = /[a-zA-Z]{2,}/.test(outside.replace(/\b(mkv|avi|mp4)\b/gi, "")) && outsideMatches.some((m) => ["title", "episode_title", "alternative_title"].includes(m.name ?? ""));
6922
+ if (!wordyOutside) {
6923
+ toRemove.push(rg);
6924
+ toAppend.push(new Match(rg.start, rg.end, {
6925
+ name: "title",
6926
+ value: cleanup(input.slice(rg.start, rg.end)),
6927
+ inputString: input
6928
+ }));
6929
+ }
6930
+ }
6931
+ }
6932
+ return toRemove.length || toAppend.length ? [toRemove, toAppend] : false;
6933
+ }
6934
+ };
6935
+ _BracketedTitleFallback.priority = POST_PROCESS;
6936
+ let BracketedTitleFallback = _BracketedTitleFallback;
6655
6937
  const _PropertyAtTitlePositionAsTitle = class _PropertyAtTitlePositionAsTitle extends Rule {
6656
6938
  constructor() {
6657
6939
  super(...arguments);
@@ -6799,7 +7081,7 @@ function title(config) {
6799
7081
  const rebulk = new Rebulk({
6800
7082
  disabled: (context) => isDisabled(context, "title")
6801
7083
  });
6802
- rebulk.rules(CountryAtTitlePosition, TitleWordAtTitlePosition, SplitOriginalScriptTitle, TitleFromPosition, PreferTitleWithYear, ExtendLoneArticleTitle, PropertyAtTitlePositionAsTitle, RemoveNumericAlternativeTitle, RemoveTailAlternativeTitle, RemoveTailTitle);
7084
+ rebulk.rules(CountryAtTitlePosition, TitleWordAtTitlePosition, SplitOriginalScriptTitle, TitleFromPosition, PreferTitleWithYear, ExtendLoneArticleTitle, PropertyAtTitlePositionAsTitle, RemoveNumericAlternativeTitle, RemoveTailAlternativeTitle, RemoveTailTitle, BracketedTitleFallback, PreBracketJunkTitle);
6803
7085
  const expectedTitle = buildExpectedFunction("expected_title");
6804
7086
  rebulk.functional(expectedTitle, {
6805
7087
  name: "title",