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