Update packages/addon/src/utils.ts
Browse files- packages/addon/src/utils.ts +55 -15
packages/addon/src/utils.ts
CHANGED
|
@@ -26,7 +26,7 @@ export function isBadVideo(file: FileData): boolean {
|
|
| 26 |
}
|
| 27 |
|
| 28 |
// Sanitizes the title by removing unnecessary file extensions and metadata
|
| 29 |
-
function sanitizeTitle(title: string
|
| 30 |
let sanitized = title
|
| 31 |
.replace(/\.(1080p|HEVC|x265|AAC|mp4|mkv|avi|mov|webm|flv|wmv)/gi, '') // Remove file extensions
|
| 32 |
.replace(/[-_]/g, ' ') // Replace hyphens and underscores with spaces
|
|
@@ -36,19 +36,37 @@ function sanitizeTitle(title: string, isStrict: boolean = false): string {
|
|
| 36 |
|
| 37 |
sanitized = sanitized.replace(/\b(Americas)\b/g, "America's"); // Preserve apostrophe
|
| 38 |
|
| 39 |
-
// Handle stricter sanitization for movies (e.g., "Alive" case)
|
| 40 |
-
if (isStrict) {
|
| 41 |
-
// Remove overly common words in movie titles like "The", "Alive"
|
| 42 |
-
sanitized = sanitized.replace(/\b(Alive|The|The\s)/g, '').trim();
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
log('sanitizeTitle - Sanitized title:', sanitized);
|
| 46 |
return sanitized;
|
| 47 |
}
|
| 48 |
|
| 49 |
-
|
|
|
|
| 50 |
const sanitizedQuery = query.toLowerCase().trim();
|
| 51 |
-
const sanitizedTitle = sanitizeTitle(fileTitle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
log('matchesTitle - Matching title:', { fileTitle, query, sanitizedTitle });
|
| 54 |
|
|
@@ -58,11 +76,10 @@ export function matchesTitle(fileTitle: string, query: string, strict: boolean):
|
|
| 58 |
return result;
|
| 59 |
}
|
| 60 |
|
| 61 |
-
|
| 62 |
-
const
|
| 63 |
-
log('matchesTitle - Loose match result:',
|
| 64 |
-
|
| 65 |
-
return matchResult;
|
| 66 |
}
|
| 67 |
|
| 68 |
export function createStreamUrl({
|
|
@@ -178,4 +195,27 @@ export function capitalizeFirstLetter(str: string): string {
|
|
| 178 |
const result = str.charAt(0).toUpperCase() + str.slice(1);
|
| 179 |
log('capitalizeFirstLetter - Capitalized first letter:', result);
|
| 180 |
return result;
|
| 181 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
// Sanitizes the title by removing unnecessary file extensions and metadata
|
| 29 |
+
function sanitizeTitle(title: string): string {
|
| 30 |
let sanitized = title
|
| 31 |
.replace(/\.(1080p|HEVC|x265|AAC|mp4|mkv|avi|mov|webm|flv|wmv)/gi, '') // Remove file extensions
|
| 32 |
.replace(/[-_]/g, ' ') // Replace hyphens and underscores with spaces
|
|
|
|
| 36 |
|
| 37 |
sanitized = sanitized.replace(/\b(Americas)\b/g, "America's"); // Preserve apostrophe
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
log('sanitizeTitle - Sanitized title:', sanitized);
|
| 40 |
return sanitized;
|
| 41 |
}
|
| 42 |
|
| 43 |
+
// New: Enhanced matching for "common word" cases
|
| 44 |
+
function matchesWithYearAndGenre(fileTitle: string, query: string, year: string | undefined, genre: string | undefined): boolean {
|
| 45 |
const sanitizedQuery = query.toLowerCase().trim();
|
| 46 |
+
const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();
|
| 47 |
+
|
| 48 |
+
// Handle cases where "Alive" is a common word but needs to be matched with the year/genre
|
| 49 |
+
const yearPattern = year ? new RegExp(`(${year})`, 'i') : null;
|
| 50 |
+
const genrePattern = genre ? new RegExp(`(${genre})`, 'i') : null;
|
| 51 |
+
|
| 52 |
+
// Match based on title, and if year or genre is available, match those as well
|
| 53 |
+
let match = sanitizedTitle.includes(sanitizedQuery);
|
| 54 |
+
|
| 55 |
+
if (match && yearPattern) {
|
| 56 |
+
match = yearPattern.test(sanitizedTitle);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
if (match && genrePattern) {
|
| 60 |
+
match = genrePattern.test(sanitizedTitle);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
log('matchesWithYearAndGenre - Title matching with year/genre:', { fileTitle, query, match });
|
| 64 |
+
return match;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
export function matchesTitle(fileTitle: string, query: string, strict: boolean, year?: string, genre?: string): boolean {
|
| 68 |
+
const sanitizedQuery = query.toLowerCase().trim();
|
| 69 |
+
const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();
|
| 70 |
|
| 71 |
log('matchesTitle - Matching title:', { fileTitle, query, sanitizedTitle });
|
| 72 |
|
|
|
|
| 76 |
return result;
|
| 77 |
}
|
| 78 |
|
| 79 |
+
// Use year and genre matching logic for problematic cases
|
| 80 |
+
const result = matchesWithYearAndGenre(fileTitle, query, year, genre);
|
| 81 |
+
log('matchesTitle - Loose match result:', result);
|
| 82 |
+
return result;
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
export function createStreamUrl({
|
|
|
|
| 195 |
const result = str.charAt(0).toUpperCase() + str.slice(1);
|
| 196 |
log('capitalizeFirstLetter - Capitalized first letter:', result);
|
| 197 |
return result;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
export function testTitleFunctions() {
|
| 201 |
+
log("testTitleFunctions - Sanitize Title Tests:");
|
| 202 |
+
log(sanitizeTitle("America's")); // Expected: "America's"
|
| 203 |
+
log(sanitizeTitle("Amérîcâ")); // Expected: "America"
|
| 204 |
+
log(sanitizeTitle("Am_er-ic.a")); // Expected: "Am er ic a"
|
| 205 |
+
log(sanitizeTitle("D'où vient-il?")); // Expected: "D'ou vient il"
|
| 206 |
+
log(sanitizeTitle("Fête du cinéma")); // Expected: "Fete du cinema"
|
| 207 |
+
|
| 208 |
+
log("\ntestTitleFunctions - Matches Title Tests:");
|
| 209 |
+
log(matchesTitle("America's Next Top Model", "America's", false)); // Expected: true
|
| 210 |
+
log(matchesTitle("Amérîcâ's Got Talent", "America", false)); // Expected: true
|
| 211 |
+
log(matchesTitle("Am_er-ic.a the Beautiful", "America the Beautiful", false)); // Expected: true
|
| 212 |
+
log(matchesTitle("La fête de l'été", "fete", false)); // Expected: true
|
| 213 |
+
log(matchesTitle("Fête du cinéma", "cinema", false)); // Expected: true
|
| 214 |
+
log(matchesTitle("Alive", "Alive", true)); // Expected: true
|
| 215 |
+
log(matchesTitle("Alive (1993)", "Alive", false, "1993", "Drama")); // Expected: true
|
| 216 |
+
|
| 217 |
+
log("End of Title Functions Test");
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
// Run the test
|
| 221 |
+
testTitleFunctions();
|