mikmc5 commited on
Commit
81d2771
·
verified ·
1 Parent(s): 9e19c39

Update solid.js

Browse files
Files changed (1) hide show
  1. solid.js +83 -76
solid.js CHANGED
@@ -1,108 +1,76 @@
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
 
11
- // Function to extract hash from magnet links
12
  function extractHash(magnetLink) {
13
  const hashMatch = magnetLink.match(/btih:([A-Fa-f0-9]{40})/);
14
  return hashMatch ? hashMatch[1] : '';
15
  }
16
 
17
- // Function to generate a direct magnet link from a hash
18
  function createMagnetLink(hash) {
19
  return `magnet:?xt=urn:btih:${hash}`;
20
  }
21
 
22
- // Function to format size from raw text
23
- function formatSize(sizeText) {
24
- return sizeText.replace('Size', '').trim();
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
 
27
- // Function to test token validity
28
- async function testTokenValidity(magnetLink, token) {
29
- if (!token) {
30
- console.log("⚠️ No token provided, skipping token test.");
31
- return;
32
- }
33
-
34
- console.log(`🔍 Testing token for magnet link: ${magnetLink}`);
35
-
36
- try {
37
- const url = new URL(magnetLink);
38
- url.searchParams.append('token', token);
39
-
40
- const response = await fetch(url.toString(), {
41
- headers: {
42
- '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',
43
- 'Referer': 'https://solidtorrents.to/',
44
- 'Accept-Language': 'en-US,en;q=0.9',
45
- 'Connection': 'keep-alive',
46
- 'DNT': '1',
47
- 'Upgrade-Insecure-Requests': '1'
48
- }
49
- });
50
-
51
- if (response.ok) {
52
- console.log("✅ Token is valid! Magnet link works with the token.");
53
- } else {
54
- console.log("❌ Token might not be necessary or is invalid.");
55
- }
56
- } catch (error) {
57
- console.error("❌ Error testing token validity:", error);
58
  }
59
- }
60
-
61
- // Function to fetch and parse search results from SolidTorrents
62
- async function fetchRSSFeeds(searchQuery) {
63
- console.log(`🔄 Searching SolidTorrents for: "${searchQuery}"`);
64
 
65
- try {
66
- const url = `https://solidtorrents.to/search?q=${encodeURIComponent(searchQuery)}`;
67
- const response = await fetch(url, {
68
- headers: {
69
- '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',
70
- 'Referer': 'https://solidtorrents.to/',
71
- 'Accept-Language': 'en-US,en;q=0.9',
72
- 'Connection': 'keep-alive',
73
- 'DNT': '1',
74
- 'Upgrade-Insecure-Requests': '1'
75
- }
76
- });
77
-
78
- if (!response.ok) {
79
- throw new Error(`Failed to fetch search page: ${response.status}`);
80
- }
81
-
82
- const html = await response.text();
83
- const dom = new JSDOM(html);
84
- const torrents = [];
85
-
86
- dom.window.document.querySelectorAll('.search-result').forEach(item => {
87
  const titleElement = item.querySelector('h5.title a');
88
  const magnetElement = item.querySelector('a[href^="magnet:?xt=urn:btih:"]');
89
  const sizeElement = item.querySelector('.stats div:nth-child(2)');
90
  const seedersElement = item.querySelector('.stats div:nth-child(3) font');
91
  const leechersElement = item.querySelector('.stats div:nth-child(4) font');
92
  const tokenElement = magnetElement ? magnetElement.getAttribute('data-token') : null;
93
-
94
  if (!titleElement || !magnetElement) return;
95
 
96
  const title = titleElement.textContent.trim();
97
  const magnetLink = magnetElement.getAttribute('href');
98
  const hash = extractHash(magnetLink);
99
  const quality = extractQuality(title);
100
- const size = sizeElement ? formatSize(sizeElement.textContent) : 'Unknown';
101
  const seeders = seedersElement ? parseInt(seedersElement.textContent) : 0;
102
  const leechers = leechersElement ? parseInt(leechersElement.textContent) : 0;
103
  const token = tokenElement ? tokenElement.trim() : null;
104
 
105
- torrents.push({
106
  title,
107
  magnetLink,
108
  hash,
@@ -110,22 +78,61 @@ async function fetchRSSFeeds(searchQuery) {
110
  size,
111
  seeders,
112
  leechers,
113
- token
 
114
  });
115
- });
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
- console.log(`✅ Found ${torrents.length} torrents with magnet links.`);
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- // Test token validity for the first result (if available)
120
- if (torrents.length > 0) {
121
- await testTokenValidity(torrents[0].magnetLink, torrents[0].token);
122
  }
123
 
124
- return torrents;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  } catch (error) {
126
- console.error('❌ Error fetching from SolidTorrents:', error);
127
  return [];
128
  }
129
  }
130
 
131
- export { fetchRSSFeeds };
 
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) {
28
+ if (!sizeStr) return 0;
29
+ const match = sizeStr.match(/([\d.]+)\s*(B|KB|MB|GB|TB)/i);
30
+ if (!match) return 0;
31
+
32
+ const [, value, unit] = match;
33
+ const multipliers = {
34
+ 'B': 1,
35
+ 'KB': 1024,
36
+ 'MB': 1024 * 1024,
37
+ 'GB': 1024 * 1024 * 1024,
38
+ 'TB': 1024 * 1024 * 1024 * 1024
39
+ };
40
+
41
+ return parseFloat(value) * multipliers[unit.toUpperCase()];
42
  }
43
 
44
+ function parseSearchResults(html) {
45
+ if (!html) {
46
+ console.log('No HTML content to parse');
47
+ return [];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
 
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 searchTorrents(searchQuery) {
93
+ if (!searchQuery) {
94
+ console.log('No search query provided');
95
+ return [];
96
+ }
97
 
98
+ console.log(`\n🔄 Searching SolidTorrents for: "${searchQuery}"`);
99
+
100
+ try {
101
+ const url = `${SITE_CONFIG.baseUrl}/search?q=${encodeURIComponent(searchQuery)}`;
102
+ console.log('Request URL:', url);
103
+
104
+ const response = await fetch(url, {
105
+ headers: {
106
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
107
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
108
+ 'Referer': `${SITE_CONFIG.baseUrl}/search?q=${encodeURIComponent(searchQuery)}`
109
+ }
110
+ });
111
 
112
+ if (!response.ok) {
113
+ throw new Error(`Search request failed: ${response.status}`);
 
114
  }
115
 
116
+ const html = await response.text();
117
+ const results = parseSearchResults(html);
118
+ console.log(`✅ Found ${results.length} raw results`);
119
+
120
+ results.sort((a, b) => {
121
+ const qualityOrder = { '2160p': 4, '4k': 4, '1080p': 3, '720p': 2 };
122
+ const aQuality = qualityOrder[a.quality] || 0;
123
+ const bQuality = qualityOrder[b.quality] || 0;
124
+
125
+ if (aQuality !== bQuality) {
126
+ return bQuality - aQuality;
127
+ }
128
+ return b.seeders - a.seeders;
129
+ });
130
+
131
+ return results;
132
  } catch (error) {
133
+ console.error('❌ Error searching SolidTorrents:', error);
134
  return [];
135
  }
136
  }
137
 
138
+ export { searchTorrents };