Update packages/addon/src/utils.ts
Browse files- packages/addon/src/utils.ts +7 -89
packages/addon/src/utils.ts
CHANGED
|
@@ -24,12 +24,11 @@ function sanitizeTitle(title: string): string {
|
|
| 24 |
.replace(/(\.S\d+E\d+)/gi, ' $1') // Ensure SxxExx format is preserved for season/episode
|
| 25 |
.trim(); // Remove any leading or trailing spaces
|
| 26 |
|
| 27 |
-
sanitized = sanitized.replace(/\b(Americas)\b/g, "America's"); // Preserve apostrophe
|
| 28 |
-
|
| 29 |
console.log('Sanitized title:', sanitized);
|
| 30 |
return sanitized;
|
| 31 |
}
|
| 32 |
|
|
|
|
| 33 |
export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {
|
| 34 |
const sanitizedQuery = query.toLowerCase().trim();
|
| 35 |
const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();
|
|
@@ -38,114 +37,33 @@ export function matchesTitle(fileTitle: string, query: string, strict: boolean):
|
|
| 38 |
console.log('Query:', query);
|
| 39 |
console.log('Sanitized title:', sanitizedTitle);
|
| 40 |
|
| 41 |
-
// Handle
|
| 42 |
const isSingleWordQuery = sanitizedQuery.split(' ').length === 1;
|
| 43 |
const isSingleWordTitle = sanitizedTitle.split(' ').length === 1;
|
| 44 |
|
| 45 |
if (isSingleWordQuery && isSingleWordTitle) {
|
|
|
|
| 46 |
const result = sanitizedTitle === sanitizedQuery;
|
| 47 |
console.log('Strict match result for single-word title:', result);
|
| 48 |
return result;
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
if (strict) {
|
| 52 |
const result = sanitizedTitle === sanitizedQuery;
|
| 53 |
console.log('Strict match result for multi-word title:', result);
|
| 54 |
return result;
|
| 55 |
}
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
const matchResult = re.test(sanitizedTitle); // test() returns a boolean
|
| 59 |
console.log('Loose match result:', matchResult);
|
| 60 |
|
| 61 |
return matchResult;
|
| 62 |
}
|
| 63 |
|
| 64 |
-
//
|
| 65 |
-
export function createStreamUrl({
|
| 66 |
-
downURL,
|
| 67 |
-
dlFarm,
|
| 68 |
-
dlPort,
|
| 69 |
-
}: Pick<EasynewsSearchResponse, 'downURL' | 'dlFarm' | 'dlPort'>): string {
|
| 70 |
-
console.log('Creating stream URL:', { downURL, dlFarm, dlPort });
|
| 71 |
-
return `${downURL}/${dlFarm}/${dlPort}`;
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
// Builds the stream path using the file data
|
| 75 |
-
export function createStreamPath(file: FileData): string {
|
| 76 |
-
const postHash = file['0'] ?? '';
|
| 77 |
-
const postTitle = file['10'] ?? '';
|
| 78 |
-
const ext = file['11'] ?? '';
|
| 79 |
-
|
| 80 |
-
console.log('Creating stream path:', { postHash, postTitle, ext });
|
| 81 |
-
return `${postHash}${ext}/${postTitle}${ext}`;
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
// Creates the stream authorization string based on username and password
|
| 85 |
-
export function createStreamAuth(username: string, password: string): string {
|
| 86 |
-
console.log('Creating stream authorization for:', username);
|
| 87 |
-
return `Authorization=${encodeURIComponent(username + ':' + password)}`;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
// Gets the file extension from the file data
|
| 91 |
-
export function getFileExtension(file: FileData): string {
|
| 92 |
-
const extension = file['2'] ?? '';
|
| 93 |
-
console.log('Getting file extension:', extension);
|
| 94 |
-
return extension;
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
// Gets the post title from the file data
|
| 98 |
-
export function getPostTitle(file: FileData): string {
|
| 99 |
-
const postTitle = file['10'] ?? '';
|
| 100 |
-
console.log('Getting post title:', postTitle);
|
| 101 |
-
return postTitle;
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
// Gets the duration of the file
|
| 105 |
-
export function getDuration(file: FileData): string {
|
| 106 |
-
const duration = file['14'] ?? '';
|
| 107 |
-
console.log('Getting file duration:', duration);
|
| 108 |
-
return duration;
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
// Gets the size of the file
|
| 112 |
-
export function getSize(file: FileData): string {
|
| 113 |
-
const size = file['4'] ?? '';
|
| 114 |
-
console.log('Getting file size:', size);
|
| 115 |
-
return size;
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
// Returns the quality of the video based on the title or fallback resolution
|
| 119 |
-
export function getQuality(
|
| 120 |
-
title: string,
|
| 121 |
-
fallbackResolution?: string
|
| 122 |
-
): string | undefined {
|
| 123 |
-
const resolution = title.match(/(720p|1080p|4K|HD|SD)/i)?.[0];
|
| 124 |
-
console.log('Parsed resolution from title:', resolution, 'Fallback resolution:', fallbackResolution);
|
| 125 |
-
return resolution ?? fallbackResolution;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
// Builds the thumbnail URL from the response and file data
|
| 129 |
-
export function createThumbnailUrl(
|
| 130 |
-
res: EasynewsSearchResponse,
|
| 131 |
-
file: FileData
|
| 132 |
-
): string {
|
| 133 |
-
const id = file['0'];
|
| 134 |
-
const idChars = id.slice(0, 3);
|
| 135 |
-
const thumbnailSlug = file['10'];
|
| 136 |
-
const thumbnailUrl = `${res.thumbURL}${idChars}/pr-${id}.jpg/th-${thumbnailSlug}.jpg`;
|
| 137 |
-
|
| 138 |
-
console.log('Creating thumbnail URL:', thumbnailUrl);
|
| 139 |
-
return thumbnailUrl;
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
// Extracts digits from a string (e.g., year or other numeric data)
|
| 143 |
-
export function extractDigits(value: string): number | undefined {
|
| 144 |
-
const match = value.match(/\d+/);
|
| 145 |
-
return match ? parseInt(match[0], 10) : undefined;
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
// Builds a search query based on the type of content and meta information
|
| 149 |
export function buildSearchQuery(
|
| 150 |
type: ContentType,
|
| 151 |
meta: MetaProviderResponse
|
|
|
|
| 24 |
.replace(/(\.S\d+E\d+)/gi, ' $1') // Ensure SxxExx format is preserved for season/episode
|
| 25 |
.trim(); // Remove any leading or trailing spaces
|
| 26 |
|
|
|
|
|
|
|
| 27 |
console.log('Sanitized title:', sanitized);
|
| 28 |
return sanitized;
|
| 29 |
}
|
| 30 |
|
| 31 |
+
// This function will handle matching logic, specifically tailored for single and multi-word titles
|
| 32 |
export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {
|
| 33 |
const sanitizedQuery = query.toLowerCase().trim();
|
| 34 |
const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();
|
|
|
|
| 37 |
console.log('Query:', query);
|
| 38 |
console.log('Sanitized title:', sanitizedTitle);
|
| 39 |
|
| 40 |
+
// Handle single-word query and title strictly
|
| 41 |
const isSingleWordQuery = sanitizedQuery.split(' ').length === 1;
|
| 42 |
const isSingleWordTitle = sanitizedTitle.split(' ').length === 1;
|
| 43 |
|
| 44 |
if (isSingleWordQuery && isSingleWordTitle) {
|
| 45 |
+
// Strictly match for single-word titles (both movie or series)
|
| 46 |
const result = sanitizedTitle === sanitizedQuery;
|
| 47 |
console.log('Strict match result for single-word title:', result);
|
| 48 |
return result;
|
| 49 |
}
|
| 50 |
|
| 51 |
+
// If strict is enabled, match titles exactly
|
| 52 |
if (strict) {
|
| 53 |
const result = sanitizedTitle === sanitizedQuery;
|
| 54 |
console.log('Strict match result for multi-word title:', result);
|
| 55 |
return result;
|
| 56 |
}
|
| 57 |
|
| 58 |
+
// Loose match logic for multi-word titles (handle series and movies with multiple words)
|
| 59 |
+
const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i'); // Match the first part of the query
|
| 60 |
const matchResult = re.test(sanitizedTitle); // test() returns a boolean
|
| 61 |
console.log('Loose match result:', matchResult);
|
| 62 |
|
| 63 |
return matchResult;
|
| 64 |
}
|
| 65 |
|
| 66 |
+
// This function helps to build the search query by adding season and episode information for series
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
export function buildSearchQuery(
|
| 68 |
type: ContentType,
|
| 69 |
meta: MetaProviderResponse
|