File size: 7,671 Bytes
f55c62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { extract, FuzzballExtractOptions } from 'fuzzball';
import { createLogger } from '../utils/index.js';
import { MetadataTitle } from '../metadata/utils.js';

const logger = createLogger('parser');

const umlautMap: Record<string, string> = {
  Ä: 'Ae',
  ä: 'ae',
  Ö: 'Oe',
  ö: 'oe',
  Ü: 'Ue',
  ü: 'ue',
  ß: 'ss',
};

type TitleMatchOptions = {
  threshold: number;
  limitTitles?: number;
} & Exclude<FuzzballExtractOptions, 'returnObjects'>;

interface TitleMatchInnerResult {
  matched: boolean;
  matchedIndex?: number;
}

/**
 * Inner matching function shared by titleMatch and titleMatchWithLang.
 * Returns the match result and the index of the best matching title.
 */
function _titleMatchInner(
  parsedTitle: string,
  titles: string[],
  options: TitleMatchOptions
): TitleMatchInnerResult {
  const { threshold, limitTitles, ...extractOptions } = options;

  if (limitTitles && titles.length > limitTitles) {
    titles = titles.slice(0, limitTitles);
  }

  // when threshold is 1, no need to use levenshtein distance, just check for exact matches
  if (threshold === 1 && !extractOptions.scorer) {
    const idx = titles.findIndex(
      (title) => title.toLowerCase() === parsedTitle.toLowerCase()
    );
    return { matched: idx !== -1, matchedIndex: idx !== -1 ? idx : undefined };
  }

  const results = extract(parsedTitle, titles, {
    ...extractOptions,
    returnObjects: true,
  }) as { choice: string; score: number; key: number }[];

  let bestScore = 0;
  let bestKey: number | undefined;
  for (const result of results) {
    if (result.score > bestScore) {
      bestScore = result.score;
      bestKey = result.key;
    }
  }

  const matched = bestScore / 100 >= threshold;
  return { matched, matchedIndex: matched ? bestKey : undefined };
}

/**
 * Check if a parsed title matches any of the provided titles.
 * @returns true if a match is found above the threshold.
 */
export function titleMatch(
  parsedTitle: string,
  titles: string[],
  options: TitleMatchOptions
): boolean {
  return _titleMatchInner(parsedTitle, titles, options).matched;
}

/**
 * Like titleMatch, but accepts MetadataTitle[] and returns the language
 * of the best matching title (if any). Normalises MetadataTitle strings
 * internally so callers only need to normalise parsedTitle.
 */
export function titleMatchWithLang(
  parsedTitle: string,
  titles: MetadataTitle[],
  options: TitleMatchOptions
): { matched: boolean; language?: string } {
  const normalisedTitles = titles.map((t) => normaliseTitle(t.title));
  const result = _titleMatchInner(parsedTitle, normalisedTitles, options);
  return {
    matched: result.matched,
    language:
      result.matchedIndex !== undefined
        ? titles[result.matchedIndex]?.language
        : undefined,
  };
}

export function preprocessTitle(
  parsedTitle: string,
  filename: string,
  titles: string[]
) {
  let preprocessedTitle = parsedTitle;

  const separatorPatterns = [
    /\s*[\/\|]\s*/,
    /[\s\.\-\(\[]+a[\s\.]?k[\s\.]?a[\s\.\)\-\]]+/i,
    /\s*\(([^)]+)\)$/,
  ];
  for (const pattern of separatorPatterns) {
    const match = preprocessedTitle.match(pattern);

    if (match) {
      // if more than 20% of titles contain the separator pattern, consider it common and do not split
      const hasExistingTitleWithSeparator =
        titles.filter((title) => pattern.test(title.toLowerCase())).length /
          titles.length >
        0.2;

      if (!hasExistingTitleWithSeparator) {
        const parts = preprocessedTitle.split(pattern);
        if (parts.length > 1 && parts[0]?.trim()) {
          const originalTitle = preprocessedTitle;
          preprocessedTitle = parts[0].trim();
          logger.silly(
            `Updated title from "${originalTitle}" to "${preprocessedTitle}"`
          );
          break;
        }
      }
    }
  }

  if (
    titles.some((title) => title.toLowerCase().includes('saga')) &&
    filename?.toLowerCase().includes('saga') &&
    !preprocessedTitle.toLowerCase().includes('saga')
  ) {
    preprocessedTitle += ' Saga';
  }

  return preprocessedTitle;
}

export function normaliseTitle(title: string) {
  return title
    .replace(/[ÄäÖöÜüß]/g, (c) => umlautMap[c])
    .replace(/&/g, 'and')
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .replace(/[^\p{L}\p{N}+]/gu, '')
    .toLowerCase();
}

export function cleanTitle(title: string) {
  // replace German umlauts with ASCII equivalents, then normalize to NFD
  let cleaned = title
    .replace(/[ÄäÖöÜüß]/g, (c) => umlautMap[c])
    .normalize('NFD');

  for (const char of ['♪', '♫', '★', '☆', '♡', '♥', '-']) {
    cleaned = cleaned.replaceAll(char, ' ');
  }

  return cleaned
    .replace(/&/g, 'and')
    .replace(/[\u0300-\u036f]/g, '') // Remove diacritics
    .replace(/[^\p{L}\p{N}\s]/gu, '') // Remove remaining special chars
    .replace(/\s+/g, ' ') // Normalise spaces
    .toLowerCase()
    .trim();
}

export function parseDuration(
  durationString: string,
  output: 'ms' | 's' = 'ms'
): number | undefined {
  // Regular expression to match different formats of time durations
  const regex =
    /(?<![^\s\[(_\-,.])(?:(\d+)h[:\s]?(\d+)m[:\s]?(\d+)s|(\d+)h[:\s]?(\d+)m|(\d+)m[:\s]?(\d+)s|(\d+)h|(\d+)m|(\d+)s)(?=[\s\)\]_.\-,]|$)/gi;

  const match = regex.exec(durationString);
  if (!match) {
    return 0;
  }

  const hours = parseInt(match[1] || match[4] || match[8] || '0', 10);
  const minutes = parseInt(
    match[2] || match[5] || match[6] || match[9] || '0',
    10
  );
  const seconds = parseInt(match[3] || match[7] || match[10] || '0', 10);

  // Convert to milliseconds
  const totalMilliseconds = (hours * 3600 + minutes * 60 + seconds) * 1000;
  if (output === 's') {
    return Math.floor(totalMilliseconds / 1000);
  }

  return totalMilliseconds;
}

export function parseAgeString(ageString: string): number | undefined {
  const match = ageString.match(/^(\d+)([a-zA-Z])$/);
  if (!match) {
    return undefined;
  }

  const value = parseInt(match[1], 10);
  const unit = match[2].toLowerCase();

  switch (unit) {
    case 'd':
      return value * 24;
    case 'h':
      return value;
    case 'm':
      return value / 60;
    case 'y':
      return value * 24 * 365;
    default:
      return undefined;
  }
}

export function parseBitrate(bitrateString: string): number | undefined {
  const match = bitrateString.match(
    /^(\d+(\.\d+)?)\s*(bps|kbps|mbps|gbps|tbps)$/i
  );
  if (!match) {
    const trimmed = bitrateString.trim();
    if (!/^\d+(\.\d+)?$/.test(trimmed)) {
      return undefined;
    }
    return parseFloat(trimmed);
  }
  const num = parseFloat(match[1]);
  const unit = match[3].toLowerCase();
  switch (unit) {
    case 'bps':
      return num;
    case 'kbps':
      return num * 1000;
    case 'mbps':
      return num * 1000000;
    case 'gbps':
      return num * 1000000000;
    case 'tbps':
      return num * 1000000000000;
    default:
      return num;
  }
}

function base32ToHex(base32: string): string {
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  let bits = '';
  for (const char of base32.toUpperCase()) {
    bits += alphabet.indexOf(char).toString(2).padStart(5, '0');
  }
  let hex = '';
  for (let i = 0; i + 4 <= bits.length; i += 4) {
    hex += parseInt(bits.slice(i, i + 4), 2).toString(16);
  }
  return hex;
}

export function extractInfoHashFromMagnet(magnet: string): string | undefined {
  const match = magnet.match(
    /(?:urn(?::|%3A)btih(?::|%3A))([a-f0-9]{40}|[a-z2-7]{32})/i
  )?.[1];
  if (!match) return undefined;
  if (match.length === 40) return match.toLowerCase();
  return base32ToHex(match);
}