Create pbay.js
Browse files
pbay.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fetch from 'node-fetch';
|
| 2 |
+
|
| 3 |
+
// β
Extracts video quality from title
|
| 4 |
+
function extractQuality(title) {
|
| 5 |
+
if (!title) return '';
|
| 6 |
+
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd|WEBRip|BluRay|REMUX)\b/i);
|
| 7 |
+
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
// β
Formats size into human-readable format
|
| 11 |
+
function formatSize(bytes) {
|
| 12 |
+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
| 13 |
+
if (bytes === 0) return '0 B';
|
| 14 |
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
| 15 |
+
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
// β
Extracts magnet link from "Guid"
|
| 19 |
+
function extractMagnetFromGuid(guid) {
|
| 20 |
+
if (!guid) return null;
|
| 21 |
+
const magnetMatch = guid.match(/magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*/);
|
| 22 |
+
return magnetMatch ? magnetMatch[0] : null;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// β
Removes the year from queries
|
| 26 |
+
function cleanQuery(query) {
|
| 27 |
+
return query.replace(/\(\d{4}\)/g, '').trim(); // Removes (YYYY)
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// β
Caches results to prevent duplicate requests
|
| 31 |
+
const cache = new Map();
|
| 32 |
+
|
| 33 |
+
// β
Fetch data from Jackett API (The Pirate Bay)
|
| 34 |
+
async function fetchRSSFeeds(searchQuery, type = 'movie') {
|
| 35 |
+
const cleanedQuery = cleanQuery(searchQuery);
|
| 36 |
+
console.log(`π Fetching RSS feed for: "${cleanedQuery}"`);
|
| 37 |
+
|
| 38 |
+
if (cache.has(cleanedQuery)) {
|
| 39 |
+
console.log(`β‘ Using cached results for: "${cleanedQuery}"`);
|
| 40 |
+
return cache.get(cleanedQuery);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
const API_URL = `http://81.150.31.153:9117/api/v2.0/indexers/all/results?apikey=1c99yaajym29qbrb715q3me1qwj2d42g&Query=${encodeURIComponent(cleanedQuery)}&Category%5B%5D=2000&Category%5B%5D=2030&Category%5B%5D=2040&Category%5B%5D=2045&Category%5B%5D=5000&Category%5B%5D=5040&Category%5B%5D=5045&Category%5B%5D=5050&Category%5B%5D=8000&Category%5B%5D=100201&Category%5B%5D=100202&Category%5B%5D=100205&Category%5B%5D=100207&Category%5B%5D=100208&Category%5B%5D=100211&Category%5B%5D=100212&Category%5B%5D=100501&Category%5B%5D=100502&Category%5B%5D=100505&Category%5B%5D=100507&Tracker%5B%5D=thepiratebay&_=1741639126439`;
|
| 44 |
+
|
| 45 |
+
try {
|
| 46 |
+
await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents bot detection
|
| 47 |
+
|
| 48 |
+
const response = await fetch(API_URL, {
|
| 49 |
+
headers: {
|
| 50 |
+
'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',
|
| 51 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 52 |
+
'Connection': 'keep-alive',
|
| 53 |
+
'Cookie': 'Jackett=your_cookie_here' // β
Use your Jackett cookie if required
|
| 54 |
+
}
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
if (!response.ok) {
|
| 58 |
+
throw new Error(`β Failed to fetch Jackett API: ${response.status}`);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
const jsonResponse = await response.text();
|
| 62 |
+
const data = JSON.parse(jsonResponse);
|
| 63 |
+
const results = data.Results || [];
|
| 64 |
+
|
| 65 |
+
const streams = results.map((item) => {
|
| 66 |
+
if (!item || !item.Title || !item.Guid) return null;
|
| 67 |
+
|
| 68 |
+
// β
Extract magnet link from "Guid"
|
| 69 |
+
const magnetLink = extractMagnetFromGuid(item.Guid);
|
| 70 |
+
if (!magnetLink) return null;
|
| 71 |
+
|
| 72 |
+
// β
Extract size
|
| 73 |
+
const size = item.Size ? formatSize(item.Size) : 'Unknown';
|
| 74 |
+
|
| 75 |
+
// β
Convert size to bytes for sorting
|
| 76 |
+
let sizeInBytes = item.Size || 0;
|
| 77 |
+
|
| 78 |
+
// β
Extract IMDb ID if available
|
| 79 |
+
const imdbId = item.Imdb ? `tt${item.Imdb}` : null;
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
magnetLink,
|
| 83 |
+
websiteTitle: item.Title,
|
| 84 |
+
quality: extractQuality(item.Title),
|
| 85 |
+
size,
|
| 86 |
+
source: item.Tracker || 'Unknown',
|
| 87 |
+
infoHash: item.InfoHash || null,
|
| 88 |
+
seeders: item.Seeders || 0,
|
| 89 |
+
peers: item.Peers || 0,
|
| 90 |
+
imdbId,
|
| 91 |
+
mainFileSize: sizeInBytes,
|
| 92 |
+
pubDate: item.PublishDate,
|
| 93 |
+
categories: Array.isArray(item.CategoryDesc) ? item.CategoryDesc : [item.CategoryDesc]
|
| 94 |
+
};
|
| 95 |
+
}).filter(Boolean);
|
| 96 |
+
|
| 97 |
+
// β
Sort by quality and size
|
| 98 |
+
streams.sort((a, b) => {
|
| 99 |
+
const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
|
| 100 |
+
const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
|
| 101 |
+
if (qualityDiff === 0) {
|
| 102 |
+
return b.mainFileSize - a.mainFileSize;
|
| 103 |
+
}
|
| 104 |
+
return qualityDiff;
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
console.log(`β
Processed ${streams.length} valid streams`);
|
| 108 |
+
|
| 109 |
+
// β
Store result in cache
|
| 110 |
+
cache.set(cleanedQuery, streams);
|
| 111 |
+
|
| 112 |
+
return streams;
|
| 113 |
+
} catch (error) {
|
| 114 |
+
console.error('β Error fetching from Jackett:', error);
|
| 115 |
+
return [];
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
// β
Export function for Stremio addon
|
| 120 |
+
export { fetchRSSFeeds };
|