Download packages/addon/src/utils.ts from mikmc/easynews: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/spaces/mikmc/easynews/resolve/main/packages/addon/src/utils.ts
- Command line
-
hf download hf://spaces/mikmc/easynews/packages/addon/src/utils.ts
-
curl -L -o utils.ts https://huggingface.co/spaces/mikmc/easynews/resolve/main/packages/addon/src/utils.ts
5.62 kB
| import { EasynewsSearchResponse, FileData } from '@easynews/api'; | |
| import { MetaProviderResponse } from './meta'; | |
| import { ContentType } from 'stremio-addon-sdk'; | |
| import { parse as parseTorrentTitle } from 'parse-torrent-title'; | |
| // Configurable debug flag | |
| const DEBUG = true; | |
| const log = (...args: any[]) => DEBUG && console.log(...args); | |
| export function isBadVideo(file: FileData): boolean { | |
| const duration = file['14'] ?? ''; // Using the key '14' for duration | |
| log('isBadVideo - Checking if file is bad video:', { file, duration }); | |
| return ( | |
| !!duration.match(/^\d+s/) || | |
| !!duration.match('^[0-5]m') || | |
| file.passwd || | |
| file.virus || | |
| file.type.toUpperCase() !== 'VIDEO' | |
| ); | |
| } | |
| export function sanitizeTitle(title: string, isStrict: boolean = false): string { | |
| let sanitized = title | |
| .replace(/\.(1080p|HEVC|x265|AAC|mp4|mkv|avi|mov|webm|flv|wmv)/gi, '') // Remove file extensions | |
| .replace(/[-_]/g, ' ') // Replace hyphens and underscores with spaces | |
| .replace(/\s+/g, ' ') // Collapse multiple spaces | |
| .replace(/(\.S\d+E\d+)/gi, ' $1') // Preserve season/episode formatting | |
| .replace(/\b(Americas)\b/g, "America's") // Preserve apostrophe | |
| .toLowerCase() | |
| .trim(); | |
| // Stricter sanitization for possible single-word movie titles | |
| if (isStrict) { | |
| sanitized = sanitized.replace(/\b(Alive|The|The\s)/g, '').trim(); | |
| } | |
| log('sanitizeTitle - Sanitized title:', sanitized); | |
| return sanitized; | |
| } | |
| export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean { | |
| const sanitizedQuery = sanitizeTitle(query).toLowerCase(); | |
| const sanitizedTitle = sanitizeTitle(fileTitle, strict).toLowerCase(); | |
| log('matchesTitle - Matching title:', { fileTitle, query, sanitizedTitle }); | |
| if (strict) { | |
| // Attempt strict match only if it seems to be a single-word title query | |
| const singleWordQuery = sanitizedQuery.split(' ').length === 1; | |
| if (singleWordQuery) { | |
| const { title: parsedTitle } = parseTorrentTitle(fileTitle); | |
| const result = parsedTitle?.toLowerCase().trim() === sanitizedQuery; | |
| log('matchesTitle - Strict match result:', result); | |
| return result; | |
| } | |
| } | |
| const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i'); | |
| const matchResult = re.test(sanitizedTitle); | |
| log('matchesTitle - Loose match result:', matchResult); | |
| return matchResult; | |
| } | |
| export function createStreamUrl({ | |
| downURL, | |
| dlFarm, | |
| dlPort, | |
| }: Pick<EasynewsSearchResponse, 'downURL' | 'dlFarm' | 'dlPort'>): string { | |
| log('createStreamUrl - Creating stream URL:', { downURL, dlFarm, dlPort }); | |
| return `${downURL}/${dlFarm}/${dlPort}`; | |
| } | |
| export function createStreamPath(file: FileData): string { | |
| const postHash = file['0'] ?? ''; | |
| const postTitle = file['10'] ?? ''; | |
| const ext = file['11'] ?? ''; | |
| log('createStreamPath - Creating stream path:', { postHash, postTitle, ext }); | |
| return `${postHash}${ext}/${postTitle}${ext}`; | |
| } | |
| export function createStreamAuth(username: string, password: string): string { | |
| log('createStreamAuth - Creating stream authorization for:', username); | |
| return `Authorization=${encodeURIComponent(username + ':' + password)}`; | |
| } | |
| export function getFileExtension(file: FileData): string { | |
| const extension = file['2'] ?? ''; | |
| log('getFileExtension - Getting file extension:', extension); | |
| return extension; | |
| } | |
| export function getPostTitle(file: FileData): string { | |
| const postTitle = file['10'] ?? ''; | |
| log('getPostTitle - Getting post title:', postTitle); | |
| return postTitle; | |
| } | |
| export function getDuration(file: FileData): string { | |
| const duration = file['14'] ?? ''; | |
| log('getDuration - Getting file duration:', duration); | |
| return duration; | |
| } | |
| export function getSize(file: FileData): string { | |
| const size = file['4'] ?? ''; | |
| log('getSize - Getting file size:', size); | |
| return size; | |
| } | |
| export function getQuality(title: string, fallbackResolution?: string): string | undefined { | |
| const resolution = title.match(/(720p|1080p|4K|HD|SD)/i)?.[0]; | |
| log('getQuality - Parsed resolution from title:', { resolution, fallbackResolution }); | |
| return resolution ?? fallbackResolution; | |
| } | |
| export 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`; | |
| log('createThumbnailUrl - Creating thumbnail URL:', thumbnailUrl); | |
| return thumbnailUrl; | |
| } | |
| export function extractDigits(value: string): number | undefined { | |
| const match = value.match(/\d+/); | |
| return match ? parseInt(match[0], 10) : undefined; | |
| } | |
| export function buildSearchQuery(type: ContentType, meta: MetaProviderResponse): string { | |
| let query = `${meta.name}`; | |
| log('buildSearchQuery - Building search query for type:', { type, meta }); | |
| if (type === 'series') { | |
| if (meta.season) { | |
| query += ` S${meta.season.toString().padStart(2, '0')}`; | |
| } | |
| if (meta.episode) { | |
| query += `${!meta.season ? ' ' : ''}E${meta.episode.toString().padStart(2, '0')}`; | |
| } | |
| } | |
| if (meta.year) { | |
| query += ` ${meta.year}`; | |
| } | |
| log('buildSearchQuery - Final search query:', query); | |
| return query; | |
| } | |
| export function logError(message: { message: string; error: unknown; context: unknown }) { | |
| console.error('logError - Error logged:', message); | |
| } | |
| export function capitalizeFirstLetter(str: string): string { | |
| if (!str) return str; | |
| const result = str.charAt(0).toUpperCase() + str.slice(1); | |
| log('capitalizeFirstLetter - Capitalized first letter:', result); | |
| return result; | |
| } | |