Update server.js
Browse files123453 unmodified lines910111213146 unmodified lines21222324252627282930313233343536373839404142434445462 unmodified lines49505152535455565758596061626364656667686970717273747547 unmodified lines1231241251261271286 unmodified lines1351361371381391401411421437 unmodified lines1511521531541551561 unmodified line1581591601611621636 unmodified lines1701711721731741751761771781791801811821831846 unmodified lines19119219319419519619719822 unmodified lines221222223224import express from 'express';import { fetchRSSFeeds } from './rutracker.js'; // Adjust path to your rutracker.js fileconst app = express();const port = process.env.PORT || 7860;3 unmodified linesapp.use(express.json());function standardizeEpisodeFormat(query) {const altMatch = query.match(/^(.*?)(\d{1,2})x(\d{1,2})/i);if (altMatch) {const [_, title, season, episode] = altMatch;6 unmodified linesfunction isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {if (!torrentTitle || !showTitle) return false;torrentTitle = torrentTitle.replace(/\[.*?\]/g, '').replace(/\(.*?\)/g, '').replace(/\s+/g, ' ').trim();const normalizedTorrentTitle = torrentTitle.toLowerCase();const normalizedShowTitle = showTitle.toLowerCase().trim();const showWords = normalizedShowTitle.split(' ').filter(word => word.length > 2).filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));const hasAllShowWords = showWords.every(word => normalizedTorrentTitle.includes(word));if (!hasAllShowWords) return false;const seasonStr = String(seasonNum).padStart(2, '0');const episodeStr = String(episodeNum).padStart(2, '0');const patterns = [new RegExp(`s${seasonStr}e${episodeStr}`, 'i'),new RegExp(`${seasonNum}x${episodeStr}`, 'i'),2 unmodified linesnew RegExp(`s${seasonStr}\\.?e${episodeStr}`, 'i'),new RegExp(`${seasonStr}${episodeStr}`, 'i')];return patterns.some(pattern => pattern.test(normalizedTorrentTitle));}function isExactMovieMatch(torrentTitle, movieTitle, year) {torrentTitle = torrentTitle.replace(/\[.*?\]/g, '').replace(/\(.*?\)/g, '').replace(/\s+/g, ' ').trim();const normalizedTorrentTitle = torrentTitle.toLowerCase();const normalizedMovieTitle = movieTitle.toLowerCase();const movieWords = normalizedMovieTitle.split(' ').filter(word => word.length > 2).filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));const hasAllMovieWords = movieWords.every(word => normalizedTorrentTitle.includes(word));if (!hasAllMovieWords) return false;const yearMatch = torrentTitle.match(/(?:19|20)\d{2}/);return yearMatch && yearMatch[0] === year.toString();}47 unmodified lineslet exactMatchParams = {};let tmdbData = null;if (type === 'series') {const tmdbMatch = query.match(/^tmdb:(\d+):(\d+):(\d+)$/);if (tmdbMatch) {6 unmodified linesepisode: parseInt(episodeNum)};searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;}}else {searchQuery = standardizeEpisodeFormat(query);const seasonEpisodeMatch = searchQuery.match(/^(.*?)S(\d{1,2})E(\d{1,2})/i);if (seasonEpisodeMatch) {7 unmodified lines} else if (type === 'movie') {if (query.startsWith('tmdb:')) {const tmdbId = query.replace('tmdb:', '');tmdbData = await getTMDBDetails(tmdbId, 'movie');if (tmdbData) {exactMatchParams = {1 unmodified lineyear: new Date(tmdbData.release_date).getFullYear()};searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;}} else {const movieYearMatch = query.match(/^(.+?)(?:\s*\(?(\d{4})\)?)?$/);6 unmodified lines}}let results = await fetchRSSFeeds(searchQuery, type);if (type === 'series' && exactMatchParams.season !== undefined) {results = results.filter(result =>isExactEpisodeMatch(result.websiteTitle, exactMatchParams.title, exactMatchParams.season, exactMatchParams.episode));} else if (type === 'movie' && exactMatchParams.year) {results = results.filter(result =>isExactMovieMatch(result.websiteTitle, exactMatchParams.title, exactMatchParams.year));}6 unmodified linestitle: result.websiteTitle,quality: result.quality,size: result.size,seeders: 0, // Knaben RSS doesn't provide seeder infoleechers: 0, // Knaben RSS doesn't provide leecher infomagnetLink: result.magnetLink}))};22 unmodified linesif (!TMDB_API_KEY) {console.warn('Warning: TMDB_API_KEY not set. TMDB features will not work.');}});123453 unmodified lines91011121314156 unmodified lines2223242526272829303132333435363738394041424344454647484950515253542 unmodified lines57585960616263646566676869707172737475767778798081828384858647 unmodified lines1341351361371381391401416 unmodified lines1481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791807 unmodified lines1881891901911921931941 unmodified line1961971981992002012022032042052062072082092102112122132142152162172186 unmodified lines2252262272282292302312322332342352362372382392402412422432442452462472482492502512526 unmodified lines25926026126226326422 unmodified lines287288289290import express from 'express';import { fetchRSSFeeds } from './rutracker.js';const app = express();const port = process.env.PORT || 7860;3 unmodified linesapp.use(express.json());function standardizeEpisodeFormat(query) {// Handle 1x01 formatconst altMatch = query.match(/^(.*?)(\d{1,2})x(\d{1,2})/i);if (altMatch) {const [_, title, season, episode] = altMatch;6 unmodified linesfunction isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {if (!torrentTitle || !showTitle) return false;// Clean up titletorrentTitle = torrentTitle.replace(/\[.*?\]/g, '').replace(/\(.*?\)/g, '').replace(/\s+/g, ' ').trim();// Normalize the titlesconst normalizedTorrentTitle = torrentTitle.toLowerCase();const normalizedShowTitle = showTitle.toLowerCase().trim();// Split show title into significant wordsconst showWords = normalizedShowTitle.split(' ').filter(word => word.length > 2).filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));const hasAllShowWords = showWords.every(word =>normalizedTorrentTitle.includes(word));if (!hasAllShowWords) return false;// Format season and episode numbersconst seasonStr = String(seasonNum).padStart(2, '0');const episodeStr = String(episodeNum).padStart(2, '0');// Common episode patternsconst patterns = [new RegExp(`s${seasonStr}e${episodeStr}`, 'i'),new RegExp(`${seasonNum}x${episodeStr}`, 'i'),2 unmodified linesnew RegExp(`s${seasonStr}\\.?e${episodeStr}`, 'i'),new RegExp(`${seasonStr}${episodeStr}`, 'i')];return patterns.some(pattern => pattern.test(normalizedTorrentTitle));}function isExactMovieMatch(torrentTitle, movieTitle, year) {// Clean titletorrentTitle = torrentTitle.replace(/\[.*?\]/g, '').replace(/\(.*?\)/g, '').replace(/\s+/g, ' ').trim();const normalizedTorrentTitle = torrentTitle.toLowerCase();const normalizedMovieTitle = movieTitle.toLowerCase();const movieWords = normalizedMovieTitle.split(' ').filter(word => word.length > 2).filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));const hasAllMovieWords = movieWords.every(word =>normalizedTorrentTitle.includes(word));if (!hasAllMovieWords) return false;const yearMatch = torrentTitle.match(/(?:19|20)\d{2}/);return yearMatch && yearMatch[0] === year.toString();}47 unmodified lineslet exactMatchParams = {};let tmdbData = null;console.log(`๐ Received search request: ${query} (Type: ${type})`);if (type === 'series') {const tmdbMatch = query.match(/^tmdb:(\d+):(\d+):(\d+)$/);if (tmdbMatch) {6 unmodified linesepisode: parseInt(episodeNum)};searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;console.log(`๐บ TMDB ID resolved to: ${searchQuery}`);}} else if (query.startsWith('tt')) {const imdbMatch = query.match(/^(tt\d+):(\d+):(\d+)$/);if (imdbMatch) {const [_, imdbId, seasonNum, episodeNum] = imdbMatch;const findResponse = await fetch(`${TMDB_BASE_URL}/find/${imdbId}?api_key=${TMDB_API_KEY}&external_source=imdb_id`);if (findResponse.ok) {const findData = await findResponse.json();if (findData.tv_results?.[0]) {const tmdbId = findData.tv_results[0].id;tmdbData = await getTVShowDetails(tmdbId, parseInt(seasonNum), parseInt(episodeNum));if (tmdbData) {exactMatchParams = {title: tmdbData.showTitle,season: parseInt(seasonNum),episode: parseInt(episodeNum)};searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;console.log(`๐บ IMDb ID resolved to: ${searchQuery}`);}}}}} else {searchQuery = standardizeEpisodeFormat(query);const seasonEpisodeMatch = searchQuery.match(/^(.*?)S(\d{1,2})E(\d{1,2})/i);if (seasonEpisodeMatch) {7 unmodified lines} else if (type === 'movie') {if (query.startsWith('tmdb:')) {const tmdbId = query.replace('tmdb:', '');console.log(`๐ TMDB ID detected: ${tmdbId}`);tmdbData = await getTMDBDetails(tmdbId, 'movie');if (tmdbData) {exactMatchParams = {1 unmodified lineyear: new Date(tmdbData.release_date).getFullYear()};searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;console.log(`๐ฌ TMDB ID resolved to: ${searchQuery}`);}} else if (query.startsWith('tt')) {const findResponse = await fetch(`${TMDB_BASE_URL}/find/${query}?api_key=${TMDB_API_KEY}&external_source=imdb_id`);if (findResponse.ok) {const findData = await findResponse.json();if (findData.movie_results?.[0]) {tmdbData = findData.movie_results[0];exactMatchParams = {title: tmdbData.title,year: new Date(tmdbData.release_date).getFullYear()};searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;console.log(`๐ฌ IMDb ID resolved to: ${searchQuery}`);}}} else {const movieYearMatch = query.match(/^(.+?)(?:\s*\(?(\d{4})\)?)?$/);6 unmodified lines}}console.log(`๐ Fetching torrents for: ${searchQuery} (Original: ${query})`);let results = await fetchRSSFeeds(searchQuery, type);console.log(`โ Found ${results.length} torrents for "${searchQuery}"`);if (type === 'series' && exactMatchParams.season !== undefined) {results = results.filter(result =>isExactEpisodeMatch(result.websiteTitle,exactMatchParams.title,exactMatchParams.season,exactMatchParams.episode));} else if (type === 'movie' && exactMatchParams.year) {results = results.filter(result =>isExactMovieMatch(result.websiteTitle,exactMatchParams.title,exactMatchParams.year));}6 unmodified linestitle: result.websiteTitle,quality: result.quality,size: result.size,magnetLink: result.magnetLink}))};22 unmodified linesif (!TMDB_API_KEY) {console.warn('Warning: TMDB_API_KEY not set. TMDB features will not work.');}});
server.js+89-23