Update packages/addon/src/utils.ts
Browse files23 unmodified lines2425262728293031323334352 unmodified lines383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015123 unmodified lines.replace(/(\.S\d+E\d+)/gi, ' $1') // Ensure SxxExx format is preserved for season/episode.trim(); // Remove any leading or trailing spacessanitized = sanitized.replace(/\b(Americas)\b/g, "America's"); // Preserve apostropheconsole.log('Sanitized title:', sanitized);return sanitized;}export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {const sanitizedQuery = query.toLowerCase().trim();const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();2 unmodified linesconsole.log('Query:', query);console.log('Sanitized title:', sanitizedTitle);// Handle case for single word titles strictlyconst isSingleWordQuery = sanitizedQuery.split(' ').length === 1;const isSingleWordTitle = sanitizedTitle.split(' ').length === 1;if (isSingleWordQuery && isSingleWordTitle) {const result = sanitizedTitle === sanitizedQuery;console.log('Strict match result for single-word title:', result);return result;}if (strict) {const result = sanitizedTitle === sanitizedQuery;console.log('Strict match result for multi-word title:', result);return result;}const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i'); // Match the first part of the query onlyconst matchResult = re.test(sanitizedTitle); // test() returns a booleanconsole.log('Loose match result:', matchResult);return matchResult;}// Builds the stream URL using provided detailsexport function createStreamUrl({downURL,dlFarm,dlPort,}: Pick<EasynewsSearchResponse, 'downURL' | 'dlFarm' | 'dlPort'>): string {console.log('Creating stream URL:', { downURL, dlFarm, dlPort });return `${downURL}/${dlFarm}/${dlPort}`;}// Builds the stream path using the file dataexport function createStreamPath(file: FileData): string {const postHash = file['0'] ?? '';const postTitle = file['10'] ?? '';const ext = file['11'] ?? '';console.log('Creating stream path:', { postHash, postTitle, ext });return `${postHash}${ext}/${postTitle}${ext}`;}// Creates the stream authorization string based on username and passwordexport function createStreamAuth(username: string, password: string): string {console.log('Creating stream authorization for:', username);return `Authorization=${encodeURIComponent(username + ':' + password)}`;}// Gets the file extension from the file dataexport function getFileExtension(file: FileData): string {const extension = file['2'] ?? '';console.log('Getting file extension:', extension);return extension;}// Gets the post title from the file dataexport function getPostTitle(file: FileData): string {const postTitle = file['10'] ?? '';console.log('Getting post title:', postTitle);return postTitle;}// Gets the duration of the fileexport function getDuration(file: FileData): string {const duration = file['14'] ?? '';console.log('Getting file duration:', duration);return duration;}// Gets the size of the fileexport function getSize(file: FileData): string {const size = file['4'] ?? '';console.log('Getting file size:', size);return size;}// Returns the quality of the video based on the title or fallback resolutionexport function getQuality(title: string,fallbackResolution?: string): string | undefined {const resolution = title.match(/(720p|1080p|4K|HD|SD)/i)?.[0];console.log('Parsed resolution from title:', resolution, 'Fallback resolution:', fallbackResolution);return resolution ?? fallbackResolution;}// Builds the thumbnail URL from the response and file dataexport function createThumbnailUrl(res: EasynewsSearchResponse,file: FileData): string {const id = file['0'];const idChars = id.slice(0, 3);const thumbnailSlug = file['10'];const thumbnailUrl = `${res.thumbURL}${idChars}/pr-${id}.jpg/th-${thumbnailSlug}.jpg`;console.log('Creating thumbnail URL:', thumbnailUrl);return thumbnailUrl;}// Extracts digits from a string (e.g., year or other numeric data)export function extractDigits(value: string): number | undefined {const match = value.match(/\d+/);return match ? parseInt(match[0], 10) : undefined;}// Builds a search query based on the type of content and meta informationexport function buildSearchQuery(type: ContentType,meta: MetaProviderResponse23 unmodified lines24252627282930313233342 unmodified lines37383940414243444546474849505152535455565758596061626364656667686923 unmodified lines.replace(/(\.S\d+E\d+)/gi, ' $1') // Ensure SxxExx format is preserved for season/episode.trim(); // Remove any leading or trailing spacesconsole.log('Sanitized title:', sanitized);return sanitized;}// This function will handle matching logic, specifically tailored for single and multi-word titlesexport function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {const sanitizedQuery = query.toLowerCase().trim();const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();2 unmodified linesconsole.log('Query:', query);console.log('Sanitized title:', sanitizedTitle);// Handle single-word query and title strictlyconst isSingleWordQuery = sanitizedQuery.split(' ').length === 1;const isSingleWordTitle = sanitizedTitle.split(' ').length === 1;if (isSingleWordQuery && isSingleWordTitle) {// Strictly match for single-word titles (both movie or series)const result = sanitizedTitle === sanitizedQuery;console.log('Strict match result for single-word title:', result);return result;}// If strict is enabled, match titles exactlyif (strict) {const result = sanitizedTitle === sanitizedQuery;console.log('Strict match result for multi-word title:', result);return result;}// Loose match logic for multi-word titles (handle series and movies with multiple words)const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i'); // Match the first part of the queryconst matchResult = re.test(sanitizedTitle); // test() returns a booleanconsole.log('Loose match result:', matchResult);return matchResult;}// This function helps to build the search query by adding season and episode information for seriesexport function buildSearchQuery(type: ContentType,meta: MetaProviderResponse
packages/addon/src/utils.ts+7-89