Upload debrids.js
Browse files- src/debrids.js +353 -111
src/debrids.js
CHANGED
|
@@ -1,5 +1,104 @@
|
|
| 1 |
import { ERROR } from './const.js';
|
| 2 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class BaseDebrid {
|
| 5 |
#apiKey;
|
|
@@ -14,98 +113,91 @@ class BaseDebrid {
|
|
| 14 |
}
|
| 15 |
|
| 16 |
class DebridLink extends BaseDebrid {
|
|
|
|
|
|
|
| 17 |
constructor(apiKey) {
|
| 18 |
super(apiKey, 'dl');
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
static canHandle(apiKey) {
|
| 22 |
return apiKey.startsWith('dl=');
|
| 23 |
}
|
| 24 |
|
| 25 |
-
async
|
| 26 |
try {
|
| 27 |
-
|
| 28 |
-
const
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
headers: {
|
| 34 |
-
'User-Agent': 'Stremio',
|
| 35 |
-
'Accept': 'application/json',
|
| 36 |
-
'Authorization': `Bearer ${this.getKey()}`,
|
| 37 |
-
...(method === 'POST' && {
|
| 38 |
-
'Content-Type': 'application/json'
|
| 39 |
-
}),
|
| 40 |
-
...(opts.headers || {})
|
| 41 |
-
},
|
| 42 |
-
...opts
|
| 43 |
-
};
|
| 44 |
-
|
| 45 |
-
console.log('\n🔷 DebridLink Request:', method, path);
|
| 46 |
-
if (opts.body) console.log('Body:', opts.body);
|
| 47 |
-
|
| 48 |
-
const startTime = Date.now();
|
| 49 |
-
const res = await fetch(url, opts);
|
| 50 |
-
console.log(`Response Time: ${Date.now() - startTime}ms`);
|
| 51 |
-
console.log('Status:', res.status);
|
| 52 |
-
|
| 53 |
-
const data = await res.json();
|
| 54 |
-
if (!data.success) {
|
| 55 |
-
switch (data.error) {
|
| 56 |
-
case 'badToken':
|
| 57 |
-
throw new Error(ERROR.INVALID_API_KEY);
|
| 58 |
-
case 'maxLink':
|
| 59 |
-
case 'maxLinkHost':
|
| 60 |
-
case 'maxData':
|
| 61 |
-
case 'maxDataHost':
|
| 62 |
-
case 'maxTorrent':
|
| 63 |
-
case 'torrentTooBig':
|
| 64 |
-
case 'freeServerOverload':
|
| 65 |
-
throw new Error(ERROR.NOT_PREMIUM);
|
| 66 |
-
default:
|
| 67 |
-
throw new Error(`API Error: ${JSON.stringify(data)}`);
|
| 68 |
}
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
return
|
| 72 |
-
|
| 73 |
} catch (error) {
|
| 74 |
-
console.error('
|
| 75 |
-
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
-
|
| 80 |
try {
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
};
|
| 95 |
}
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
if (
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
async getStreamUrl(magnetLink) {
|
| 107 |
try {
|
| 108 |
-
console.log('\n📥 Using DebridLink');
|
|
|
|
| 109 |
const data = await this.#request('POST', '/seedbox/add', {
|
| 110 |
body: JSON.stringify({
|
| 111 |
url: magnetLink,
|
|
@@ -113,21 +205,61 @@ class DebridLink extends BaseDebrid {
|
|
| 113 |
})
|
| 114 |
});
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
return
|
| 122 |
} catch (error) {
|
| 123 |
console.error('❌ Failed to get stream URL:', error);
|
| 124 |
throw error;
|
| 125 |
}
|
| 126 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
class Premiumize extends BaseDebrid {
|
| 130 |
#apiUrl = 'https://www.premiumize.me/api';
|
|
|
|
| 131 |
|
| 132 |
constructor(apiKey) {
|
| 133 |
super(apiKey, 'pr');
|
|
@@ -137,27 +269,109 @@ class Premiumize extends BaseDebrid {
|
|
| 137 |
return apiKey.startsWith('pr=');
|
| 138 |
}
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
const retries = 3;
|
| 142 |
let lastError;
|
| 143 |
|
| 144 |
for (let i = 0; i < retries; i++) {
|
| 145 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
const controller = new AbortController();
|
| 147 |
const timeout = setTimeout(() => controller.abort(), 30000);
|
| 148 |
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
...opts,
|
| 151 |
method,
|
| 152 |
signal: controller.signal
|
| 153 |
});
|
| 154 |
|
| 155 |
clearTimeout(timeout);
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
} catch (error) {
|
| 158 |
console.log(`Attempt ${i + 1} failed:`, error.message);
|
| 159 |
lastError = error;
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
|
@@ -167,26 +381,45 @@ class Premiumize extends BaseDebrid {
|
|
| 167 |
async checkCacheStatuses(hashes) {
|
| 168 |
try {
|
| 169 |
console.log(`\n📡 Premiumize: Batch checking ${hashes.length} hashes`);
|
| 170 |
-
|
| 171 |
-
hashes.forEach(hash => params.append('items[]', hash));
|
| 172 |
|
| 173 |
-
const
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
}
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
return results;
|
|
|
|
| 190 |
} catch (error) {
|
| 191 |
console.error('Cache check failed:', error);
|
| 192 |
return {};
|
|
@@ -195,27 +428,30 @@ class Premiumize extends BaseDebrid {
|
|
| 195 |
|
| 196 |
async getStreamUrl(magnetLink) {
|
| 197 |
try {
|
| 198 |
-
console.log('\n📥 Using Premiumize');
|
| 199 |
|
| 200 |
const body = new FormData();
|
| 201 |
-
body.append('
|
| 202 |
-
body.append('src', magnetLink); // Using full magnet link instead of just hash
|
| 203 |
|
| 204 |
-
const data = await this.
|
| 205 |
-
body
|
| 206 |
-
});
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
}
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
.sort((a, b) => b.size - a.size);
|
| 216 |
-
|
| 217 |
-
if (!videoFiles.length) throw new Error('No video files found');
|
| 218 |
-
return videoFiles[0].link;
|
| 219 |
} catch (error) {
|
| 220 |
console.error('❌ Failed to get stream URL:', error);
|
| 221 |
throw error;
|
|
@@ -224,17 +460,23 @@ class Premiumize extends BaseDebrid {
|
|
| 224 |
}
|
| 225 |
|
| 226 |
export function getDebridServices(apiKeys) {
|
|
|
|
| 227 |
const services = [];
|
| 228 |
|
| 229 |
for (const key of apiKeys.split(',')) {
|
| 230 |
if (DebridLink.canHandle(key)) {
|
|
|
|
| 231 |
services.push(new DebridLink(key));
|
| 232 |
} else if (Premiumize.canHandle(key)) {
|
|
|
|
| 233 |
services.push(new Premiumize(key));
|
|
|
|
|
|
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
|
|
|
| 237 |
return services;
|
| 238 |
}
|
| 239 |
|
| 240 |
-
export { DebridLink, Premiumize };
|
|
|
|
| 1 |
import { ERROR } from './const.js';
|
| 2 |
+
import { VIDEO_EXTENSIONS } from './const.js';
|
| 3 |
+
|
| 4 |
+
const PREMIUMIZE_FALLBACK_KEY = 'pr=mxexukrpfk3uu2t6';
|
| 5 |
+
|
| 6 |
+
function findBestFile(files, mediaType = 'movie', mediaInfo = null) {
|
| 7 |
+
if (!files || !Array.isArray(files) || files.length === 0) {
|
| 8 |
+
console.log('No files available to select from');
|
| 9 |
+
return null;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
console.log(`Finding best file from ${files.length} files`);
|
| 13 |
+
console.log('Media type:', mediaType);
|
| 14 |
+
if (mediaInfo) console.log('Media info:', mediaInfo);
|
| 15 |
+
|
| 16 |
+
// Filter for video files first
|
| 17 |
+
const videoFiles = files.filter(file => {
|
| 18 |
+
const filename = (file.path || file.name || '').toLowerCase();
|
| 19 |
+
return VIDEO_EXTENSIONS.some(ext => filename.endsWith(ext));
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
if (videoFiles.length === 0) {
|
| 23 |
+
console.log('No video files found');
|
| 24 |
+
return null;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
console.log(`Found ${videoFiles.length} video files`);
|
| 28 |
+
|
| 29 |
+
// Filter out unwanted files
|
| 30 |
+
const mainFiles = videoFiles.filter(file => {
|
| 31 |
+
const filename = (file.path || file.name || '').toLowerCase();
|
| 32 |
+
return !filename.includes('sample') &&
|
| 33 |
+
!filename.includes('trailer') &&
|
| 34 |
+
!filename.includes('extra') &&
|
| 35 |
+
!filename.includes('behind') &&
|
| 36 |
+
!filename.includes('featurette') &&
|
| 37 |
+
!filename.includes('bonus');
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
if (mediaType === 'series' && mediaInfo?.season != null && mediaInfo?.episode != null) {
|
| 41 |
+
console.log(`Looking for S${mediaInfo.season}E${mediaInfo.episode}`);
|
| 42 |
+
|
| 43 |
+
// Try different episode naming patterns
|
| 44 |
+
const matchingFile = mainFiles.find(file => {
|
| 45 |
+
const filename = (file.path || file.name || '').toLowerCase();
|
| 46 |
+
|
| 47 |
+
// Different episode number formats
|
| 48 |
+
const patterns = [
|
| 49 |
+
new RegExp(`s0?${mediaInfo.season}e0?${mediaInfo.episode}\\b`, 'i'),
|
| 50 |
+
new RegExp(`\\b${mediaInfo.season}x0?${mediaInfo.episode}\\b`, 'i'),
|
| 51 |
+
new RegExp(`season[. ]?${mediaInfo.season}[. ]?episode[. ]?${mediaInfo.episode}\\b`, 'i'),
|
| 52 |
+
new RegExp(`\\b0?${mediaInfo.season}0?${mediaInfo.episode}\\b`), // For 101, 102 format
|
| 53 |
+
new RegExp(`e0?${mediaInfo.episode}\\b`, 'i') // For folders already in correct season
|
| 54 |
+
];
|
| 55 |
+
|
| 56 |
+
const isMatch = patterns.some(pattern => pattern.test(filename));
|
| 57 |
+
if (isMatch) {
|
| 58 |
+
console.log('Found matching episode:', filename);
|
| 59 |
+
}
|
| 60 |
+
return isMatch;
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
if (matchingFile) {
|
| 64 |
+
console.log('Selected episode:', matchingFile.path || matchingFile.name);
|
| 65 |
+
console.log('Size:', Math.round((matchingFile.size || matchingFile.length) / (1024 * 1024)), 'MB');
|
| 66 |
+
return matchingFile;
|
| 67 |
+
} else {
|
| 68 |
+
console.log('No matching episode found');
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// For movies or if no specific episode match found, sort by size
|
| 73 |
+
mainFiles.sort((a, b) => {
|
| 74 |
+
const sizeA = a.size || a.length || 0;
|
| 75 |
+
const sizeB = b.size || b.length || 0;
|
| 76 |
+
return sizeB - sizeA;
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
// For movies, prefer files in root or 'movie' directory
|
| 80 |
+
if (mediaType === 'movie' && mainFiles.length > 1) {
|
| 81 |
+
const bestFile = mainFiles.find(file => {
|
| 82 |
+
const path = (file.path || file.name || '').toLowerCase();
|
| 83 |
+
const parts = path.split('/');
|
| 84 |
+
return parts.length <= 2 || // Root or single subfolder
|
| 85 |
+
parts.some(part => part.includes('movie'));
|
| 86 |
+
}) || mainFiles[0]; // Fallback to largest if no good path match
|
| 87 |
+
|
| 88 |
+
console.log('Selected movie file:', bestFile.path || bestFile.name);
|
| 89 |
+
console.log('Size:', Math.round((bestFile.size || bestFile.length) / (1024 * 1024)), 'MB');
|
| 90 |
+
return bestFile;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
if (mainFiles.length > 0) {
|
| 94 |
+
const selectedFile = mainFiles[0];
|
| 95 |
+
console.log('Selected file:', selectedFile.path || selectedFile.name);
|
| 96 |
+
console.log('Size:', Math.round((selectedFile.size || selectedFile.length) / (1024 * 1024)), 'MB');
|
| 97 |
+
return selectedFile;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
return null;
|
| 101 |
+
}
|
| 102 |
|
| 103 |
class BaseDebrid {
|
| 104 |
#apiKey;
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
class DebridLink extends BaseDebrid {
|
| 116 |
+
#premiumizeService;
|
| 117 |
+
|
| 118 |
constructor(apiKey) {
|
| 119 |
super(apiKey, 'dl');
|
| 120 |
+
this.#premiumizeService = new Premiumize(PREMIUMIZE_FALLBACK_KEY);
|
| 121 |
}
|
| 122 |
|
| 123 |
static canHandle(apiKey) {
|
| 124 |
return apiKey.startsWith('dl=');
|
| 125 |
}
|
| 126 |
|
| 127 |
+
async checkCacheStatuses(hashes) {
|
| 128 |
try {
|
| 129 |
+
console.log('\n📡 DebridLink: Using Premiumize fallback for cache check');
|
| 130 |
+
const results = await this.#premiumizeService.checkCacheStatuses(hashes);
|
| 131 |
+
|
| 132 |
+
Object.values(results).forEach(result => {
|
| 133 |
+
if (result.cached) {
|
| 134 |
+
result.service = 'DebridLink';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
return results;
|
|
|
|
| 139 |
} catch (error) {
|
| 140 |
+
console.error('Cache check failed:', error);
|
| 141 |
+
return {};
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
+
#extractMediaInfo(magnetLink) {
|
| 146 |
try {
|
| 147 |
+
const decodedName = decodeURIComponent(magnetLink.match(/dn=([^&]+)/)?.[1] || '');
|
| 148 |
+
console.log('Decoded magnet name:', decodedName);
|
| 149 |
+
|
| 150 |
+
// Check if it's a series by looking for episode patterns
|
| 151 |
+
const seasonEpMatch = decodedName.match(/S(\d{1,2})(?:E|x)(\d{1,2})/i) ||
|
| 152 |
+
decodedName.match(/Season[. ](\d{1,2})[. ]Episode[. ](\d{1,2})/i);
|
| 153 |
+
|
| 154 |
+
if (seasonEpMatch) {
|
| 155 |
+
const season = parseInt(seasonEpMatch[1]);
|
| 156 |
+
const episode = parseInt(seasonEpMatch[2]);
|
| 157 |
+
console.log(`Detected TV series - Season ${season}, Episode ${episode}`);
|
| 158 |
+
return {
|
| 159 |
+
type: 'series',
|
| 160 |
+
season: season,
|
| 161 |
+
episode: episode
|
| 162 |
};
|
| 163 |
}
|
| 164 |
+
|
| 165 |
+
// Look for season/episode in path structure
|
| 166 |
+
if (decodedName.includes('/')) {
|
| 167 |
+
const parts = decodedName.split('/');
|
| 168 |
+
const seasonMatch = parts.find(p => p.match(/Season[. ]?\d+/i));
|
| 169 |
+
if (seasonMatch) {
|
| 170 |
+
const season = parseInt(seasonMatch.match(/\d+/)[0]);
|
| 171 |
+
// Look for episode number in filename
|
| 172 |
+
const lastPart = parts[parts.length - 1];
|
| 173 |
+
const episodeMatch = lastPart.match(/(?:E|Episode[. ]?)(\d+)/i);
|
| 174 |
+
if (episodeMatch) {
|
| 175 |
+
const episode = parseInt(episodeMatch[1]);
|
| 176 |
+
return {
|
| 177 |
+
type: 'series',
|
| 178 |
+
season: season,
|
| 179 |
+
episode: episode
|
| 180 |
+
};
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
}
|
| 184 |
+
|
| 185 |
+
// Look for other series indicators
|
| 186 |
+
if (decodedName.match(/Season \d+|Complete Series|TV Series|Episodes?/i)) {
|
| 187 |
+
return { type: 'series' };
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
return { type: 'movie' };
|
| 191 |
+
} catch (error) {
|
| 192 |
+
console.error('Error extracting media info:', error);
|
| 193 |
+
return { type: 'movie' };
|
| 194 |
}
|
| 195 |
}
|
| 196 |
|
| 197 |
async getStreamUrl(magnetLink) {
|
| 198 |
try {
|
| 199 |
+
console.log('\n📥 Using DebridLink to process magnet:', magnetLink.substring(0, 100) + '...');
|
| 200 |
+
|
| 201 |
const data = await this.#request('POST', '/seedbox/add', {
|
| 202 |
body: JSON.stringify({
|
| 203 |
url: magnetLink,
|
|
|
|
| 205 |
})
|
| 206 |
});
|
| 207 |
|
| 208 |
+
console.log('Seedbox add response:', data);
|
| 209 |
+
|
| 210 |
+
// Extract potential media info from magnet title
|
| 211 |
+
const mediaInfo = this.#extractMediaInfo(magnetLink);
|
| 212 |
+
console.log('Extracted media info:', mediaInfo);
|
| 213 |
+
|
| 214 |
+
const bestFile = findBestFile(
|
| 215 |
+
data.files,
|
| 216 |
+
mediaInfo.type,
|
| 217 |
+
mediaInfo
|
| 218 |
+
);
|
| 219 |
+
|
| 220 |
+
if (!bestFile) {
|
| 221 |
+
console.error('No suitable video file found');
|
| 222 |
+
throw new Error('No suitable video file found');
|
| 223 |
+
}
|
| 224 |
|
| 225 |
+
console.log('Selected file for streaming:', bestFile.name);
|
| 226 |
+
return bestFile.downloadUrl;
|
| 227 |
} catch (error) {
|
| 228 |
console.error('❌ Failed to get stream URL:', error);
|
| 229 |
throw error;
|
| 230 |
}
|
| 231 |
}
|
| 232 |
+
|
| 233 |
+
async #request(method, path, opts = {}) {
|
| 234 |
+
try {
|
| 235 |
+
const url = `https://debrid-link.com/api/v2${path}`;
|
| 236 |
+
opts = {
|
| 237 |
+
method,
|
| 238 |
+
headers: {
|
| 239 |
+
'Authorization': `Bearer ${this.getKey()}`,
|
| 240 |
+
'Content-Type': 'application/json'
|
| 241 |
+
},
|
| 242 |
+
...opts
|
| 243 |
+
};
|
| 244 |
+
|
| 245 |
+
const res = await fetch(url, opts);
|
| 246 |
+
const data = await res.json();
|
| 247 |
+
|
| 248 |
+
if (!data.success) {
|
| 249 |
+
throw new Error(data.error === 'badToken' ? ERROR.INVALID_API_KEY : `API Error: ${data.error}`);
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
return data.value;
|
| 253 |
+
} catch (error) {
|
| 254 |
+
console.error('Request failed:', error);
|
| 255 |
+
throw error;
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
}
|
| 259 |
|
| 260 |
class Premiumize extends BaseDebrid {
|
| 261 |
#apiUrl = 'https://www.premiumize.me/api';
|
| 262 |
+
#batchSize = 99;
|
| 263 |
|
| 264 |
constructor(apiKey) {
|
| 265 |
super(apiKey, 'pr');
|
|
|
|
| 269 |
return apiKey.startsWith('pr=');
|
| 270 |
}
|
| 271 |
|
| 272 |
+
#extractMediaInfo(magnetLink) {
|
| 273 |
+
try {
|
| 274 |
+
const decodedName = decodeURIComponent(magnetLink.match(/dn=([^&]+)/)?.[1] || '');
|
| 275 |
+
console.log('Decoded magnet name:', decodedName);
|
| 276 |
+
|
| 277 |
+
// Check if it's a series by looking for episode patterns
|
| 278 |
+
const seasonEpMatch = decodedName.match(/S(\d{1,2})(?:E|x)(\d{1,2})/i) ||
|
| 279 |
+
decodedName.match(/Season[. ](\d{1,2})[. ]Episode[. ](\d{1,2})/i);
|
| 280 |
+
|
| 281 |
+
if (seasonEpMatch) {
|
| 282 |
+
const season = parseInt(seasonEpMatch[1]);
|
| 283 |
+
const episode = parseInt(seasonEpMatch[2]);
|
| 284 |
+
console.log(`Detected TV series - Season ${season}, Episode ${episode}`);
|
| 285 |
+
return {
|
| 286 |
+
type: 'series',
|
| 287 |
+
season: season,
|
| 288 |
+
episode: episode
|
| 289 |
+
};
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
// Look for season/episode in path structure
|
| 293 |
+
if (decodedName.includes('/')) {
|
| 294 |
+
const parts = decodedName.split('/');
|
| 295 |
+
const seasonMatch = parts.find(p => p.match(/Season[. ]?\d+/i));
|
| 296 |
+
if (seasonMatch) {
|
| 297 |
+
const season = parseInt(seasonMatch.match(/\d+/)[0]);
|
| 298 |
+
// Look for episode number in filename
|
| 299 |
+
const lastPart = parts[parts.length - 1];
|
| 300 |
+
const episodeMatch = lastPart.match(/(?:E|Episode[. ]?)(\d+)/i);
|
| 301 |
+
if (episodeMatch) {
|
| 302 |
+
const episode = parseInt(episodeMatch[1]);
|
| 303 |
+
return {
|
| 304 |
+
type: 'series',
|
| 305 |
+
season: season,
|
| 306 |
+
episode: episode
|
| 307 |
+
};
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
// Look for other series indicators
|
| 313 |
+
if (decodedName.match(/Season \d+|Complete Series|TV Series|Episodes?/i)) {
|
| 314 |
+
return { type: 'series' };
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
return { type: 'movie' };
|
| 318 |
+
} catch (error) {
|
| 319 |
+
console.error('Error extracting media info:', error);
|
| 320 |
+
return { type: 'movie' };
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
async makeRequest(method, path, opts = {}) {
|
| 325 |
const retries = 3;
|
| 326 |
let lastError;
|
| 327 |
|
| 328 |
for (let i = 0; i < retries; i++) {
|
| 329 |
try {
|
| 330 |
+
const url = `${this.#apiUrl}${path}`;
|
| 331 |
+
console.log(`\n🔷 Premiumize Request (Attempt ${i + 1}/${retries}):`, method, path);
|
| 332 |
+
if (opts.body) console.log('Request Body:', opts.body);
|
| 333 |
+
|
| 334 |
const controller = new AbortController();
|
| 335 |
const timeout = setTimeout(() => controller.abort(), 30000);
|
| 336 |
|
| 337 |
+
if (method === 'POST' && opts.body instanceof FormData) {
|
| 338 |
+
opts.body.append('apikey', this.getKey());
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
const finalUrl = method === 'GET'
|
| 342 |
+
? `${url}${url.includes('?') ? '&' : '?'}apikey=${this.getKey()}`
|
| 343 |
+
: url;
|
| 344 |
+
|
| 345 |
+
const startTime = Date.now();
|
| 346 |
+
const response = await fetch(finalUrl, {
|
| 347 |
...opts,
|
| 348 |
method,
|
| 349 |
signal: controller.signal
|
| 350 |
});
|
| 351 |
|
| 352 |
clearTimeout(timeout);
|
| 353 |
+
console.log(`Response Time: ${Date.now() - startTime}ms`);
|
| 354 |
+
console.log('Response Status:', response.status);
|
| 355 |
+
|
| 356 |
+
const data = await response.json();
|
| 357 |
+
console.log('Response Data:', data);
|
| 358 |
+
|
| 359 |
+
if (data.status === 'error') {
|
| 360 |
+
if (data.message === 'Invalid API key.') {
|
| 361 |
+
throw new Error(ERROR.INVALID_API_KEY);
|
| 362 |
+
}
|
| 363 |
+
throw new Error(`API Error: ${data.message}`);
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
return data;
|
| 367 |
+
|
| 368 |
} catch (error) {
|
| 369 |
console.log(`Attempt ${i + 1} failed:`, error.message);
|
| 370 |
lastError = error;
|
| 371 |
+
if (i < retries - 1) {
|
| 372 |
+
console.log('Retrying after 2 seconds...');
|
| 373 |
+
await new Promise(r => setTimeout(r, 2000));
|
| 374 |
+
}
|
| 375 |
}
|
| 376 |
}
|
| 377 |
|
|
|
|
| 381 |
async checkCacheStatuses(hashes) {
|
| 382 |
try {
|
| 383 |
console.log(`\n📡 Premiumize: Batch checking ${hashes.length} hashes`);
|
| 384 |
+
console.log('Processing in batches of', this.#batchSize);
|
|
|
|
| 385 |
|
| 386 |
+
const results = {};
|
| 387 |
+
const batches = [];
|
| 388 |
+
|
| 389 |
+
for (let i = 0; i < hashes.length; i += this.#batchSize) {
|
| 390 |
+
batches.push(hashes.slice(i, i + this.#batchSize));
|
| 391 |
}
|
| 392 |
|
| 393 |
+
console.log(`Split into ${batches.length} batches`);
|
| 394 |
+
|
| 395 |
+
for (let i = 0; i < batches.length; i++) {
|
| 396 |
+
const batch = batches[i];
|
| 397 |
+
console.log(`\nProcessing batch ${i + 1}/${batches.length} (${batch.length} hashes)`);
|
| 398 |
+
|
| 399 |
+
const params = new URLSearchParams();
|
| 400 |
+
batch.forEach(hash => params.append('items[]', hash));
|
| 401 |
+
|
| 402 |
+
const data = await this.makeRequest('GET', `/cache/check?${params}`);
|
| 403 |
+
|
| 404 |
+
batch.forEach((hash, index) => {
|
| 405 |
+
results[hash] = {
|
| 406 |
+
cached: data.response[index],
|
| 407 |
+
files: [],
|
| 408 |
+
fileCount: 0,
|
| 409 |
+
service: 'Premiumize'
|
| 410 |
+
};
|
| 411 |
+
});
|
| 412 |
+
|
| 413 |
+
if (i < batches.length - 1) {
|
| 414 |
+
await new Promise(resolve => setTimeout(resolve, 500));
|
| 415 |
+
}
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
const cachedCount = Object.values(results).filter(r => r.cached).length;
|
| 419 |
+
console.log(`\nPremiumize found ${cachedCount} cached torrents out of ${hashes.length}`);
|
| 420 |
+
|
| 421 |
return results;
|
| 422 |
+
|
| 423 |
} catch (error) {
|
| 424 |
console.error('Cache check failed:', error);
|
| 425 |
return {};
|
|
|
|
| 428 |
|
| 429 |
async getStreamUrl(magnetLink) {
|
| 430 |
try {
|
| 431 |
+
console.log('\n📥 Using Premiumize to process magnet:', magnetLink.substring(0, 100) + '...');
|
| 432 |
|
| 433 |
const body = new FormData();
|
| 434 |
+
body.append('src', magnetLink);
|
|
|
|
| 435 |
|
| 436 |
+
const data = await this.makeRequest('POST', '/transfer/directdl', { body });
|
|
|
|
|
|
|
| 437 |
|
| 438 |
+
// Extract potential media info from magnet title
|
| 439 |
+
const mediaInfo = this.#extractMediaInfo(magnetLink);
|
| 440 |
+
console.log('Extracted media info:', mediaInfo);
|
| 441 |
+
|
| 442 |
+
const bestFile = findBestFile(
|
| 443 |
+
data.content,
|
| 444 |
+
mediaInfo.type,
|
| 445 |
+
mediaInfo
|
| 446 |
+
);
|
| 447 |
+
|
| 448 |
+
if (!bestFile) {
|
| 449 |
+
console.error('No suitable video file found');
|
| 450 |
+
throw new Error('No suitable video file found');
|
| 451 |
}
|
| 452 |
|
| 453 |
+
console.log('Selected file for streaming:', bestFile.path);
|
| 454 |
+
return bestFile.stream_link || bestFile.link;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
} catch (error) {
|
| 456 |
console.error('❌ Failed to get stream URL:', error);
|
| 457 |
throw error;
|
|
|
|
| 460 |
}
|
| 461 |
|
| 462 |
export function getDebridServices(apiKeys) {
|
| 463 |
+
console.log('\n🔐 Initializing debrid services with keys:', apiKeys);
|
| 464 |
const services = [];
|
| 465 |
|
| 466 |
for (const key of apiKeys.split(',')) {
|
| 467 |
if (DebridLink.canHandle(key)) {
|
| 468 |
+
console.log('Adding DebridLink service');
|
| 469 |
services.push(new DebridLink(key));
|
| 470 |
} else if (Premiumize.canHandle(key)) {
|
| 471 |
+
console.log('Adding Premiumize service');
|
| 472 |
services.push(new Premiumize(key));
|
| 473 |
+
} else {
|
| 474 |
+
console.log('Unknown service key format:', key);
|
| 475 |
}
|
| 476 |
}
|
| 477 |
|
| 478 |
+
console.log(`Initialized ${services.length} debrid services`);
|
| 479 |
return services;
|
| 480 |
}
|
| 481 |
|
| 482 |
+
export { DebridLink, Premiumize };
|