Update server.js
Browse files
server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
-
import { fetchRSSFeeds } from './rutracker.js';
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
const port = process.env.PORT || 7860;
|
|
@@ -9,6 +9,7 @@ const TMDB_BASE_URL = 'https://api.themoviedb.org/3';
|
|
| 9 |
app.use(express.json());
|
| 10 |
|
| 11 |
function standardizeEpisodeFormat(query) {
|
|
|
|
| 12 |
const altMatch = query.match(/^(.*?)(\d{1,2})x(\d{1,2})/i);
|
| 13 |
if (altMatch) {
|
| 14 |
const [_, title, season, episode] = altMatch;
|
|
@@ -21,26 +22,33 @@ function standardizeEpisodeFormat(query) {
|
|
| 21 |
|
| 22 |
function isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {
|
| 23 |
if (!torrentTitle || !showTitle) return false;
|
| 24 |
-
|
|
|
|
| 25 |
torrentTitle = torrentTitle.replace(/\[.*?\]/g, '')
|
| 26 |
.replace(/\(.*?\)/g, '')
|
| 27 |
.replace(/\s+/g, ' ')
|
| 28 |
.trim();
|
| 29 |
-
|
|
|
|
| 30 |
const normalizedTorrentTitle = torrentTitle.toLowerCase();
|
| 31 |
const normalizedShowTitle = showTitle.toLowerCase().trim();
|
| 32 |
-
|
|
|
|
| 33 |
const showWords = normalizedShowTitle.split(' ')
|
| 34 |
.filter(word => word.length > 2)
|
| 35 |
.filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));
|
| 36 |
-
|
| 37 |
-
const hasAllShowWords = showWords.every(word =>
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
if (!hasAllShowWords) return false;
|
| 40 |
-
|
|
|
|
| 41 |
const seasonStr = String(seasonNum).padStart(2, '0');
|
| 42 |
const episodeStr = String(episodeNum).padStart(2, '0');
|
| 43 |
-
|
|
|
|
| 44 |
const patterns = [
|
| 45 |
new RegExp(`s${seasonStr}e${episodeStr}`, 'i'),
|
| 46 |
new RegExp(`${seasonNum}x${episodeStr}`, 'i'),
|
|
@@ -49,27 +57,30 @@ function isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {
|
|
| 49 |
new RegExp(`s${seasonStr}\\.?e${episodeStr}`, 'i'),
|
| 50 |
new RegExp(`${seasonStr}${episodeStr}`, 'i')
|
| 51 |
];
|
| 52 |
-
|
| 53 |
return patterns.some(pattern => pattern.test(normalizedTorrentTitle));
|
| 54 |
}
|
| 55 |
|
| 56 |
function isExactMovieMatch(torrentTitle, movieTitle, year) {
|
|
|
|
| 57 |
torrentTitle = torrentTitle.replace(/\[.*?\]/g, '')
|
| 58 |
.replace(/\(.*?\)/g, '')
|
| 59 |
.replace(/\s+/g, ' ')
|
| 60 |
.trim();
|
| 61 |
-
|
| 62 |
const normalizedTorrentTitle = torrentTitle.toLowerCase();
|
| 63 |
const normalizedMovieTitle = movieTitle.toLowerCase();
|
| 64 |
-
|
| 65 |
const movieWords = normalizedMovieTitle.split(' ')
|
| 66 |
.filter(word => word.length > 2)
|
| 67 |
.filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));
|
| 68 |
-
|
| 69 |
-
const hasAllMovieWords = movieWords.every(word =>
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
if (!hasAllMovieWords) return false;
|
| 72 |
-
|
| 73 |
const yearMatch = torrentTitle.match(/(?:19|20)\d{2}/);
|
| 74 |
return yearMatch && yearMatch[0] === year.toString();
|
| 75 |
}
|
|
@@ -123,6 +134,8 @@ async function handleSearch({ query, type }) {
|
|
| 123 |
let exactMatchParams = {};
|
| 124 |
let tmdbData = null;
|
| 125 |
|
|
|
|
|
|
|
| 126 |
if (type === 'series') {
|
| 127 |
const tmdbMatch = query.match(/^tmdb:(\d+):(\d+):(\d+)$/);
|
| 128 |
if (tmdbMatch) {
|
|
@@ -135,9 +148,33 @@ async function handleSearch({ query, type }) {
|
|
| 135 |
episode: parseInt(episodeNum)
|
| 136 |
};
|
| 137 |
searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;
|
|
|
|
| 138 |
}
|
| 139 |
-
}
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
searchQuery = standardizeEpisodeFormat(query);
|
| 142 |
const seasonEpisodeMatch = searchQuery.match(/^(.*?)S(\d{1,2})E(\d{1,2})/i);
|
| 143 |
if (seasonEpisodeMatch) {
|
|
@@ -151,6 +188,7 @@ async function handleSearch({ query, type }) {
|
|
| 151 |
} else if (type === 'movie') {
|
| 152 |
if (query.startsWith('tmdb:')) {
|
| 153 |
const tmdbId = query.replace('tmdb:', '');
|
|
|
|
| 154 |
tmdbData = await getTMDBDetails(tmdbId, 'movie');
|
| 155 |
if (tmdbData) {
|
| 156 |
exactMatchParams = {
|
|
@@ -158,6 +196,23 @@ async function handleSearch({ query, type }) {
|
|
| 158 |
year: new Date(tmdbData.release_date).getFullYear()
|
| 159 |
};
|
| 160 |
searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
} else {
|
| 163 |
const movieYearMatch = query.match(/^(.+?)(?:\s*\(?(\d{4})\)?)?$/);
|
|
@@ -170,15 +225,28 @@ async function handleSearch({ query, type }) {
|
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
|
|
|
|
|
|
| 173 |
let results = await fetchRSSFeeds(searchQuery, type);
|
| 174 |
|
|
|
|
|
|
|
| 175 |
if (type === 'series' && exactMatchParams.season !== undefined) {
|
| 176 |
results = results.filter(result =>
|
| 177 |
-
isExactEpisodeMatch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
);
|
| 179 |
} else if (type === 'movie' && exactMatchParams.year) {
|
| 180 |
results = results.filter(result =>
|
| 181 |
-
isExactMovieMatch(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
);
|
| 183 |
}
|
| 184 |
|
|
@@ -191,8 +259,6 @@ async function handleSearch({ query, type }) {
|
|
| 191 |
title: result.websiteTitle,
|
| 192 |
quality: result.quality,
|
| 193 |
size: result.size,
|
| 194 |
-
seeders: 0, // Knaben RSS doesn't provide seeder info
|
| 195 |
-
leechers: 0, // Knaben RSS doesn't provide leecher info
|
| 196 |
magnetLink: result.magnetLink
|
| 197 |
}))
|
| 198 |
};
|
|
@@ -221,4 +287,4 @@ app.listen(port, () => {
|
|
| 221 |
if (!TMDB_API_KEY) {
|
| 222 |
console.warn('Warning: TMDB_API_KEY not set. TMDB features will not work.');
|
| 223 |
}
|
| 224 |
-
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
+
import { fetchRSSFeeds } from './rutracker.js';
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
const port = process.env.PORT || 7860;
|
|
|
|
| 9 |
app.use(express.json());
|
| 10 |
|
| 11 |
function standardizeEpisodeFormat(query) {
|
| 12 |
+
// Handle 1x01 format
|
| 13 |
const altMatch = query.match(/^(.*?)(\d{1,2})x(\d{1,2})/i);
|
| 14 |
if (altMatch) {
|
| 15 |
const [_, title, season, episode] = altMatch;
|
|
|
|
| 22 |
|
| 23 |
function isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {
|
| 24 |
if (!torrentTitle || !showTitle) return false;
|
| 25 |
+
|
| 26 |
+
// Clean up title
|
| 27 |
torrentTitle = torrentTitle.replace(/\[.*?\]/g, '')
|
| 28 |
.replace(/\(.*?\)/g, '')
|
| 29 |
.replace(/\s+/g, ' ')
|
| 30 |
.trim();
|
| 31 |
+
|
| 32 |
+
// Normalize the titles
|
| 33 |
const normalizedTorrentTitle = torrentTitle.toLowerCase();
|
| 34 |
const normalizedShowTitle = showTitle.toLowerCase().trim();
|
| 35 |
+
|
| 36 |
+
// Split show title into significant words
|
| 37 |
const showWords = normalizedShowTitle.split(' ')
|
| 38 |
.filter(word => word.length > 2)
|
| 39 |
.filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));
|
| 40 |
+
|
| 41 |
+
const hasAllShowWords = showWords.every(word =>
|
| 42 |
+
normalizedTorrentTitle.includes(word)
|
| 43 |
+
);
|
| 44 |
+
|
| 45 |
if (!hasAllShowWords) return false;
|
| 46 |
+
|
| 47 |
+
// Format season and episode numbers
|
| 48 |
const seasonStr = String(seasonNum).padStart(2, '0');
|
| 49 |
const episodeStr = String(episodeNum).padStart(2, '0');
|
| 50 |
+
|
| 51 |
+
// Common episode patterns
|
| 52 |
const patterns = [
|
| 53 |
new RegExp(`s${seasonStr}e${episodeStr}`, 'i'),
|
| 54 |
new RegExp(`${seasonNum}x${episodeStr}`, 'i'),
|
|
|
|
| 57 |
new RegExp(`s${seasonStr}\\.?e${episodeStr}`, 'i'),
|
| 58 |
new RegExp(`${seasonStr}${episodeStr}`, 'i')
|
| 59 |
];
|
| 60 |
+
|
| 61 |
return patterns.some(pattern => pattern.test(normalizedTorrentTitle));
|
| 62 |
}
|
| 63 |
|
| 64 |
function isExactMovieMatch(torrentTitle, movieTitle, year) {
|
| 65 |
+
// Clean title
|
| 66 |
torrentTitle = torrentTitle.replace(/\[.*?\]/g, '')
|
| 67 |
.replace(/\(.*?\)/g, '')
|
| 68 |
.replace(/\s+/g, ' ')
|
| 69 |
.trim();
|
| 70 |
+
|
| 71 |
const normalizedTorrentTitle = torrentTitle.toLowerCase();
|
| 72 |
const normalizedMovieTitle = movieTitle.toLowerCase();
|
| 73 |
+
|
| 74 |
const movieWords = normalizedMovieTitle.split(' ')
|
| 75 |
.filter(word => word.length > 2)
|
| 76 |
.filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));
|
| 77 |
+
|
| 78 |
+
const hasAllMovieWords = movieWords.every(word =>
|
| 79 |
+
normalizedTorrentTitle.includes(word)
|
| 80 |
+
);
|
| 81 |
+
|
| 82 |
if (!hasAllMovieWords) return false;
|
| 83 |
+
|
| 84 |
const yearMatch = torrentTitle.match(/(?:19|20)\d{2}/);
|
| 85 |
return yearMatch && yearMatch[0] === year.toString();
|
| 86 |
}
|
|
|
|
| 134 |
let exactMatchParams = {};
|
| 135 |
let tmdbData = null;
|
| 136 |
|
| 137 |
+
console.log(`๐ Received search request: ${query} (Type: ${type})`);
|
| 138 |
+
|
| 139 |
if (type === 'series') {
|
| 140 |
const tmdbMatch = query.match(/^tmdb:(\d+):(\d+):(\d+)$/);
|
| 141 |
if (tmdbMatch) {
|
|
|
|
| 148 |
episode: parseInt(episodeNum)
|
| 149 |
};
|
| 150 |
searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;
|
| 151 |
+
console.log(`๐บ TMDB ID resolved to: ${searchQuery}`);
|
| 152 |
}
|
| 153 |
+
} else if (query.startsWith('tt')) {
|
| 154 |
+
const imdbMatch = query.match(/^(tt\d+):(\d+):(\d+)$/);
|
| 155 |
+
if (imdbMatch) {
|
| 156 |
+
const [_, imdbId, seasonNum, episodeNum] = imdbMatch;
|
| 157 |
+
const findResponse = await fetch(
|
| 158 |
+
`${TMDB_BASE_URL}/find/${imdbId}?api_key=${TMDB_API_KEY}&external_source=imdb_id`
|
| 159 |
+
);
|
| 160 |
+
if (findResponse.ok) {
|
| 161 |
+
const findData = await findResponse.json();
|
| 162 |
+
if (findData.tv_results?.[0]) {
|
| 163 |
+
const tmdbId = findData.tv_results[0].id;
|
| 164 |
+
tmdbData = await getTVShowDetails(tmdbId, parseInt(seasonNum), parseInt(episodeNum));
|
| 165 |
+
if (tmdbData) {
|
| 166 |
+
exactMatchParams = {
|
| 167 |
+
title: tmdbData.showTitle,
|
| 168 |
+
season: parseInt(seasonNum),
|
| 169 |
+
episode: parseInt(episodeNum)
|
| 170 |
+
};
|
| 171 |
+
searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;
|
| 172 |
+
console.log(`๐บ IMDb ID resolved to: ${searchQuery}`);
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
} else {
|
| 178 |
searchQuery = standardizeEpisodeFormat(query);
|
| 179 |
const seasonEpisodeMatch = searchQuery.match(/^(.*?)S(\d{1,2})E(\d{1,2})/i);
|
| 180 |
if (seasonEpisodeMatch) {
|
|
|
|
| 188 |
} else if (type === 'movie') {
|
| 189 |
if (query.startsWith('tmdb:')) {
|
| 190 |
const tmdbId = query.replace('tmdb:', '');
|
| 191 |
+
console.log(`๐ TMDB ID detected: ${tmdbId}`);
|
| 192 |
tmdbData = await getTMDBDetails(tmdbId, 'movie');
|
| 193 |
if (tmdbData) {
|
| 194 |
exactMatchParams = {
|
|
|
|
| 196 |
year: new Date(tmdbData.release_date).getFullYear()
|
| 197 |
};
|
| 198 |
searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;
|
| 199 |
+
console.log(`๐ฌ TMDB ID resolved to: ${searchQuery}`);
|
| 200 |
+
}
|
| 201 |
+
} else if (query.startsWith('tt')) {
|
| 202 |
+
const findResponse = await fetch(
|
| 203 |
+
`${TMDB_BASE_URL}/find/${query}?api_key=${TMDB_API_KEY}&external_source=imdb_id`
|
| 204 |
+
);
|
| 205 |
+
if (findResponse.ok) {
|
| 206 |
+
const findData = await findResponse.json();
|
| 207 |
+
if (findData.movie_results?.[0]) {
|
| 208 |
+
tmdbData = findData.movie_results[0];
|
| 209 |
+
exactMatchParams = {
|
| 210 |
+
title: tmdbData.title,
|
| 211 |
+
year: new Date(tmdbData.release_date).getFullYear()
|
| 212 |
+
};
|
| 213 |
+
searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;
|
| 214 |
+
console.log(`๐ฌ IMDb ID resolved to: ${searchQuery}`);
|
| 215 |
+
}
|
| 216 |
}
|
| 217 |
} else {
|
| 218 |
const movieYearMatch = query.match(/^(.+?)(?:\s*\(?(\d{4})\)?)?$/);
|
|
|
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
+
console.log(`๐ Fetching torrents for: ${searchQuery} (Original: ${query})`);
|
| 229 |
+
|
| 230 |
let results = await fetchRSSFeeds(searchQuery, type);
|
| 231 |
|
| 232 |
+
console.log(`โ
Found ${results.length} torrents for "${searchQuery}"`);
|
| 233 |
+
|
| 234 |
if (type === 'series' && exactMatchParams.season !== undefined) {
|
| 235 |
results = results.filter(result =>
|
| 236 |
+
isExactEpisodeMatch(
|
| 237 |
+
result.websiteTitle,
|
| 238 |
+
exactMatchParams.title,
|
| 239 |
+
exactMatchParams.season,
|
| 240 |
+
exactMatchParams.episode
|
| 241 |
+
)
|
| 242 |
);
|
| 243 |
} else if (type === 'movie' && exactMatchParams.year) {
|
| 244 |
results = results.filter(result =>
|
| 245 |
+
isExactMovieMatch(
|
| 246 |
+
result.websiteTitle,
|
| 247 |
+
exactMatchParams.title,
|
| 248 |
+
exactMatchParams.year
|
| 249 |
+
)
|
| 250 |
);
|
| 251 |
}
|
| 252 |
|
|
|
|
| 259 |
title: result.websiteTitle,
|
| 260 |
quality: result.quality,
|
| 261 |
size: result.size,
|
|
|
|
|
|
|
| 262 |
magnetLink: result.magnetLink
|
| 263 |
}))
|
| 264 |
};
|
|
|
|
| 287 |
if (!TMDB_API_KEY) {
|
| 288 |
console.warn('Warning: TMDB_API_KEY not set. TMDB features will not work.');
|
| 289 |
}
|
| 290 |
+
});
|