mikmc5 commited on
Commit
565c8a7
·
verified ·
1 Parent(s): b6ffa61

Update bt4g.js

Browse files
Files changed (1) hide show
  1. bt4g.js +128 -134
bt4g.js CHANGED
@@ -1,134 +1,128 @@
1
- import xml2js from 'xml2js';
2
-
3
- const parser = new xml2js.Parser({
4
- explicitArray: false,
5
- ignoreAttrs: true
6
- });
7
-
8
- function extractQuality(title) {
9
- if (!title) return '';
10
- const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
11
- return qualityMatch ? qualityMatch[1].toLowerCase() : '';
12
- }
13
-
14
- function formatSize(bytes) {
15
- const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
16
- if (bytes === 0) return '0 B';
17
- const i = Math.floor(Math.log(bytes) / Math.log(1024));
18
- return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
19
- }
20
-
21
- function extractMagnetFromDescription(description) {
22
- if (!description) return null;
23
- const magnetMatch = description.match(/magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*/);
24
- return magnetMatch ? magnetMatch[0] : null;
25
- }
26
-
27
- async function fetchRSSFeeds(searchQuery, type = 'movie') {
28
- console.log('\n🔄 Fetching RSS feed for:', searchQuery);
29
-
30
- try {
31
- // Extract title and year
32
- const yearMatch = searchQuery.match(/\(\d{4}\)/);
33
- const year = yearMatch ? yearMatch[0].replace(/[()]/g, '') : '';
34
- const title = searchQuery.replace(/\(\d{4}\)/, '').trim();
35
-
36
- // Construct the BT4G RSS URL with the search query
37
- const searchTerm = `${title} ${year}`.trim();
38
- const encodedSearch = encodeURIComponent(searchTerm);
39
- const rssUrl = `https://bt4gprx.com/search?q=${encodedSearch}&p=1&orderby=size&page=rss`;
40
-
41
- console.log('Request URL:', rssUrl);
42
-
43
- const response = await fetch(rssUrl, {
44
- headers: {
45
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
46
- 'Accept': 'application/rss+xml,application/xml,text/xml'
47
- }
48
- });
49
-
50
- if (!response.ok) {
51
- throw new Error(`Failed to fetch from BT4G: ${response.status}`);
52
- }
53
-
54
- const rssData = await response.text();
55
- const result = await parser.parseStringPromise(rssData);
56
-
57
- if (!result?.rss?.channel?.item) {
58
- console.log('No items found');
59
- return [];
60
- }
61
-
62
- const items = Array.isArray(result.rss.channel.item) ?
63
- result.rss.channel.item : [result.rss.channel.item];
64
-
65
- console.log(`Found ${items.length} items`);
66
-
67
- const streams = items.map(item => {
68
- if (!item || !item.title || !item.description) return null;
69
-
70
- // Extract magnet link from description
71
- const magnetLink = extractMagnetFromDescription(item.description);
72
- if (!magnetLink) return null;
73
-
74
- // Extract size from description (if available)
75
- const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
76
- const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
77
-
78
- // Convert size to bytes for sorting
79
- let sizeInBytes = 0;
80
- if (sizeMatch) {
81
- const [, value, unit] = sizeMatch;
82
- const multiplier = unit.toUpperCase() === 'TB' ? 1024 * 1024 * 1024 * 1024 :
83
- unit.toUpperCase() === 'GB' ? 1024 * 1024 * 1024 :
84
- 1024 * 1024;
85
- sizeInBytes = parseFloat(value) * multiplier;
86
- }
87
-
88
- // Extract info hash from magnet link
89
- const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
90
- const infoHash = infoHashMatch ? infoHashMatch[1] : null;
91
-
92
- return {
93
- magnetLink,
94
- websiteTitle: item.title, // <-- Correctly pulling title from the item
95
- quality: extractQuality(item.title),
96
- size,
97
- source: item.author || 'Unknown',
98
- infoHash,
99
- mainFileSize: sizeInBytes,
100
- pubDate: item.pubDate,
101
- categories: Array.isArray(item.category) ? item.category : [item.category]
102
- };
103
- }).filter(Boolean);
104
-
105
- // Sort by quality and size
106
- streams.sort((a, b) => {
107
- const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
108
- const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
109
- if (qualityDiff === 0) {
110
- return b.mainFileSize - a.mainFileSize;
111
- }
112
- return qualityDiff;
113
- });
114
-
115
- console.log(`✅ Processed ${streams.length} valid streams`);
116
-
117
- if (streams.length > 0) {
118
- console.log('Sample stream:', {
119
- title: streams[0].websiteTitle,
120
- quality: streams[0].quality,
121
- size: streams[0].size,
122
- source: streams[0].source
123
- });
124
- }
125
-
126
- return streams;
127
-
128
- } catch (error) {
129
- console.error('❌ Error fetching from BT4G:', error);
130
- return [];
131
- }
132
- }
133
-
134
- export { fetchRSSFeeds };
 
1
+ import xml2js from 'xml2js';
2
+
3
+ const parser = new xml2js.Parser({
4
+ explicitArray: false,
5
+ ignoreAttrs: true
6
+ });
7
+
8
+ function extractQuality(title) {
9
+ if (!title) return '';
10
+ const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
11
+ return qualityMatch ? qualityMatch[1].toLowerCase() : '';
12
+ }
13
+
14
+ function formatSize(bytes) {
15
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
16
+ if (bytes === 0) return '0 B';
17
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
18
+ return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
19
+ }
20
+
21
+ function extractMagnetFromDescription(description) {
22
+ if (!description) return null;
23
+ const magnetMatch = description.match(/magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*/);
24
+ return magnetMatch ? magnetMatch[0] : null;
25
+ }
26
+
27
+ async function fetchRSSFeeds(searchQuery, type = 'movie') {
28
+ console.log('\n🔄 Fetching RSS feed for:', searchQuery);
29
+
30
+ try {
31
+ // Construct the BT4G RSS URL with the search query
32
+ const encodedSearch = encodeURIComponent(searchQuery);
33
+ const rssUrl = `https://bt4gprx.com/search?q=${encodedSearch}&p=1&orderby=size&page=rss`;
34
+
35
+ console.log('Request URL:', rssUrl);
36
+
37
+ const response = await fetch(rssUrl, {
38
+ headers: {
39
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
40
+ 'Accept': 'application/rss+xml,application/xml,text/xml'
41
+ }
42
+ });
43
+
44
+ if (!response.ok) {
45
+ throw new Error(`Failed to fetch from BT4G: ${response.status}`);
46
+ }
47
+
48
+ const rssData = await response.text();
49
+ const result = await parser.parseStringPromise(rssData);
50
+
51
+ if (!result?.rss?.channel?.item) {
52
+ console.log('No items found');
53
+ return [];
54
+ }
55
+
56
+ const items = Array.isArray(result.rss.channel.item) ? result.rss.channel.item : [result.rss.channel.item];
57
+
58
+ console.log(`Found ${items.length} items`);
59
+
60
+ const streams = items.map(item => {
61
+ if (!item || !item.title || !item.description) return null;
62
+
63
+ // Extract magnet link from description
64
+ const magnetLink = extractMagnetFromDescription(item.description);
65
+ if (!magnetLink) return null;
66
+
67
+ // Extract size from description (if available)
68
+ const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
69
+ const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
70
+
71
+ // Convert size to bytes for sorting
72
+ let sizeInBytes = 0;
73
+ if (sizeMatch) {
74
+ const [, value, unit] = sizeMatch;
75
+ const multiplier = unit.toUpperCase() === 'TB' ? 1024 * 1024 * 1024 * 1024 :
76
+ unit.toUpperCase() === 'GB' ? 1024 * 1024 * 1024 :
77
+ 1024 * 1024;
78
+ sizeInBytes = parseFloat(value) * multiplier;
79
+ }
80
+
81
+ // Extract info hash from magnet link
82
+ const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
83
+ const infoHash = infoHashMatch ? infoHashMatch[1] : null;
84
+
85
+ return {
86
+ magnetLink,
87
+ websiteTitle: item.title, // Make sure it's pulled from the RSS title correctly
88
+ quality: extractQuality(item.title),
89
+ size,
90
+ source: item.author || 'Unknown',
91
+ infoHash,
92
+ mainFileSize: sizeInBytes,
93
+ pubDate: item.pubDate,
94
+ categories: Array.isArray(item.category) ? item.category : [item.category]
95
+ };
96
+ }).filter(Boolean);
97
+
98
+ // Sort by quality and size
99
+ streams.sort((a, b) => {
100
+ const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
101
+ const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
102
+ if (qualityDiff === 0) {
103
+ return b.mainFileSize - a.mainFileSize;
104
+ }
105
+ return qualityDiff;
106
+ });
107
+
108
+ console.log(`✅ Processed ${streams.length} valid streams`);
109
+
110
+ if (streams.length > 0) {
111
+ console.log('Sample stream:', {
112
+ title: streams[0].websiteTitle,
113
+ quality: streams[0].quality,
114
+ size: streams[0].size,
115
+ source: streams[0].source
116
+ });
117
+ }
118
+
119
+ return streams;
120
+
121
+ } catch (error) {
122
+ console.error('❌ Error fetching from BT4G:', error);
123
+ return [];
124
+ }
125
+ }
126
+
127
+
128
+ export { fetchRSSFeeds };