File size: 12,534 Bytes
20a7802 |
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
const fetch = require('node-fetch');
const winston = require('winston');
// Logger configuration
const logger = winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
class EasynewsSearcher {
constructor(username, password, maxRetries = 3, retryDelay = 1000) {
this.username = username;
this.password = password;
this.maxRetries = maxRetries;
this.retryDelay = retryDelay;
this.maxFileSize = 100; // Maximum file size in GB
}
getSearchUrl(searchTerm) {
const encodedSearchTerm = encodeURIComponent(searchTerm);
return `https://members.easynews.com/1.0/global5/search.html?submit=Search&gps=&sbj=&from=&ns=&fil=${encodedSearchTerm}&fex=&vc=&ac=&fty%5B%5D=VIDEO&s1=dtime&s1d=-&s2=nsubject&s2d=%2B&s3=nrfile&s3d=%2B&pby=1000&pno=1&sS=5&u=1&svL=&d1=&d1t=&d2=&d2t=&b1=&b1t=&b2=&b2t=&px1=&px1t=&px2=&px2t=&fps1=&fps1t=&fps2=&fps2t=&bps1=&bps1t=&bps2=&bps2t=&hz1=&hz1t=&hz2=&hz2t=&rn1=&rn1t=&rn2=&rn2t=&fly=1`;
}
detectQuality(filename) {
const qualityPatterns = [
{
quality: '4K',
patterns: [/2160p/i, /4K/i, /UHD/i, /ULTRA.?HD/i]
},
{
quality: '1080p',
patterns: [/1080[pi]/i, /FHD/i, /FULL.?HD/i]
},
{
quality: '720p',
patterns: [/720p/i, /HD(?!TV)/i]
},
{
quality: '480p',
patterns: [/480[pi]/i, /SD\b/i]
}
];
for (const { quality, patterns } of qualityPatterns) {
if (patterns.some(pattern => pattern.test(filename))) {
return quality;
}
}
// Default quality if no match found
return 'SD';
}
getQualityEmoji(quality) {
const emojiMap = {
'4K': '๐',
'1080p': '๐ฅ',
'720p': '๐บ',
'480p': '๐ฑ',
'SD': '๐พ'
};
return emojiMap[quality] || '๐บ';
}
detectLanguagesFromFilename(filename) {
const languagePatterns = {
'English': [
/\bENG\b/i,
/\bENGLISH\b/i,
/\bEN\b/i,
/\bDUAL[._]AUDIO\b/i
],
'French': [
/\bFR\b/i,
/\bFRENCH\b/i,
/\bVFF\b/i,
/\bTRUEFRENCH\b/i
],
'German': [
/\bGER(MAN)?\b/i,
/\bDEU\b/i
],
'Spanish': [
/\bESP\b/i,
/\bSPA(NISH)?\b/i
],
'Italian': [
/\bITA(LIAN)?\b/i
],
'Multi': [
/\bMULTI\b/i,
/\bMULTILANGUAGE\b/i,
/\bDUAL[._]AUDIO\b/i
],
'Nordic': [
/\bNORDIC\b/i,
/\bNORDiC\b/i
]
};
const languages = new Set();
for (const [language, patterns] of Object.entries(languagePatterns)) {
if (patterns.some(pattern => pattern.test(filename))) {
languages.add(language);
}
}
return Array.from(languages);
}
getLanguageFromCode(code) {
const languageMap = {
'us': 'English',
'gb': 'English',
'ca': 'English',
'au': 'English',
'nz': 'English',
'fr': 'French',
'de': 'German',
'es': 'Spanish',
'it': 'Italian',
'jp': 'Japanese',
'kr': 'Korean',
'cn': 'Chinese',
'ru': 'Russian',
'nl': 'Dutch',
'pl': 'Polish',
'se': 'Swedish',
'dk': 'Danish',
'no': 'Norwegian',
'fi': 'Finnish',
'pt': 'Portuguese',
'br': 'Portuguese',
'tr': 'Turkish',
'nordic': 'Nordic'
};
return languageMap[code.toLowerCase()] || code;
}
getLanguageEmojis(languages) {
const emojiMap = {
'English': ['๐บ๐ธ', '๐ฌ๐ง'],
'French': '๐ซ๐ท',
'German': '๐ฉ๐ช',
'Spanish': '๐ช๐ธ',
'Italian': '๐ฎ๐น',
'Japanese': '๐ฏ๐ต',
'Korean': '๐ฐ๐ท',
'Chinese': '๐จ๐ณ',
'Russian': '๐ท๐บ',
'Dutch': '๐ณ๐ฑ',
'Polish': '๐ต๐ฑ',
'Swedish': '๐ธ๐ช',
'Danish': '๐ฉ๐ฐ',
'Norwegian': '๐ณ๐ด',
'Finnish': '๐ซ๐ฎ',
'Portuguese': '๐ต๐น',
'Turkish': '๐น๐ท',
'Nordic': '๐ฉ๐ฐ',
'Multi': '๐'
};
return languages.flatMap(lang => {
const emoji = emojiMap[lang];
return Array.isArray(emoji) ? emoji : [emoji || '๐ณ๏ธ'];
});
}
convertToGB(fileSize) {
const size = parseFloat(fileSize);
const unit = fileSize.split(' ')[1]?.toLowerCase() || 'gb';
switch (unit) {
case 'kb': return size / (1024 * 1024);
case 'mb': return size / 1024;
case 'gb': return size;
case 'tb': return size * 1024;
default: return 0;
}
}
extractFileInfo(filename) {
// Clean up filename
let cleanName = filename
.replace(/\.[^/.]+$/, '') // Remove extension
.replace(/\b(?:480p|720p|1080[pi]|2160p|4k|uhd|hdr|bluray|webrip|web-dl|webdl|web)\b.*$/i, '')
.replace(/\./g, ' ')
.replace(/-/g, ' ')
.trim();
// Extract year
const yearMatch = cleanName.match(/\b(?:19|20)\d{2}\b/);
const year = yearMatch ? parseInt(yearMatch[0]) : null;
if (year) {
cleanName = cleanName.replace(yearMatch[0], '').trim();
}
// Extract season/episode
const seasonEpMatch = cleanName.match(/S(\d{1,2})E(\d{1,2})/i);
const season = seasonEpMatch ? parseInt(seasonEpMatch[1]) : null;
const episode = seasonEpMatch ? parseInt(seasonEpMatch[2]) : null;
if (season && episode) {
cleanName = cleanName.replace(/S\d{1,2}E\d{1,2}/i, '').trim();
}
// Clean title
cleanName = cleanName
.replace(/\[.*?\]/g, '')
.replace(/\(.*?\)/g, '')
.replace(/\s+/g, ' ')
.trim();
return {
title: cleanName,
year,
season,
episode
};
}
parseXMLItem(itemXml) {
try {
// Extract URL
const enclosureMatch = itemXml.match(/<enclosure url="([^"]+)"/);
if (!enclosureMatch) return null;
let url = enclosureMatch[1];
url = this.decodeHTMLEntities(url);
// Extract file size
const sizeMatch = itemXml.match(/length="([^"]+)"/);
const fileSize = sizeMatch ? this.decodeHTMLEntities(sizeMatch[1]) : '0 B';
// Get filename from URL
const filenameMatch = url.match(/\/([^\/]+\.(?:mkv|mp4|avi|ts))(?:\?|$)/i);
if (!filenameMatch) return null;
const filename = this.decodeHTMLEntities(decodeURIComponent(filenameMatch[1]));
// Skip sample files
if (/(^|-|\s)sample[s]?(\.|$|\s|-)/i.test(filename)) {
logger.debug(`Skipping sample file: ${filename}`);
return null;
}
// Check file size
const sizeInGB = this.convertToGB(fileSize);
if (sizeInGB > this.maxFileSize) {
logger.debug(`Skipping large file: ${filename} (${fileSize})`);
return null;
}
// Extract information
const info = this.extractFileInfo(filename);
// Extract languages from flags
const flagCodes = new Set();
const flagMatches = itemXml.matchAll(/flags\/16\/([^.]+)\.png/g);
for (const match of flagMatches) {
flagCodes.add(match[1].toLowerCase());
}
// Get languages
const filenameLanguages = this.detectLanguagesFromFilename(filename);
const flagLanguages = Array.from(flagCodes).map(code => this.getLanguageFromCode(code));
const allLanguages = Array.from(new Set([...flagLanguages, ...filenameLanguages]));
// Default to English for standard releases
if (allLanguages.length === 0 &&
/\b(?:BluRay|WEB-DL|WEBRip|BRRip|DVDRip)\b/i.test(filename) &&
!/\b(?:FRENCH|GERMAN|SPANISH|ITALIAN|NORDIC)\b/i.test(filename)) {
allLanguages.push('English');
}
// Detect quality
const quality = this.detectQuality(filename);
return {
filename,
linkUrl: url,
fileSize,
quality,
qualityEmoji: this.getQualityEmoji(quality),
languages: allLanguages,
languageEmojis: this.getLanguageEmojis(allLanguages),
...info
};
} catch (error) {
logger.error(`Error parsing XML item: ${error.message}`);
return null;
}
}
decodeHTMLEntities(text) {
return text.replace(/&([^;]+);/g, (match, entity) => {
const entities = {
'amp': '&',
'apos': "'",
'lt': '<',
'gt': '>',
'quot': '"',
'nbsp': ' ',
'#046': '.',
'#058': ':',
'#047': '/',
'#040': '(',
'#041': ')',
'#064': '@',
'#037': '%',
'#043': '+',
'#061': '='
};
if (entity.startsWith('#')) {
const code = parseInt(entity.substring(1));
return isNaN(code) ? match : String.fromCharCode(code);
}
return entities[entity] || match;
});
}
async fetchWithRetry(fetchFunction, retries = this.maxRetries) {
try {
return await fetchFunction();
} catch (error) {
if (retries > 0) {
logger.warn(`Retrying... ${retries} attempts left`);
await new Promise(resolve => setTimeout(resolve, this.retryDelay));
return this.fetchWithRetry(fetchFunction, retries - 1);
} else {
throw error;
}
}
}
async fetchEasynewsRssFeed(searchTerm) {
const url = this.getSearchUrl(searchTerm);
logger.debug(`Fetching from URL: ${url}`);
const response = await fetch(url, {
headers: {
'Authorization': `Basic ${Buffer.from(`${this.username}:${this.password}`).toString('base64')}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.text();
}
async search(searchTerm) {
logger.info(`Searching Easynews for: ${searchTerm}`);
try {
const xml = await this.fetchWithRetry(() => this.fetchEasynewsRssFeed(searchTerm));
const itemRegex = /<item>([\s\S]*?)<\/item>/g;
const results = [];
let match;
while ((match = itemRegex.exec(xml)) !== null) {
const result = this.parseXMLItem(match[1]);
if (result) {
results.push(result);
}
}
logger.info(`Found ${results.length} results`);
return results;
} catch (error) {
logger.error(`Failed to search Easynews for: ${searchTerm}`, error);
return [];
}
}
}
module.exports = EasynewsSearcher; |