hankchin commited on
Commit
9e50bb6
Β·
verified Β·
1 Parent(s): c371e3c

Update bitsearch.js

Browse files
Files changed (1) hide show
  1. bitsearch.js +152 -9
bitsearch.js CHANGED
@@ -1,10 +1,73 @@
1
  import fetch from 'node-fetch';
2
  import { JSDOM } from 'jsdom';
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  // Function to extract video quality from the title
5
  function extractQuality(title) {
6
  if (!title) return '';
7
- const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
8
  return qualityMatch ? qualityMatch[1].toLowerCase() : '';
9
  }
10
 
@@ -19,6 +82,30 @@ function createMagnetLink(hash) {
19
  return `magnet:?xt=urn:btih:${hash}`;
20
  }
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  // βœ… Function to extract magnet links from torrentz9 detail pages
23
  async function extractMagnetLinksFromPage(url) {
24
  try {
@@ -55,14 +142,62 @@ async function extractMagnetLinksFromPage(url) {
55
  }
56
  }
57
 
58
- // βœ… Main function to search torrentz9 and extract torrent data
59
- async function fetchRSSFeeds(searchQuery) {
60
- console.log(`πŸ”„ Searching Torrentz9 for: "${searchQuery}"`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  try {
63
  await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents bot detection
64
 
65
- const url = `https://torrentz9.org/search?keyword=${encodeURIComponent(searchQuery)}`;
66
  const response = await fetch(url, {
67
  headers: {
68
  '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',
@@ -125,11 +260,19 @@ async function fetchRSSFeeds(searchQuery) {
125
  }
126
  }));
127
 
128
- // βœ… Ensure only torrents with magnet links are returned
129
- const validTorrents = torrents.filter(torrent => torrent.magnetLink);
 
 
 
 
 
 
 
 
130
 
131
- console.log(`βœ… Found ${validTorrents.length} torrents with magnet links.`);
132
- return validTorrents;
133
 
134
  } catch (error) {
135
  console.error('❌ Error fetching from Torrentz9:', error);
 
1
  import fetch from 'node-fetch';
2
  import { JSDOM } from 'jsdom';
3
 
4
+ // πŸš€ UNIVERSAL FIX: IMDb Detection
5
+ function isImdbSearch(query) {
6
+ return /^tt\d+$/i.test(query?.toString().trim());
7
+ }
8
+
9
+ // πŸš€ UNIVERSAL FIX: Content Type Detection
10
+ function detectContentType(item) {
11
+ let type = 'unknown';
12
+
13
+ const title = (item.title || '').toLowerCase();
14
+
15
+ // Check for clear series indicators
16
+ const seriesPatterns = [
17
+ /\bs\d+e\d+\b/, // S01E01
18
+ /\bseason\s+\d+/, // Season 1
19
+ /\bepisode\s+\d+/, // Episode 1
20
+ /\d+x\d+/, // 1x01
21
+ /\b(serial|series|tv|show)\b/ // Serial, series, TV, show
22
+ ];
23
+
24
+ if (seriesPatterns.some(pattern => pattern.test(title))) {
25
+ type = 'series';
26
+ }
27
+
28
+ // Check for clear movie indicators
29
+ const moviePatterns = [
30
+ /\b(movie|film|cinema)\b/,
31
+ /\b\d{4}\b.*\b(bluray|webrip|dvdrip|hdcam|remux)\b/
32
+ ];
33
+
34
+ if (moviePatterns.some(pattern => pattern.test(title))) {
35
+ type = 'movie';
36
+ }
37
+
38
+ return type;
39
+ }
40
+
41
+ // πŸš€ UNIVERSAL FIX: Filtering Logic
42
+ function shouldIncludeResult(item, requestedType, originalQuery) {
43
+ // βœ… CRITICAL FIX: For IMDb searches, be VERY permissive
44
+ if (isImdbSearch(originalQuery)) {
45
+ console.log(`🎯 IMDb search detected - including ALL results for: ${originalQuery}`);
46
+ return true; // βœ… ALWAYS include results for IMDb searches
47
+ }
48
+
49
+ // For text searches, apply loose filtering
50
+ const detectedType = detectContentType(item);
51
+
52
+ // βœ… Be more permissive even for text searches
53
+ if (requestedType === 'movie') {
54
+ // Include if it's a movie OR if we can't determine type
55
+ return detectedType === 'movie' || detectedType === 'unknown';
56
+ }
57
+
58
+ if (requestedType === 'series') {
59
+ // Include if it's a series OR if we can't determine type
60
+ return detectedType === 'series' || detectedType === 'unknown';
61
+ }
62
+
63
+ // Default: include everything
64
+ return true;
65
+ }
66
+
67
  // Function to extract video quality from the title
68
  function extractQuality(title) {
69
  if (!title) return '';
70
+ const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd|webrip|bluray|remux)\b/i);
71
  return qualityMatch ? qualityMatch[1].toLowerCase() : '';
72
  }
73
 
 
82
  return `magnet:?xt=urn:btih:${hash}`;
83
  }
84
 
85
+ // Function to extract info hash from magnet link
86
+ function extractInfoHash(magnetLink) {
87
+ if (!magnetLink) return null;
88
+ const match = magnetLink.match(/btih:([a-fA-F0-9]+)/);
89
+ return match ? match[1] : null;
90
+ }
91
+
92
+ // Function to format size consistently
93
+ function formatSize(sizeStr) {
94
+ if (!sizeStr) return '0 GB';
95
+ // If it's already formatted, return as is
96
+ if (sizeStr.includes('GB') || sizeStr.includes('MB')) return sizeStr;
97
+ // Otherwise assume it's in bytes and convert
98
+ const bytes = parseInt(sizeStr);
99
+ if (isNaN(bytes)) return sizeStr;
100
+ const gb = bytes / Math.pow(1024, 3);
101
+ return `${gb.toFixed(2)} GB`;
102
+ }
103
+
104
+ // Function to clean query
105
+ function cleanQuery(query) {
106
+ return query?.toString().trim() || '';
107
+ }
108
+
109
  // βœ… Function to extract magnet links from torrentz9 detail pages
110
  async function extractMagnetLinksFromPage(url) {
111
  try {
 
142
  }
143
  }
144
 
145
+ // πŸš€ UNIVERSAL FIX: Results Processing
146
+ function processResults(rawResults, requestedType, originalQuery) {
147
+ if (!Array.isArray(rawResults)) {
148
+ console.log('❌ No valid results array found');
149
+ return [];
150
+ }
151
+
152
+ console.log(`πŸ“Š Processing ${rawResults.length} raw results for query: ${originalQuery}`);
153
+
154
+ const processedResults = rawResults
155
+ .map((item, index) => {
156
+ if (!item || !item.magnetLink || !item.title) {
157
+ console.log(`⚠️ Skipping item ${index}: missing required fields`);
158
+ return null;
159
+ }
160
+
161
+ // βœ… Apply universal filtering
162
+ if (!shouldIncludeResult(item, requestedType, originalQuery)) {
163
+ console.log(`❌ Filtered out: ${item.title}`);
164
+ return null;
165
+ }
166
+
167
+ // Process the item (convert to standard format)
168
+ return {
169
+ title: item.title,
170
+ magnetLink: item.magnetLink,
171
+ quality: item.quality || extractQuality(item.title),
172
+ size: formatSize(item.size),
173
+ seeders: parseInt(item.seeders || 0),
174
+ peers: parseInt(item.leechers || item.peers || 0),
175
+ source: 'Torrentz9',
176
+ infoHash: extractInfoHash(item.magnetLink),
177
+ detailUrl: item.detailUrl
178
+ };
179
+ })
180
+ .filter(Boolean);
181
+
182
+ console.log(`βœ… Processed ${processedResults.length} valid results`);
183
+ return processedResults;
184
+ }
185
+
186
+ // βœ… Main function to search torrentz9 and extract torrent data - FIXED VERSION
187
+ async function fetchRSSFeeds(searchQuery, type = 'movie') {
188
+ const cleanedQuery = cleanQuery(searchQuery);
189
+ console.log(`πŸ”„ Searching Torrentz9 for: "${cleanedQuery}" (type: ${type})`);
190
+
191
+ // Check if it's an IMDb search
192
+ const isImdb = isImdbSearch(cleanedQuery);
193
+ if (isImdb) {
194
+ console.log(`🎯 IMDb search detected: ${cleanedQuery}`);
195
+ }
196
 
197
  try {
198
  await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents bot detection
199
 
200
+ const url = `https://torrentz9.org/search?keyword=${encodeURIComponent(cleanedQuery)}`;
201
  const response = await fetch(url, {
202
  headers: {
203
  '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',
 
260
  }
261
  }));
262
 
263
+ // βœ… Apply universal processing and filtering
264
+ const processedResults = processResults(torrents, type, cleanedQuery);
265
+
266
+ // Sort results by quality and seeders
267
+ processedResults.sort((a, b) => {
268
+ const qualityOrder = { '4k': 5, '2160p': 5, '1080p': 4, '720p': 3, '480p': 2 };
269
+ const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
270
+ if (qualityDiff !== 0) return qualityDiff;
271
+ return b.seeders - a.seeders;
272
+ });
273
 
274
+ console.log(`βœ… Found ${processedResults.length} valid torrents after filtering.`);
275
+ return processedResults;
276
 
277
  } catch (error) {
278
  console.error('❌ Error fetching from Torrentz9:', error);