Update server.js
Browse files
server.js
CHANGED
|
@@ -1,149 +1,5 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
import { fetchRSSFeeds } from './anime.js';
|
| 3 |
-
import fetch from 'node-fetch';
|
| 4 |
-
import { JSDOM } from 'jsdom';
|
| 5 |
-
|
| 6 |
-
// Function to extract video quality from the title
|
| 7 |
-
function extractQuality(title) {
|
| 8 |
-
if (!title) return '';
|
| 9 |
-
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 10 |
-
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
// Function to extract hash from BTSOW detail URLs
|
| 14 |
-
function extractHash(detailUrl) {
|
| 15 |
-
const hashMatch = detailUrl.match(/hash\/([A-Fa-f0-9]{40})/);
|
| 16 |
-
return hashMatch ? hashMatch[1] : '';
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
// Function to generate a direct magnet link from a hash
|
| 20 |
-
function createMagnetLink(hash) {
|
| 21 |
-
return `magnet:?xt=urn:btih:${hash}`;
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
// Function to extract magnet links from a BTSOW detail page
|
| 25 |
-
async function extractMagnetLinksFromPage(url) {
|
| 26 |
-
try {
|
| 27 |
-
await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents rate-limiting
|
| 28 |
-
|
| 29 |
-
const response = await fetch(url, {
|
| 30 |
-
headers: {
|
| 31 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
|
| 32 |
-
'Referer': 'https://btsow.motorcycles/',
|
| 33 |
-
'Accept-Language': 'en-US,en;q=0.9',
|
| 34 |
-
'Connection': 'keep-alive',
|
| 35 |
-
'DNT': '1',
|
| 36 |
-
'Upgrade-Insecure-Requests': '1'
|
| 37 |
-
}
|
| 38 |
-
});
|
| 39 |
-
|
| 40 |
-
if (!response.ok) {
|
| 41 |
-
console.error(`❌ Failed to fetch page (${response.status}):`, url);
|
| 42 |
-
return null;
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
const html = await response.text();
|
| 46 |
-
const dom = new JSDOM(html);
|
| 47 |
-
const magnetLinks = dom.window.document.querySelectorAll('a[href^="magnet:?xt=urn:btih:"]');
|
| 48 |
-
|
| 49 |
-
return Array.from(magnetLinks).map(link => link.href);
|
| 50 |
-
} catch (error) {
|
| 51 |
-
console.error('❌ Error fetching magnet links:', error);
|
| 52 |
-
return null;
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
// Function to fetch and parse BTSOW search results
|
| 57 |
-
async function fetchRSSFeeds(searchQuery) {
|
| 58 |
-
console.log(`🔄 Searching BTSOW for: "${searchQuery}"`);
|
| 59 |
-
|
| 60 |
-
try {
|
| 61 |
-
await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents bot detection
|
| 62 |
-
|
| 63 |
-
const url = `https://btsow.motorcycles/search/${encodeURIComponent(searchQuery)}`;
|
| 64 |
-
const response = await fetch(url, {
|
| 65 |
-
headers: {
|
| 66 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
|
| 67 |
-
'Referer': 'https://btsow.motorcycles/',
|
| 68 |
-
'Accept-Language': 'en-US,en;q=0.9',
|
| 69 |
-
'Connection': 'keep-alive',
|
| 70 |
-
'DNT': '1',
|
| 71 |
-
'Upgrade-Insecure-Requests': '1'
|
| 72 |
-
}
|
| 73 |
-
});
|
| 74 |
-
|
| 75 |
-
if (!response.ok) {
|
| 76 |
-
throw new Error(`Failed to fetch BTSOW search page: ${response.status}`);
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
const html = await response.text();
|
| 80 |
-
const dom = new JSDOM(html);
|
| 81 |
-
const torrents = [];
|
| 82 |
-
|
| 83 |
-
dom.window.document.querySelectorAll('.data-list .row').forEach(row => {
|
| 84 |
-
const magnetLinkElement = row.querySelector('a[href^="//btsow.motorcycles/magnet/detail/hash/"]');
|
| 85 |
-
const titleElement = row.querySelector('.file');
|
| 86 |
-
const metaElement = row.querySelector('.size-date');
|
| 87 |
-
|
| 88 |
-
if (!magnetLinkElement || !titleElement || !metaElement) return;
|
| 89 |
-
|
| 90 |
-
const detailUrl = 'https:' + magnetLinkElement.getAttribute('href');
|
| 91 |
-
const title = titleElement.textContent.trim();
|
| 92 |
-
const quality = extractQuality(title);
|
| 93 |
-
|
| 94 |
-
// Extract hash to generate direct magnet link
|
| 95 |
-
const hash = extractHash(detailUrl);
|
| 96 |
-
const directMagnet = hash ? createMagnetLink(hash) : '';
|
| 97 |
-
|
| 98 |
-
// Extract meta information (size & date)
|
| 99 |
-
const metaText = metaElement.textContent.trim();
|
| 100 |
-
const sizeMatch = metaText.match(/Size:([^/]+)/);
|
| 101 |
-
const dateMatch = metaText.match(/Convert Date:(.+)/);
|
| 102 |
-
|
| 103 |
-
const size = sizeMatch ? sizeMatch[1].trim() : 'Unknown';
|
| 104 |
-
const date = dateMatch ? dateMatch[1].trim() : 'Unknown';
|
| 105 |
-
|
| 106 |
-
torrents.push({
|
| 107 |
-
title,
|
| 108 |
-
detailUrl,
|
| 109 |
-
hash,
|
| 110 |
-
size,
|
| 111 |
-
date,
|
| 112 |
-
quality,
|
| 113 |
-
magnetLinks: directMagnet ? [directMagnet] : [],
|
| 114 |
-
});
|
| 115 |
-
});
|
| 116 |
-
|
| 117 |
-
// Fetch additional magnet links
|
| 118 |
-
await Promise.all(torrents.map(async (torrent) => {
|
| 119 |
-
if (!torrent.magnetLinks.length) {
|
| 120 |
-
const magnetLinks = await extractMagnetLinksFromPage(torrent.detailUrl);
|
| 121 |
-
if (magnetLinks) {
|
| 122 |
-
torrent.magnetLinks.push(...magnetLinks);
|
| 123 |
-
}
|
| 124 |
-
}
|
| 125 |
-
}));
|
| 126 |
-
|
| 127 |
-
// Filter out torrents with no magnet links
|
| 128 |
-
const validTorrents = torrents.filter(torrent => torrent.magnetLinks.length > 0);
|
| 129 |
-
|
| 130 |
-
// Sort by quality
|
| 131 |
-
validTorrents.sort((a, b) => {
|
| 132 |
-
const qualityOrder = { '4k': 1, '2160p': 2, '1080p': 3, '720p': 4, '': 5 };
|
| 133 |
-
return (qualityOrder[a.quality] || 5) - (qualityOrder[b.quality] || 5);
|
| 134 |
-
});
|
| 135 |
-
|
| 136 |
-
console.log(`✅ Found ${validTorrents.length} torrents.`);
|
| 137 |
-
return validTorrents;
|
| 138 |
-
|
| 139 |
-
} catch (error) {
|
| 140 |
-
console.error('❌ Error fetching from BTSOW:', error);
|
| 141 |
-
return [];
|
| 142 |
-
}
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
-
// Export fetchRSSFeeds as the single function
|
| 146 |
-
export { fetchRSSFeeds };
|
| 147 |
|
| 148 |
const app = express();
|
| 149 |
const port = process.env.PORT || 7860;
|
|
@@ -164,70 +20,57 @@ function standardizeEpisodeFormat(query) {
|
|
| 164 |
return query;
|
| 165 |
}
|
| 166 |
|
| 167 |
-
function isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
// 🔹 Ensure input is a string before using replace()
|
| 173 |
-
torrentTitle = String(torrentTitle);
|
| 174 |
-
showTitle = String(showTitle);
|
| 175 |
|
| 176 |
-
//
|
| 177 |
-
const cleanTorrentTitle = torrentTitle.replace(/[^\w\s]/g, '').toLowerCase();
|
| 178 |
-
const cleanShowTitle = showTitle.replace(/[^\w\s]/g, '').toLowerCase();
|
| 179 |
|
| 180 |
-
//
|
| 181 |
-
const showWords = cleanShowTitle.split(' ')
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
//
|
| 185 |
-
if (!showWords.every(word => cleanTorrentTitle.includes(word))) {
|
| 186 |
-
return false;
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
// 🔹 Normalize season & episode numbers
|
| 190 |
const seasonStr = seasonNum.toString().padStart(2, '0');
|
| 191 |
const episodeStr = episodeNum.toString().padStart(2, '0');
|
| 192 |
|
| 193 |
-
//
|
| 194 |
const patterns = [
|
| 195 |
-
new RegExp(`s${seasonStr}e${episodeStr}`, 'i'),
|
| 196 |
new RegExp(`\\b${seasonNum}x${episodeNum}\\b`, 'i'), // 1x05
|
| 197 |
new RegExp(`\\b${seasonStr}${episodeStr}\\b`, 'i'), // 0105
|
| 198 |
new RegExp(`season\\s*${seasonNum}\\s*episode\\s*${episodeNum}`, 'i') // Season 1 Episode 5
|
| 199 |
];
|
| 200 |
|
|
|
|
| 201 |
return patterns.some(pattern => pattern.test(cleanTorrentTitle));
|
| 202 |
}
|
| 203 |
|
| 204 |
function isExactMovieMatch(torrentTitle, movieTitle, year) {
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
//
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
//
|
| 213 |
-
const
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
if
|
| 222 |
-
|
| 223 |
-
}
|
| 224 |
-
|
| 225 |
-
// 🔹 Check for the release year in torrent title
|
| 226 |
-
const yearMatch = torrentTitle.match(/(?:19|20)\d{2}/);
|
| 227 |
-
return yearMatch ? yearMatch[0] === year.toString() : false;
|
| 228 |
}
|
| 229 |
|
| 230 |
-
|
| 231 |
async function getTMDBDetails(tmdbId, type = 'movie') {
|
| 232 |
try {
|
| 233 |
const response = await fetch(`${TMDB_BASE_URL}/${type}/${tmdbId}?api_key=${TMDB_API_KEY}&append_to_response=external_ids`);
|
|
@@ -336,9 +179,9 @@ async function handleSearch({ query, type }) {
|
|
| 336 |
if (tmdbData) {
|
| 337 |
exactMatchParams = {
|
| 338 |
title: tmdbData.title,
|
| 339 |
-
year: new Date(tmdbData.release_date).getFullYear()
|
| 340 |
};
|
| 341 |
-
searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;
|
| 342 |
}
|
| 343 |
}
|
| 344 |
// Handle IMDB ID format
|
|
@@ -352,9 +195,9 @@ async function handleSearch({ query, type }) {
|
|
| 352 |
tmdbData = findData.movie_results[0];
|
| 353 |
exactMatchParams = {
|
| 354 |
title: tmdbData.title,
|
| 355 |
-
year: new Date(tmdbData.release_date).getFullYear()
|
| 356 |
};
|
| 357 |
-
searchQuery = `${tmdbData.title} (${exactMatchParams.year})`;
|
| 358 |
}
|
| 359 |
}
|
| 360 |
}
|
|
@@ -369,25 +212,17 @@ async function handleSearch({ query, type }) {
|
|
| 369 |
}
|
| 370 |
}
|
| 371 |
|
| 372 |
-
|
|
|
|
| 373 |
|
| 374 |
-
//
|
| 375 |
if (type === 'series' && exactMatchParams.season !== undefined) {
|
| 376 |
-
results = results.filter(result =>
|
| 377 |
-
isExactEpisodeMatch(
|
| 378 |
-
result.websiteTitle,
|
| 379 |
-
exactMatchParams.title,
|
| 380 |
-
exactMatchParams.season,
|
| 381 |
-
exactMatchParams.episode
|
| 382 |
-
)
|
| 383 |
);
|
| 384 |
} else if (type === 'movie' && exactMatchParams.year) {
|
| 385 |
results = results.filter(result =>
|
| 386 |
-
isExactMovieMatch(
|
| 387 |
-
result.websiteTitle,
|
| 388 |
-
exactMatchParams.title,
|
| 389 |
-
exactMatchParams.year
|
| 390 |
-
)
|
| 391 |
);
|
| 392 |
}
|
| 393 |
|
|
@@ -397,7 +232,7 @@ async function handleSearch({ query, type }) {
|
|
| 397 |
type,
|
| 398 |
tmdbData,
|
| 399 |
results: results.map(result => ({
|
| 400 |
-
title: result.
|
| 401 |
quality: result.quality,
|
| 402 |
size: result.size,
|
| 403 |
seeders: 0, // Knaben RSS doesn't provide seeder info
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
import { fetchRSSFeeds } from './anime.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
const port = process.env.PORT || 7860;
|
|
|
|
| 20 |
return query;
|
| 21 |
}
|
| 22 |
|
| 23 |
+
function isExactEpisodeMatch(torrentTitle, showTitle, seasonNum, episodeNum) {
|
| 24 |
+
// Early return if any of the inputs are missing
|
| 25 |
+
if (!torrentTitle || !showTitle || seasonNum == null || episodeNum == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
// Clean up and normalize titles
|
| 28 |
+
const cleanTorrentTitle = String(torrentTitle).replace(/[^\w\s]/g, '').toLowerCase();
|
| 29 |
+
const cleanShowTitle = String(showTitle).replace(/[^\w\s]/g, '').toLowerCase();
|
| 30 |
|
| 31 |
+
// Split the show title into significant words
|
| 32 |
+
const showWords = cleanShowTitle.split(' ').filter(word => word.length > 2);
|
| 33 |
+
|
| 34 |
+
// Check if all significant words in the show title are present in the cleaned torrent title
|
| 35 |
+
if (!showWords.every(word => cleanTorrentTitle.includes(word))) return false;
|
| 36 |
|
| 37 |
+
// Format season and episode numbers with leading zeros
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
const seasonStr = seasonNum.toString().padStart(2, '0');
|
| 39 |
const episodeStr = episodeNum.toString().padStart(2, '0');
|
| 40 |
|
| 41 |
+
// ✅ FIX: Add support for different episode formats
|
| 42 |
const patterns = [
|
| 43 |
+
new RegExp(`s${seasonStr}e${episodeStr}`, 'i'), // S01E05
|
| 44 |
new RegExp(`\\b${seasonNum}x${episodeNum}\\b`, 'i'), // 1x05
|
| 45 |
new RegExp(`\\b${seasonStr}${episodeStr}\\b`, 'i'), // 0105
|
| 46 |
new RegExp(`season\\s*${seasonNum}\\s*episode\\s*${episodeNum}`, 'i') // Season 1 Episode 5
|
| 47 |
];
|
| 48 |
|
| 49 |
+
// Check if any of the episode patterns match the cleaned torrent title
|
| 50 |
return patterns.some(pattern => pattern.test(cleanTorrentTitle));
|
| 51 |
}
|
| 52 |
|
| 53 |
function isExactMovieMatch(torrentTitle, movieTitle, year) {
|
| 54 |
+
// Early return if any input is missing
|
| 55 |
+
if (!torrentTitle || !movieTitle || year == null) return false;
|
| 56 |
+
|
| 57 |
+
// Clean and normalize the titles
|
| 58 |
+
const cleanTorrentTitle = String(torrentTitle).replace(/[^\w\s]/g, '').toLowerCase();
|
| 59 |
+
const cleanMovieTitle = String(movieTitle).replace(/[^\w\s]/g, '').toLowerCase();
|
| 60 |
+
|
| 61 |
+
// Split the movie title into words and filter out short words
|
| 62 |
+
const movieWords = cleanMovieTitle.split(' ').filter(word => word.length > 2);
|
| 63 |
+
|
| 64 |
+
// Check if all words in the movie title are present in the cleaned torrent title
|
| 65 |
+
if (!movieWords.every(word => cleanTorrentTitle.includes(word))) return false;
|
| 66 |
+
|
| 67 |
+
// ✅ FIX: Improve year matching, allowing variations
|
| 68 |
+
const yearMatch = torrentTitle.match(/\b(19\d{2}|20\d{2})\b/);
|
| 69 |
+
|
| 70 |
+
// Check if the year in the title matches the provided year
|
| 71 |
+
return yearMatch ? parseInt(yearMatch[0]) === year : false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
|
|
|
| 74 |
async function getTMDBDetails(tmdbId, type = 'movie') {
|
| 75 |
try {
|
| 76 |
const response = await fetch(`${TMDB_BASE_URL}/${type}/${tmdbId}?api_key=${TMDB_API_KEY}&append_to_response=external_ids`);
|
|
|
|
| 179 |
if (tmdbData) {
|
| 180 |
exactMatchParams = {
|
| 181 |
title: tmdbData.title,
|
| 182 |
+
year: tmdbData.release_date ? new Date(tmdbData.release_date).getFullYear() : null
|
| 183 |
};
|
| 184 |
+
searchQuery = exactMatchParams.year ? `${tmdbData.title} (${exactMatchParams.year})` : tmdbData.title;
|
| 185 |
}
|
| 186 |
}
|
| 187 |
// Handle IMDB ID format
|
|
|
|
| 195 |
tmdbData = findData.movie_results[0];
|
| 196 |
exactMatchParams = {
|
| 197 |
title: tmdbData.title,
|
| 198 |
+
year: tmdbData.release_date ? new Date(tmdbData.release_date).getFullYear() : null
|
| 199 |
};
|
| 200 |
+
searchQuery = exactMatchParams.year ? `${tmdbData.title} (${exactMatchParams.year})` : tmdbData.title;
|
| 201 |
}
|
| 202 |
}
|
| 203 |
}
|
|
|
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
+
// ✅ FIX: `fetchRSSFeeds()` should be called without `type` parameter
|
| 216 |
+
let results = await fetchRSSFeeds(searchQuery);
|
| 217 |
|
| 218 |
+
// ✅ FIX: Ensure filtering uses `result.title` instead of `result.websiteTitle`
|
| 219 |
if (type === 'series' && exactMatchParams.season !== undefined) {
|
| 220 |
+
results = results.filter(result =>
|
| 221 |
+
isExactEpisodeMatch(result.title, exactMatchParams.title, exactMatchParams.season, exactMatchParams.episode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
);
|
| 223 |
} else if (type === 'movie' && exactMatchParams.year) {
|
| 224 |
results = results.filter(result =>
|
| 225 |
+
isExactMovieMatch(result.title, exactMatchParams.title, exactMatchParams.year)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
);
|
| 227 |
}
|
| 228 |
|
|
|
|
| 232 |
type,
|
| 233 |
tmdbData,
|
| 234 |
results: results.map(result => ({
|
| 235 |
+
title: result.title,
|
| 236 |
quality: result.quality,
|
| 237 |
size: result.size,
|
| 238 |
seeders: 0, // Knaben RSS doesn't provide seeder info
|