mikmc5 commited on
Commit
786b5a6
·
verified ·
1 Parent(s): d2806d2

Update bitsearch.js

Browse files
Files changed (1) hide show
  1. bitsearch.js +76 -139
bitsearch.js CHANGED
@@ -1,50 +1,27 @@
 
 
 
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) {
@@ -71,104 +48,80 @@ function parseSearchResults(html) {
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
 
@@ -179,24 +132,9 @@ async function searchTorrents(searchQuery, type = 'movie') {
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;
@@ -207,16 +145,15 @@ async function searchTorrents(searchQuery, type = 'movie') {
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
  }
@@ -226,4 +163,4 @@ async function searchTorrents(searchQuery, type = 'movie') {
226
  }
227
  }
228
 
229
- export { searchTorrents };
 
1
+ import fetch from 'node-fetch';
2
+ import { JSDOM } from 'jsdom';
3
+
4
  const SITE_CONFIG = {
5
+ baseUrl: 'https://solidtorrents.to',
6
  fallbackUrls: [
7
+ 'https://solidtorrents.cc',
8
+ 'https://solidtorrents.me'
9
  ]
10
  };
11
 
12
+ function extractQuality(title) {
13
+ if (!title) return '';
14
+ const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
15
+ return qualityMatch ? qualityMatch[1].toLowerCase() : '';
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
+ function extractHash(magnetLink) {
19
+ const hashMatch = magnetLink.match(/btih:([A-Fa-f0-9]{40})/);
20
+ return hashMatch ? hashMatch[1] : '';
21
+ }
22
 
23
+ function createMagnetLink(hash) {
24
+ return `magnet:?xt=urn:btih:${hash}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
 
27
  function parseSize(sizeStr) {
 
48
  }
49
 
50
  const results = [];
51
+ const dom = new JSDOM(html);
52
+
53
+ dom.window.document.querySelectorAll('.search-result').forEach(item => {
54
  try {
55
+ const titleElement = item.querySelector('h5.title a');
56
+ const magnetElement = item.querySelector('a[href^="magnet:?xt=urn:btih:"]');
57
+ const sizeElement = item.querySelector('.stats div:nth-child(2)');
58
+ const seedersElement = item.querySelector('.stats div:nth-child(3) font');
59
+ const leechersElement = item.querySelector('.stats div:nth-child(4) font');
60
+ const tokenElement = magnetElement ? magnetElement.getAttribute('data-token') : null;
61
+
62
+ if (!titleElement || !magnetElement) return;
63
+
64
+ const title = titleElement.textContent.trim();
65
+ const magnetLink = magnetElement.getAttribute('href');
66
+ const hash = extractHash(magnetLink);
67
+ const quality = extractQuality(title);
68
+ const size = sizeElement ? sizeElement.textContent.trim() : 'Unknown';
69
+ const seeders = seedersElement ? parseInt(seedersElement.textContent) : 0;
70
+ const leechers = leechersElement ? parseInt(leechersElement.textContent) : 0;
71
+ const token = tokenElement ? tokenElement.trim() : null;
72
+
73
+ results.push({
74
+ title,
75
+ magnetLink,
76
+ hash,
77
+ quality,
78
+ size,
79
+ seeders,
80
+ leechers,
81
+ token,
82
+ mainFileSize: parseSize(size)
83
+ });
84
  } catch (error) {
85
  console.error('Error parsing result:', error);
86
  }
87
+ });
88
 
89
  return results;
90
  }
91
 
92
+ async function fetchWithTimeout(url, options = {}, timeout = 30000) {
93
+ const controller = new AbortController();
94
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
 
 
95
 
96
+ try {
97
+ const response = await fetch(url, {
98
+ ...options,
99
+ signal: controller.signal,
100
+ headers: {
101
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
102
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9',
103
+ 'Accept-Language': 'en-US,en;q=0.9',
104
+ ...options.headers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  }
106
+ });
107
+ clearTimeout(timeoutId);
108
+ return response;
109
+ } catch (error) {
110
+ clearTimeout(timeoutId);
111
+ throw error;
112
  }
 
 
113
  }
114
 
115
+ async function searchTorrents(searchQuery) {
116
  if (!searchQuery) {
117
  console.log('No search query provided');
118
  return [];
119
  }
120
 
121
+ console.log(`\n🔄 Searching SolidTorrents for: "${searchQuery}"`);
122
 
123
  try {
124
+ const formattedQuery = searchQuery.replace(/\s+/g, '+').toLowerCase();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  const url = `${SITE_CONFIG.baseUrl}/search?q=${encodeURIComponent(formattedQuery)}`;
126
  console.log('Request URL:', url);
127
 
 
132
 
133
  const html = await response.text();
134
  const results = parseSearchResults(html);
135
+ console.log(`✅ Found ${results.length} raw results`);
136
 
137
+ results.sort((a, b) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  const qualityOrder = { '2160p': 4, '4k': 4, '1080p': 3, '720p': 2 };
139
  const aQuality = qualityOrder[a.quality] || 0;
140
  const bQuality = qualityOrder[b.quality] || 0;
 
145
  return b.seeders - a.seeders;
146
  });
147
 
148
+ return results;
 
149
  } catch (error) {
150
+ console.error('❌ Error searching SolidTorrents:', error);
151
 
152
  for (const fallbackUrl of SITE_CONFIG.fallbackUrls) {
153
  try {
154
  console.log(`Trying fallback URL: ${fallbackUrl}`);
155
  SITE_CONFIG.baseUrl = fallbackUrl;
156
+ return await searchTorrents(searchQuery);
157
  } catch (fallbackError) {
158
  console.error(`Fallback ${fallbackUrl} also failed:`, fallbackError);
159
  }
 
163
  }
164
  }
165
 
166
+ export { searchTorrents };