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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const SITE_CONFIG = {
|
| 2 |
+
baseUrl: 'https://bitsearch.to',
|
| 3 |
+
fallbackUrls: [
|
| 4 |
+
'https://bitsearch.st',
|
| 5 |
+
'https://bitsearch.eu'
|
| 6 |
+
]
|
| 7 |
+
};
|
| 8 |
+
|
| 9 |
+
async function getCinemetaMetadata(imdbId) {
|
| 10 |
+
try {
|
| 11 |
+
if (!imdbId.startsWith('tt')) {
|
| 12 |
+
return null;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
console.log(`\n🎬 Fetching Cinemeta data for ${imdbId}`);
|
| 16 |
+
const response = await fetch(`https://v3-cinemeta.strem.io/meta/movie/${imdbId}.json`);
|
| 17 |
+
if (!response.ok) throw new Error('Failed to fetch from Cinemeta');
|
| 18 |
+
const data = await response.json();
|
| 19 |
+
console.log('✅ Found:', data.meta.name);
|
| 20 |
+
return data;
|
| 21 |
+
} catch (error) {
|
| 22 |
+
console.error('❌ Cinemeta error:', error);
|
| 23 |
+
return null;
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
async function fetchWithTimeout(url, options = {}, timeout = 30000) {
|
| 28 |
+
const controller = new AbortController();
|
| 29 |
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
| 30 |
+
|
| 31 |
+
try {
|
| 32 |
+
const response = await fetch(url, {
|
| 33 |
+
...options,
|
| 34 |
+
signal: controller.signal,
|
| 35 |
+
headers: {
|
| 36 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
| 37 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9',
|
| 38 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 39 |
+
...options.headers
|
| 40 |
+
}
|
| 41 |
+
});
|
| 42 |
+
clearTimeout(timeoutId);
|
| 43 |
+
return response;
|
| 44 |
+
} catch (error) {
|
| 45 |
+
clearTimeout(timeoutId);
|
| 46 |
+
throw error;
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
function parseSize(sizeStr) {
|
| 51 |
+
if (!sizeStr) return 0;
|
| 52 |
+
const match = sizeStr.match(/([\d.]+)\s*(B|KB|MB|GB|TB)/i);
|
| 53 |
+
if (!match) return 0;
|
| 54 |
+
|
| 55 |
+
const [, value, unit] = match;
|
| 56 |
+
const multipliers = {
|
| 57 |
+
'B': 1,
|
| 58 |
+
'KB': 1024,
|
| 59 |
+
'MB': 1024 * 1024,
|
| 60 |
+
'GB': 1024 * 1024 * 1024,
|
| 61 |
+
'TB': 1024 * 1024 * 1024 * 1024
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
return parseFloat(value) * multipliers[unit.toUpperCase()];
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
function parseSearchResults(html) {
|
| 68 |
+
if (!html) {
|
| 69 |
+
console.log('No HTML content to parse');
|
| 70 |
+
return [];
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
const results = [];
|
| 74 |
+
const searchResults = html.match(/<li class="card search-result my-2">[\s\S]*?<\/li>/g) || [];
|
| 75 |
+
|
| 76 |
+
for (const result of searchResults) {
|
| 77 |
+
try {
|
| 78 |
+
const titleMatch = result.match(/class="title w-100 truncate"><a[^>]*>([^<]+)<\/a>/);
|
| 79 |
+
const magnetMatch = result.match(/href="(magnet:[^"]+)"/);
|
| 80 |
+
const sizeMatch = result.match(/alt="Size"[^>]*>([^<]+)<\/div>/);
|
| 81 |
+
const seedersMatch = result.match(/alt="Seeder"[^>]*>[^<]*<font[^>]*>(\d+)<\/font>/);
|
| 82 |
+
const leechersMatch = result.match(/alt="Leecher"[^>]*>[^<]*<font[^>]*>(\d+)<\/font>/);
|
| 83 |
+
const categoryMatch = result.match(/class="category">([^<]+)<\/a>/);
|
| 84 |
+
const dateMatch = result.match(/alt="Date"[^>]*>([^<]+)<\/div>/);
|
| 85 |
+
|
| 86 |
+
if (titleMatch && magnetMatch) {
|
| 87 |
+
const title = titleMatch[1].trim();
|
| 88 |
+
const quality = extractQuality(title);
|
| 89 |
+
const size = sizeMatch ? sizeMatch[1].trim() : 'Unknown';
|
| 90 |
+
|
| 91 |
+
results.push({
|
| 92 |
+
title,
|
| 93 |
+
magnetLink: magnetMatch[1],
|
| 94 |
+
quality,
|
| 95 |
+
size,
|
| 96 |
+
source: 'BitSearch',
|
| 97 |
+
seeders: seedersMatch ? parseInt(seedersMatch[1]) : 0,
|
| 98 |
+
leechers: leechersMatch ? parseInt(leechersMatch[1]) : 0,
|
| 99 |
+
category: categoryMatch ? categoryMatch[1].trim() : 'Unknown',
|
| 100 |
+
uploadDate: dateMatch ? dateMatch[1].trim() : '',
|
| 101 |
+
mainFileSize: parseSize(size)
|
| 102 |
+
});
|
| 103 |
+
}
|
| 104 |
+
} catch (error) {
|
| 105 |
+
console.error('Error parsing result:', error);
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
return results;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
function extractQuality(title) {
|
| 113 |
+
if (!title) return '';
|
| 114 |
+
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 115 |
+
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
function isCorrectEpisode(title, searchQuery) {
|
| 119 |
+
if (!title || !searchQuery) return false;
|
| 120 |
+
|
| 121 |
+
const queryMatch = searchQuery.match(/S(\d{2})E(\d{2})/i);
|
| 122 |
+
if (!queryMatch) return false;
|
| 123 |
+
|
| 124 |
+
const querySeasonNum = parseInt(queryMatch[1]);
|
| 125 |
+
const queryEpisodeNum = parseInt(queryMatch[2]);
|
| 126 |
+
|
| 127 |
+
const patterns = [
|
| 128 |
+
/S(\d{2})E(\d{2})/i,
|
| 129 |
+
/(\d{1,2})x(\d{2})/i,
|
| 130 |
+
/[. ](\d{1,2})(\d{2})[. ]/,
|
| 131 |
+
/Season (\d{1,2}) Episode (\d{1,2})/i
|
| 132 |
+
];
|
| 133 |
+
|
| 134 |
+
for (const pattern of patterns) {
|
| 135 |
+
const match = title.match(pattern);
|
| 136 |
+
if (match) {
|
| 137 |
+
const seasonNum = parseInt(match[1]);
|
| 138 |
+
const episodeNum = parseInt(match[2]);
|
| 139 |
+
if (seasonNum === querySeasonNum && episodeNum === queryEpisodeNum) {
|
| 140 |
+
return true;
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
return false;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
async function searchTorrents(searchQuery, type = 'movie') {
|
| 149 |
+
if (!searchQuery) {
|
| 150 |
+
console.log('No search query provided');
|
| 151 |
+
return [];
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
console.log('\n🔄 Searching BitSearch for:', searchQuery);
|
| 155 |
+
|
| 156 |
+
try {
|
| 157 |
+
let title = searchQuery;
|
| 158 |
+
let year = '';
|
| 159 |
+
let searchTitle = searchQuery;
|
| 160 |
+
|
| 161 |
+
if (type === 'movie' && searchQuery.startsWith('tt')) {
|
| 162 |
+
const metadata = await getCinemetaMetadata(searchQuery);
|
| 163 |
+
if (metadata && metadata.meta) {
|
| 164 |
+
title = metadata.meta.name;
|
| 165 |
+
year = new Date(metadata.meta.released).getFullYear().toString();
|
| 166 |
+
searchTitle = `${title} ${year}`;
|
| 167 |
+
console.log('Searching with title and year:', searchTitle);
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
const formattedQuery = searchTitle.replace(/\s+/g, '+').toLowerCase();
|
| 172 |
+
const url = `${SITE_CONFIG.baseUrl}/search?q=${encodeURIComponent(formattedQuery)}`;
|
| 173 |
+
console.log('Request URL:', url);
|
| 174 |
+
|
| 175 |
+
const response = await fetchWithTimeout(url);
|
| 176 |
+
if (!response.ok) {
|
| 177 |
+
throw new Error(`Search request failed: ${response.status}`);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
const html = await response.text();
|
| 181 |
+
const results = parseSearchResults(html);
|
| 182 |
+
console.log(`Found ${results.length} raw results`);
|
| 183 |
+
|
| 184 |
+
let filteredResults = results;
|
| 185 |
+
|
| 186 |
+
if (type === 'series') {
|
| 187 |
+
filteredResults = results.filter(result =>
|
| 188 |
+
isCorrectEpisode(result.title, searchQuery)
|
| 189 |
+
);
|
| 190 |
+
} else if (type === 'movie' && year) {
|
| 191 |
+
filteredResults = results.filter(result => {
|
| 192 |
+
const yearMatch = result.title.match(/(?:19|20)\d{2}/);
|
| 193 |
+
return yearMatch && yearMatch[0] === year;
|
| 194 |
+
});
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
console.log(`Found ${filteredResults.length} relevant results after filtering`);
|
| 198 |
+
|
| 199 |
+
filteredResults.sort((a, b) => {
|
| 200 |
+
const qualityOrder = { '2160p': 4, '4k': 4, '1080p': 3, '720p': 2 };
|
| 201 |
+
const aQuality = qualityOrder[a.quality] || 0;
|
| 202 |
+
const bQuality = qualityOrder[b.quality] || 0;
|
| 203 |
+
|
| 204 |
+
if (aQuality !== bQuality) {
|
| 205 |
+
return bQuality - aQuality;
|
| 206 |
+
}
|
| 207 |
+
return b.seeders - a.seeders;
|
| 208 |
+
});
|
| 209 |
+
|
| 210 |
+
return filteredResults;
|
| 211 |
+
|
| 212 |
+
} catch (error) {
|
| 213 |
+
console.error('❌ Error searching BitSearch:', error);
|
| 214 |
+
|
| 215 |
+
for (const fallbackUrl of SITE_CONFIG.fallbackUrls) {
|
| 216 |
+
try {
|
| 217 |
+
console.log(`Trying fallback URL: ${fallbackUrl}`);
|
| 218 |
+
SITE_CONFIG.baseUrl = fallbackUrl;
|
| 219 |
+
return await searchTorrents(searchQuery, type);
|
| 220 |
+
} catch (fallbackError) {
|
| 221 |
+
console.error(`Fallback ${fallbackUrl} also failed:`, fallbackError);
|
| 222 |
+
}
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
return [];
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
export { searchTorrents };
|
const.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const ERROR = {
|
| 2 |
+
EXPIRED_API_KEY: 'Expired API key',
|
| 3 |
+
ACCESS_DENIED: 'Access denied',
|
| 4 |
+
TWO_FACTOR_AUTH: 'Two factor authentication required',
|
| 5 |
+
NOT_PREMIUM: 'Premium account required',
|
| 6 |
+
NOT_READY: 'Torrent not ready',
|
| 7 |
+
INVALID_API_KEY: 'Invalid API key'
|
| 8 |
+
};
|
| 9 |
+
|
| 10 |
+
export const VIDEO_EXTENSIONS = [
|
| 11 |
+
'.mp4', '.mkv', '.avi',
|
| 12 |
+
'.mov', '.wmv', '.flv',
|
| 13 |
+
'.webm', '.m4v', '.ts'
|
| 14 |
+
];
|
package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "torrent-api",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "API server for searching torrents with TMDB integration",
|
| 5 |
+
"main": "server.js",
|
| 6 |
+
"type": "module",
|
| 7 |
+
"scripts": {
|
| 8 |
+
"start": "node server.js",
|
| 9 |
+
"dev": "nodemon server.js"
|
| 10 |
+
},
|
| 11 |
+
"dependencies": {
|
| 12 |
+
"express": "^4.18.2",
|
| 13 |
+
"node-fetch": "^3.3.2",
|
| 14 |
+
"read-torrent": "^1.3.1",
|
| 15 |
+
"xml2js": "^0.6.2"
|
| 16 |
+
},
|
| 17 |
+
"devDependencies": {
|
| 18 |
+
"nodemon": "^3.0.2"
|
| 19 |
+
},
|
| 20 |
+
"engines": {
|
| 21 |
+
"node": ">=18.0.0"
|
| 22 |
+
},
|
| 23 |
+
"private": true
|
| 24 |
+
}
|
server.js
ADDED
|
@@ -0,0 +1,671 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express';
|
| 2 |
+
|
| 3 |
+
import { searchTorrents } from './bitsearch.js';
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
|
| 9 |
+
const port = process.env.PORT || 7860;
|
| 10 |
+
|
| 11 |
+
const TMDB_API_KEY = 'f051e7366c6105ad4f9aafe4733d9dae';
|
| 12 |
+
|
| 13 |
+
const TMDB_BASE_URL = 'https://api.themoviedb.org/3';
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
app.use(express.json());
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
function standardizeEpisodeFormat(query) {
|
| 22 |
+
|
| 23 |
+
// Handle 1x01 format
|
| 24 |
+
|
| 25 |
+
const altMatch = query.match(/^(.*?)(\d{1,2})x(\d{1,2})/i);
|
| 26 |
+
|
| 27 |
+
if (altMatch) {
|
| 28 |
+
|
| 29 |
+
const [_, title, season, episode] = altMatch;
|
| 30 |
+
|
| 31 |
+
const paddedSeason = String(season).padStart(2, '0');
|
| 32 |
+
|
| 33 |
+
const paddedEpisode = String(episode).padStart(2, '0');
|
| 34 |
+
|
| 35 |
+
return `${title.trim()} S${paddedSeason}E${paddedEpisode}`;
|
| 36 |
+
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return query;
|
| 40 |
+
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
function normalizeTitle(title) {
|
| 46 |
+
|
| 47 |
+
return title.toLowerCase()
|
| 48 |
+
|
| 49 |
+
.replace(/[^a-z0-9\s]/g, '')
|
| 50 |
+
|
| 51 |
+
.replace(/\s+/g, ' ')
|
| 52 |
+
|
| 53 |
+
.trim();
|
| 54 |
+
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
function isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {
|
| 60 |
+
|
| 61 |
+
if (!torrentTitle || !showTitle) return false;
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
// Remove HTML tags and clean up title
|
| 66 |
+
|
| 67 |
+
torrentTitle = torrentTitle.replace(/<[^>]*>/g, '')
|
| 68 |
+
|
| 69 |
+
.replace(/\[.*?\]/g, '') // Remove bracketed content
|
| 70 |
+
|
| 71 |
+
.replace(/\(.*?\)/g, '') // Remove parenthetical content
|
| 72 |
+
|
| 73 |
+
.replace(/\s+/g, ' ')
|
| 74 |
+
|
| 75 |
+
.trim();
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
// Normalize the titles for comparison
|
| 80 |
+
|
| 81 |
+
const normalizedTorrentTitle = torrentTitle.toLowerCase();
|
| 82 |
+
|
| 83 |
+
const normalizedShowTitle = showTitle.toLowerCase().trim();
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
// Split show title into significant words
|
| 88 |
+
|
| 89 |
+
const showWords = normalizedShowTitle.split(' ')
|
| 90 |
+
|
| 91 |
+
.filter(word => word.length > 2)
|
| 92 |
+
|
| 93 |
+
.filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
const hasAllShowWords = showWords.every(word =>
|
| 98 |
+
|
| 99 |
+
normalizedTorrentTitle.includes(word)
|
| 100 |
+
|
| 101 |
+
);
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if (!hasAllShowWords) return false;
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// Format season and episode numbers
|
| 110 |
+
|
| 111 |
+
const seasonStr = String(seasonNum).padStart(2, '0');
|
| 112 |
+
|
| 113 |
+
const episodeStr = String(episodeNum).padStart(2, '0');
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
// Common episode patterns
|
| 118 |
+
|
| 119 |
+
const patterns = [
|
| 120 |
+
|
| 121 |
+
new RegExp(`s${seasonStr}e${episodeStr}`, 'i'), // S01E01
|
| 122 |
+
|
| 123 |
+
new RegExp(`${seasonNum}x${episodeStr}`, 'i'), // 1x01
|
| 124 |
+
|
| 125 |
+
new RegExp(`[^0-9]${seasonNum}${episodeStr}[^0-9]`, 'i'), // 101
|
| 126 |
+
|
| 127 |
+
new RegExp(`season\\s*${seasonNum}\\s*episode\\s*${episodeNum}`, 'i'), // Season 1 Episode 1
|
| 128 |
+
|
| 129 |
+
new RegExp(`s${seasonStr}\\.?e${episodeStr}`, 'i'), // S01.E01
|
| 130 |
+
|
| 131 |
+
new RegExp(`${seasonStr}${episodeStr}`, 'i'), // 0101
|
| 132 |
+
|
| 133 |
+
new RegExp(`^${seasonNum}${episodeStr}\\s`, 'i'), // 101 at start
|
| 134 |
+
|
| 135 |
+
new RegExp(`\\s${seasonNum}${episodeStr}$`, 'i'), // 101 at end
|
| 136 |
+
|
| 137 |
+
new RegExp(`episode\\s*${episodeNum}`, 'i') // Just episode number if title matches
|
| 138 |
+
|
| 139 |
+
];
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
return patterns.some(pattern => pattern.test(normalizedTorrentTitle));
|
| 144 |
+
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
function isExactMovieMatch(torrentTitle, movieTitle, year) {
|
| 150 |
+
|
| 151 |
+
// Remove HTML tags and clean title
|
| 152 |
+
|
| 153 |
+
torrentTitle = torrentTitle.replace(/<[^>]*>/g, '')
|
| 154 |
+
|
| 155 |
+
.replace(/\[.*?\]/g, '') // Remove bracketed content
|
| 156 |
+
|
| 157 |
+
.replace(/\(.*?\)/g, '') // Remove parenthetical content
|
| 158 |
+
|
| 159 |
+
.replace(/\s+/g, ' ')
|
| 160 |
+
|
| 161 |
+
.trim();
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
// Normalize for comparison
|
| 166 |
+
|
| 167 |
+
const normalizedTorrentTitle = torrentTitle.toLowerCase();
|
| 168 |
+
|
| 169 |
+
const normalizedMovieTitle = movieTitle.toLowerCase();
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
// Split movie title into significant words and check if all are present
|
| 174 |
+
|
| 175 |
+
const movieWords = normalizedMovieTitle.split(' ')
|
| 176 |
+
|
| 177 |
+
.filter(word => word.length > 2)
|
| 178 |
+
|
| 179 |
+
.filter(word => !['the', 'and', 'or', 'in', 'on', 'at', 'to'].includes(word));
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
const hasAllMovieWords = movieWords.every(word =>
|
| 184 |
+
|
| 185 |
+
normalizedTorrentTitle.includes(word)
|
| 186 |
+
|
| 187 |
+
);
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
if (!hasAllMovieWords) return false;
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
// Check year
|
| 196 |
+
|
| 197 |
+
const yearMatch = torrentTitle.match(/(?:19|20)\d{2}/);
|
| 198 |
+
|
| 199 |
+
return yearMatch && yearMatch[0] === year.toString();
|
| 200 |
+
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
async function getTMDBDetails(tmdbId, type = 'movie') {
|
| 206 |
+
|
| 207 |
+
try {
|
| 208 |
+
|
| 209 |
+
const response = await fetch(`${TMDB_BASE_URL}/${type}/${tmdbId}?api_key=${TMDB_API_KEY}&append_to_response=external_ids`);
|
| 210 |
+
|
| 211 |
+
if (!response.ok) throw new Error(`TMDB API error: ${response.status}`);
|
| 212 |
+
|
| 213 |
+
return await response.json();
|
| 214 |
+
|
| 215 |
+
} catch (error) {
|
| 216 |
+
|
| 217 |
+
console.error('TMDB fetch error:', error);
|
| 218 |
+
|
| 219 |
+
return null;
|
| 220 |
+
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
async function getTVShowDetails(tmdbId, seasonNum, episodeNum) {
|
| 228 |
+
|
| 229 |
+
try {
|
| 230 |
+
|
| 231 |
+
// Get basic show info
|
| 232 |
+
|
| 233 |
+
const showResponse = await fetch(
|
| 234 |
+
|
| 235 |
+
`${TMDB_BASE_URL}/tv/${tmdbId}?api_key=${TMDB_API_KEY}&append_to_response=external_ids`
|
| 236 |
+
|
| 237 |
+
);
|
| 238 |
+
|
| 239 |
+
if (!showResponse.ok) throw new Error(`TMDB API error: ${showResponse.status}`);
|
| 240 |
+
|
| 241 |
+
const showData = await showResponse.json();
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
// Get specific episode info
|
| 246 |
+
|
| 247 |
+
const episodeResponse = await fetch(
|
| 248 |
+
|
| 249 |
+
`${TMDB_BASE_URL}/tv/${tmdbId}/season/${seasonNum}/episode/${episodeNum}?api_key=${TMDB_API_KEY}`
|
| 250 |
+
|
| 251 |
+
);
|
| 252 |
+
|
| 253 |
+
if (!episodeResponse.ok) throw new Error(`TMDB API error: ${episodeResponse.status}`);
|
| 254 |
+
|
| 255 |
+
const episodeData = await episodeResponse.json();
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
return {
|
| 260 |
+
|
| 261 |
+
showTitle: showData.name,
|
| 262 |
+
|
| 263 |
+
episodeTitle: episodeData.name,
|
| 264 |
+
|
| 265 |
+
seasonNumber: seasonNum,
|
| 266 |
+
|
| 267 |
+
episodeNumber: episodeNum,
|
| 268 |
+
|
| 269 |
+
airDate: episodeData.air_date,
|
| 270 |
+
|
| 271 |
+
imdbId: showData.external_ids?.imdb_id
|
| 272 |
+
|
| 273 |
+
};
|
| 274 |
+
|
| 275 |
+
} catch (error) {
|
| 276 |
+
|
| 277 |
+
console.error('TMDB fetch error:', error);
|
| 278 |
+
|
| 279 |
+
return null;
|
| 280 |
+
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
async function handleSearch({ query, type }) {
|
| 288 |
+
|
| 289 |
+
if (!query) throw new Error('Missing required parameter: query');
|
| 290 |
+
|
| 291 |
+
if (!['movie', 'series'].includes(type)) throw new Error('Invalid type. Must be either "movie" or "series"');
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
let searchQuery = query;
|
| 296 |
+
|
| 297 |
+
let exactMatchParams = {};
|
| 298 |
+
|
| 299 |
+
let tmdbData = null;
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
if (type === 'series') {
|
| 304 |
+
|
| 305 |
+
// Handle TMDB ID format: "tmdb:12345:1:1" (show_id:season:episode)
|
| 306 |
+
|
| 307 |
+
const tmdbMatch = query.match(/^tmdb:(\d+):(\d+):(\d+)$/);
|
| 308 |
+
|
| 309 |
+
if (tmdbMatch) {
|
| 310 |
+
|
| 311 |
+
const [_, tmdbId, seasonNum, episodeNum] = tmdbMatch;
|
| 312 |
+
|
| 313 |
+
tmdbData = await getTVShowDetails(tmdbId, parseInt(seasonNum), parseInt(episodeNum));
|
| 314 |
+
|
| 315 |
+
if (tmdbData) {
|
| 316 |
+
|
| 317 |
+
exactMatchParams = {
|
| 318 |
+
|
| 319 |
+
title: tmdbData.showTitle,
|
| 320 |
+
|
| 321 |
+
season: parseInt(seasonNum),
|
| 322 |
+
|
| 323 |
+
episode: parseInt(episodeNum)
|
| 324 |
+
|
| 325 |
+
};
|
| 326 |
+
|
| 327 |
+
// Try multiple search patterns
|
| 328 |
+
|
| 329 |
+
const searchPatterns = [
|
| 330 |
+
|
| 331 |
+
`${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`,
|
| 332 |
+
|
| 333 |
+
`${tmdbData.showTitle} ${seasonNum}x${String(episodeNum).padStart(2, '0')}`,
|
| 334 |
+
|
| 335 |
+
`${tmdbData.showTitle} Season ${seasonNum} Episode ${episodeNum}`,
|
| 336 |
+
|
| 337 |
+
tmdbData.showTitle // Try just the show name if specific episode search fails
|
| 338 |
+
|
| 339 |
+
];
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
// Try each search pattern until we get results
|
| 344 |
+
|
| 345 |
+
for (const pattern of searchPatterns) {
|
| 346 |
+
|
| 347 |
+
const results = await searchTorrents(pattern, type);
|
| 348 |
+
|
| 349 |
+
if (results.length > 0) {
|
| 350 |
+
|
| 351 |
+
searchQuery = pattern;
|
| 352 |
+
|
| 353 |
+
break;
|
| 354 |
+
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
if (!searchQuery) {
|
| 360 |
+
|
| 361 |
+
searchQuery = searchPatterns[0]; // Default to first pattern if none worked
|
| 362 |
+
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
// Handle IMDB ID format: "tt1234567:1:1" (imdb_id:season:episode)
|
| 370 |
+
|
| 371 |
+
else if (query.startsWith('tt')) {
|
| 372 |
+
|
| 373 |
+
const imdbMatch = query.match(/^(tt\d+):(\d+):(\d+)$/);
|
| 374 |
+
|
| 375 |
+
if (imdbMatch) {
|
| 376 |
+
|
| 377 |
+
const [_, imdbId, seasonNum, episodeNum] = imdbMatch;
|
| 378 |
+
|
| 379 |
+
// First get TMDB ID from IMDB ID
|
| 380 |
+
|
| 381 |
+
const findResponse = await fetch(
|
| 382 |
+
|
| 383 |
+
`${TMDB_BASE_URL}/find/${imdbId}?api_key=${TMDB_API_KEY}&external_source=imdb_id`
|
| 384 |
+
|
| 385 |
+
);
|
| 386 |
+
|
| 387 |
+
if (findResponse.ok) {
|
| 388 |
+
|
| 389 |
+
const findData = await findResponse.json();
|
| 390 |
+
|
| 391 |
+
if (findData.tv_results && findData.tv_results[0]) {
|
| 392 |
+
|
| 393 |
+
const tmdbId = findData.tv_results[0].id;
|
| 394 |
+
|
| 395 |
+
tmdbData = await getTVShowDetails(tmdbId, parseInt(seasonNum), parseInt(episodeNum));
|
| 396 |
+
|
| 397 |
+
if (tmdbData) {
|
| 398 |
+
|
| 399 |
+
exactMatchParams = {
|
| 400 |
+
|
| 401 |
+
title: tmdbData.showTitle,
|
| 402 |
+
|
| 403 |
+
season: parseInt(seasonNum),
|
| 404 |
+
|
| 405 |
+
episode: parseInt(episodeNum)
|
| 406 |
+
|
| 407 |
+
};
|
| 408 |
+
|
| 409 |
+
searchQuery = `${tmdbData.showTitle} S${String(seasonNum).padStart(2, '0')}E${String(episodeNum).padStart(2, '0')}`;
|
| 410 |
+
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
// Handle text formats
|
| 422 |
+
|
| 423 |
+
else {
|
| 424 |
+
|
| 425 |
+
// Convert 1x01 format to S01E01
|
| 426 |
+
|
| 427 |
+
searchQuery = standardizeEpisodeFormat(query);
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
// Try to extract season/episode info
|
| 432 |
+
|
| 433 |
+
const seasonEpisodeMatch = searchQuery.match(/^(.*?)S(\d{1,2})E(\d{1,2})/i);
|
| 434 |
+
|
| 435 |
+
if (seasonEpisodeMatch) {
|
| 436 |
+
|
| 437 |
+
exactMatchParams = {
|
| 438 |
+
|
| 439 |
+
title: seasonEpisodeMatch[1].trim(),
|
| 440 |
+
|
| 441 |
+
season: parseInt(seasonEpisodeMatch[2]),
|
| 442 |
+
|
| 443 |
+
episode: parseInt(seasonEpisodeMatch[3])
|
| 444 |
+
|
| 445 |
+
};
|
| 446 |
+
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
} else if (type === 'movie') {
|
| 452 |
+
|
| 453 |
+
// Handle TMDB ID format
|
| 454 |
+
|
| 455 |
+
if (query.startsWith('tmdb:')) {
|
| 456 |
+
|
| 457 |
+
const tmdbId = query.replace('tmdb:', '');
|
| 458 |
+
|
| 459 |
+
tmdbData = await getTMDBDetails(tmdbId, 'movie');
|
| 460 |
+
|
| 461 |
+
if (tmdbData) {
|
| 462 |
+
|
| 463 |
+
exactMatchParams = {
|
| 464 |
+
|
| 465 |
+
title: tmdbData.title,
|
| 466 |
+
|
| 467 |
+
year: new Date(tmdbData.release_date).getFullYear()
|
| 468 |
+
|
| 469 |
+
};
|
| 470 |
+
|
| 471 |
+
searchQuery = `${tmdbData.title} ${exactMatchParams.year}`;
|
| 472 |
+
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
// Handle IMDB ID format
|
| 478 |
+
|
| 479 |
+
else if (query.startsWith('tt')) {
|
| 480 |
+
|
| 481 |
+
const findResponse = await fetch(
|
| 482 |
+
|
| 483 |
+
`${TMDB_BASE_URL}/find/${query}?api_key=${TMDB_API_KEY}&external_source=imdb_id`
|
| 484 |
+
|
| 485 |
+
);
|
| 486 |
+
|
| 487 |
+
if (findResponse.ok) {
|
| 488 |
+
|
| 489 |
+
const findData = await findResponse.json();
|
| 490 |
+
|
| 491 |
+
if (findData.movie_results && findData.movie_results[0]) {
|
| 492 |
+
|
| 493 |
+
tmdbData = findData.movie_results[0];
|
| 494 |
+
|
| 495 |
+
exactMatchParams = {
|
| 496 |
+
|
| 497 |
+
title: tmdbData.title,
|
| 498 |
+
|
| 499 |
+
year: new Date(tmdbData.release_date).getFullYear()
|
| 500 |
+
|
| 501 |
+
};
|
| 502 |
+
|
| 503 |
+
searchQuery = `${tmdbData.title} ${exactMatchParams.year}`;
|
| 504 |
+
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
// Handle text format with year
|
| 512 |
+
|
| 513 |
+
else {
|
| 514 |
+
|
| 515 |
+
const movieYearMatch = query.match(/^(.+?)(?:\s+|$)(\d{4})$/);
|
| 516 |
+
|
| 517 |
+
if (movieYearMatch) {
|
| 518 |
+
|
| 519 |
+
exactMatchParams = {
|
| 520 |
+
|
| 521 |
+
title: movieYearMatch[1].trim(),
|
| 522 |
+
|
| 523 |
+
year: parseInt(movieYearMatch[2])
|
| 524 |
+
|
| 525 |
+
};
|
| 526 |
+
|
| 527 |
+
}
|
| 528 |
+
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
let results = await searchTorrents(searchQuery, type);
|
| 536 |
+
|
| 537 |
+
|
| 538 |
+
|
| 539 |
+
// Filter results based on exact match parameters
|
| 540 |
+
|
| 541 |
+
if (type === 'series' && exactMatchParams.season !== undefined) {
|
| 542 |
+
|
| 543 |
+
results = results.filter(result =>
|
| 544 |
+
|
| 545 |
+
isExactEpisodeMatch(
|
| 546 |
+
|
| 547 |
+
result.title || result.websiteTitle,
|
| 548 |
+
|
| 549 |
+
exactMatchParams.title,
|
| 550 |
+
|
| 551 |
+
exactMatchParams.season,
|
| 552 |
+
|
| 553 |
+
exactMatchParams.episode
|
| 554 |
+
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
);
|
| 558 |
+
|
| 559 |
+
} else if (type === 'movie' && exactMatchParams.year) {
|
| 560 |
+
|
| 561 |
+
results = results.filter(result =>
|
| 562 |
+
|
| 563 |
+
isExactMovieMatch(
|
| 564 |
+
|
| 565 |
+
result.title || result.websiteTitle,
|
| 566 |
+
|
| 567 |
+
exactMatchParams.title,
|
| 568 |
+
|
| 569 |
+
exactMatchParams.year
|
| 570 |
+
|
| 571 |
+
)
|
| 572 |
+
|
| 573 |
+
);
|
| 574 |
+
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
// Sort results
|
| 580 |
+
|
| 581 |
+
results.sort((a, b) => {
|
| 582 |
+
|
| 583 |
+
const qualityOrder = { '2160p': 4, '4k': 4, '1080p': 3, '720p': 2 };
|
| 584 |
+
|
| 585 |
+
return (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0) || b.seeders - a.seeders;
|
| 586 |
+
|
| 587 |
+
});
|
| 588 |
+
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
return {
|
| 592 |
+
|
| 593 |
+
query: searchQuery,
|
| 594 |
+
|
| 595 |
+
originalQuery: query,
|
| 596 |
+
|
| 597 |
+
type,
|
| 598 |
+
|
| 599 |
+
tmdbData,
|
| 600 |
+
|
| 601 |
+
results: results.map(result => ({
|
| 602 |
+
|
| 603 |
+
title: result.title || result.websiteTitle,
|
| 604 |
+
|
| 605 |
+
filename: result.filename,
|
| 606 |
+
|
| 607 |
+
quality: result.quality,
|
| 608 |
+
|
| 609 |
+
size: result.size,
|
| 610 |
+
|
| 611 |
+
seeders: result.seeders,
|
| 612 |
+
|
| 613 |
+
leechers: result.leechers,
|
| 614 |
+
|
| 615 |
+
magnetLink: result.magnetLink
|
| 616 |
+
|
| 617 |
+
}))
|
| 618 |
+
|
| 619 |
+
};
|
| 620 |
+
|
| 621 |
+
}
|
| 622 |
+
|
| 623 |
+
|
| 624 |
+
|
| 625 |
+
app.get('/api/search', async (req, res) => {
|
| 626 |
+
|
| 627 |
+
try {
|
| 628 |
+
|
| 629 |
+
const result = await handleSearch({ query: req.query.query, type: req.query.type });
|
| 630 |
+
|
| 631 |
+
res.json(result);
|
| 632 |
+
|
| 633 |
+
} catch (error) {
|
| 634 |
+
|
| 635 |
+
res.status(500).json({ error: 'Internal server error', message: error.message });
|
| 636 |
+
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
});
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
|
| 643 |
+
app.post('/api/search', async (req, res) => {
|
| 644 |
+
|
| 645 |
+
try {
|
| 646 |
+
|
| 647 |
+
const result = await handleSearch(req.body);
|
| 648 |
+
|
| 649 |
+
res.json(result);
|
| 650 |
+
|
| 651 |
+
} catch (error) {
|
| 652 |
+
|
| 653 |
+
res.status(500).json({ error: 'Internal server error', message: error.message });
|
| 654 |
+
|
| 655 |
+
}
|
| 656 |
+
|
| 657 |
+
});
|
| 658 |
+
|
| 659 |
+
|
| 660 |
+
|
| 661 |
+
app.listen(port, () => {
|
| 662 |
+
|
| 663 |
+
console.log(`Server running at http://localhost:${port}`);
|
| 664 |
+
|
| 665 |
+
if (!TMDB_API_KEY) {
|
| 666 |
+
|
| 667 |
+
console.warn('Warning: TMDB_API_KEY not set. TMDB features will not work.');
|
| 668 |
+
|
| 669 |
+
}
|
| 670 |
+
|
| 671 |
+
});
|