mikmc5 commited on
Commit
6e6f6b2
·
verified ·
1 Parent(s): 61cd19b

Update btsow.js

Browse files
Files changed (1) hide show
  1. btsow.js +48 -116
btsow.js CHANGED
@@ -1,163 +1,95 @@
1
  import fetch from 'node-fetch';
2
  import { JSDOM } from 'jsdom';
3
- import xml2js from 'xml2js';
4
 
5
- const parser = new xml2js.Parser({
6
- explicitArray: false,
7
- ignoreAttrs: true
8
- });
9
-
10
- // Helper Functions
11
  function extractQuality(title) {
12
  if (!title) return '';
13
  const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
14
  return qualityMatch ? qualityMatch[1].toLowerCase() : '';
15
  }
16
 
 
17
  function extractMagnetFromDescription(description) {
18
  if (!description) return null;
19
- const magnetMatch = description.match(/magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*/);
20
- return magnetMatch ? magnetMatch[0] : null;
21
- }
22
-
23
- function generateMagnetLinkFromHash(hash) {
24
- if (!hash) return null;
25
- return `magnet:?xt=urn:btih:${hash}`;
26
  }
27
 
 
28
  async function extractMagnetLinksFromPage(url) {
29
  try {
30
  const response = await fetch(url);
 
 
 
31
  const html = await response.text();
32
 
33
  const dom = new JSDOM(html);
34
  const magnetLinks = dom.window.document.querySelectorAll('a[href^="magnet:?xt=urn:btih:"]');
35
- const linksArray = Array.from(magnetLinks).map(link => link.href);
36
 
 
37
  return linksArray.length > 0 ? linksArray : null;
38
  } catch (error) {
39
- console.error('❌ Error fetching page:', url, error);
40
  return null;
41
  }
42
  }
43
 
44
- // Main fetchRSSFeeds function
45
- async function fetchRSSFeeds(searchQuery, type = 'movie') {
46
- console.log(`\n🔄 Fetching RSS feed for: ${searchQuery}`);
47
 
48
  try {
49
- const searchTerm = encodeURIComponent(searchQuery);
50
- const rssUrl = `https://www.torrentdownload.info/feed_s?q=${searchTerm}`;
51
-
52
- console.log('📡 Requesting RSS feed:', rssUrl);
53
-
54
- const response = await fetch(rssUrl, {
55
- headers: {
56
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
57
- 'Accept': 'application/rss+xml,application/xml,text/xml'
58
- }
59
- });
60
-
61
  if (!response.ok) {
62
- throw new Error(`Failed to fetch RSS feed: HTTP ${response.status}`);
63
  }
 
64
 
65
- const rssData = await response.text();
66
- const result = await parser.parseStringPromise(rssData);
67
-
68
- if (!result?.rss?.channel?.item) {
69
- console.log('⚠️ No items found in RSS feed.');
70
- return [];
71
- }
72
-
73
- const items = Array.isArray(result.rss.channel.item) ? result.rss.channel.item : [result.rss.channel.item];
74
-
75
- console.log(`✅ Found ${items.length} items in RSS feed.`);
76
-
77
- // Process all items
78
- const streams = await Promise.all(items.map(async (item) => {
79
- if (!item || !item.title || !item.description) return null;
80
 
81
- let magnetLink = extractMagnetFromDescription(item.description);
 
 
 
 
82
 
83
- if (magnetLink) {
84
- console.log(`🔗 Magnet link found in description: ${magnetLink}`);
85
- } else if (item.link) {
86
- console.log(`🔄 No magnet in description. Extracting from page: ${item.link}`);
87
- const extractedLinks = await extractMagnetLinksFromPage(item.link);
88
- if (extractedLinks) {
89
- magnetLink = extractedLinks[0];
90
- console.log(`✅ Extracted magnet from page: ${magnetLink}`);
91
- }
92
- }
93
 
94
- if (!magnetLink) {
95
- const hashMatch = item.description.match(/Hash:\s*([A-Za-z0-9]+)/i);
96
- const hash = hashMatch ? hashMatch[1] : item.guid ? item.guid.split('/').pop() : null;
97
-
98
- if (hash) {
99
- magnetLink = generateMagnetLinkFromHash(hash);
100
- console.log(`🔑 Generated magnet from hash: ${magnetLink}`);
101
- }
102
- }
103
 
104
- if (!magnetLink) {
105
- console.log(`❌ No magnet found for: ${item.title}`);
106
- return null;
107
- }
108
-
109
- const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
110
- const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
 
 
111
 
112
- let sizeInBytes = 0;
113
- if (sizeMatch) {
114
- const [, value, unit] = sizeMatch;
115
- const multiplier = unit.toUpperCase() === 'TB' ? 1024 * 1024 * 1024 * 1024 :
116
- unit.toUpperCase() === 'GB' ? 1024 * 1024 * 1024 :
117
- 1024 * 1024;
118
- sizeInBytes = parseFloat(value) * multiplier;
119
  }
120
-
121
- const categories = Array.isArray(item.category) ? item.category : [item.category];
122
-
123
- return {
124
- magnetLink,
125
- websiteTitle: item.title,
126
- quality: extractQuality(item.title),
127
- size,
128
- source: item.author || 'Unknown',
129
- infoHash: hash || null,
130
- mainFileSize: sizeInBytes,
131
- pubDate: item.pubDate,
132
- categories
133
- };
134
  }));
135
 
136
- const validStreams = streams.filter(Boolean);
137
-
138
- validStreams.sort((a, b) => {
139
- const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
140
- const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
141
- return qualityDiff === 0 ? b.mainFileSize - a.mainFileSize : qualityDiff;
142
- });
143
-
144
- console.log(`🎉 Successfully processed ${validStreams.length} valid streams.`);
145
- return validStreams;
146
 
147
  } catch (error) {
148
- console.error('❌ Error during fetchRSSFeeds:', error);
149
  return [];
150
  }
151
  }
152
 
153
- // Test Function
154
- (async () => {
155
- try {
156
- const results = await fetchRSSFeeds('Steins;Gate 0 S01E01');
157
- console.log(results);
158
- } catch (error) {
159
- console.error('❌ Error executing fetchRSSFeeds:', error);
160
- }
161
- })();
162
-
163
  export { fetchRSSFeeds };
 
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 magnet link from HTML description
12
  function extractMagnetFromDescription(description) {
13
  if (!description) return null;
14
+ const magnetMatch = description.match(/href="(magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*)"/);
15
+ return magnetMatch ? magnetMatch[1] : null;
 
 
 
 
 
16
  }
17
 
18
+ // Function to fetch and extract magnet links from a BTSOW page
19
  async function extractMagnetLinksFromPage(url) {
20
  try {
21
  const response = await fetch(url);
22
+ if (!response.ok) {
23
+ throw new Error(`Failed to fetch page: ${response.status}`);
24
+ }
25
  const html = await response.text();
26
 
27
  const dom = new JSDOM(html);
28
  const magnetLinks = dom.window.document.querySelectorAll('a[href^="magnet:?xt=urn:btih:"]');
 
29
 
30
+ const linksArray = Array.from(magnetLinks).map(link => link.href);
31
  return linksArray.length > 0 ? linksArray : null;
32
  } catch (error) {
33
+ console.error('❌ Error fetching magnet links:', error);
34
  return null;
35
  }
36
  }
37
 
38
+ // Function to fetch and parse BTSOW search results
39
+ async function fetchRSSFeeds(searchQuery) {
40
+ console.log(`🔄 Searching BTSOW for: "${searchQuery}"`);
41
 
42
  try {
43
+ const url = `https://btsow.motorcycles/search/${encodeURIComponent(searchQuery)}`;
44
+ const response = await fetch(url);
 
 
 
 
 
 
 
 
 
 
45
  if (!response.ok) {
46
+ throw new Error(`Failed to fetch BTSOW search page: ${response.status}`);
47
  }
48
+ const html = await response.text();
49
 
50
+ const dom = new JSDOM(html);
51
+ const torrents = [];
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ dom.window.document.querySelectorAll('.data-list .row').forEach(row => {
54
+ const magnetLinkElement = row.querySelector('a[href^="//btsow.motorcycles/magnet/detail/hash/"]');
55
+ const titleElement = row.querySelector('.file');
56
+ const sizeElement = row.querySelector('.size');
57
+ const dateElement = row.querySelector('.date');
58
 
59
+ if (!magnetLinkElement || !titleElement || !sizeElement || !dateElement) return;
 
 
 
 
 
 
 
 
 
60
 
61
+ const detailUrl = 'https:' + magnetLinkElement.getAttribute('href');
62
+ const title = titleElement.textContent.trim();
63
+ const size = sizeElement.textContent.trim();
64
+ const date = dateElement.textContent.trim();
65
+ const quality = extractQuality(title);
 
 
 
 
66
 
67
+ torrents.push({
68
+ title,
69
+ detailUrl,
70
+ size,
71
+ date,
72
+ quality,
73
+ magnetLinks: [],
74
+ });
75
+ });
76
 
77
+ // Fetch magnet links for each torrent entry
78
+ await Promise.all(torrents.map(async (torrent) => {
79
+ const magnetLinks = await extractMagnetLinksFromPage(torrent.detailUrl);
80
+ if (magnetLinks) {
81
+ torrent.magnetLinks = magnetLinks;
 
 
82
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  }));
84
 
85
+ console.log(`✅ Found ${torrents.length} torrents.`);
86
+ return torrents.filter(torrent => torrent.magnetLinks.length > 0);
 
 
 
 
 
 
 
 
87
 
88
  } catch (error) {
89
+ console.error('❌ Error fetching from BTSOW:', error);
90
  return [];
91
  }
92
  }
93
 
94
+ // Export fetchRSSFeeds as the single function
 
 
 
 
 
 
 
 
 
95
  export { fetchRSSFeeds };