File size: 12,099 Bytes
215707b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | // torbox.js
import { ERROR } from './const.js';
import { BaseDebrid, findBestFile } from './base-debrid.js';
export class TorBox extends BaseDebrid {
#apiUrl = 'https://api.torbox.app/v1/api';
constructor(apiKey) {
super(apiKey, 'tb');
}
static canHandle(apiKey) {
return apiKey.startsWith('tb=');
}
async checkCacheStatuses(hashes) {
try {
console.log(`\n📡 TorBox: Checking ${hashes.length} hashes`);
const results = {};
// Process hashes in batches of 50
const BATCH_SIZE = 50;
for (let i = 0; i < hashes.length; i += BATCH_SIZE) {
const batchHashes = hashes.slice(i, i + BATCH_SIZE);
console.log(`\nProcessing batch of ${batchHashes.length} hashes`);
// Join hashes with commas for API request
const hashParams = batchHashes.join(',');
const url = `${this.#apiUrl}/torrents/checkcached?hash=${hashParams}&format=list&list_files=true`;
console.log('Requesting URL:', url);
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.getKey()}`
}
});
// Log raw response status
console.log(`Response status: ${response.status} ${response.statusText}`);
// Get the raw response text for debugging
const responseText = await response.text();
console.log('Raw response text:', responseText);
// Try to parse the response
let responseData;
try {
responseData = JSON.parse(responseText);
// Log the full response structure for debugging
console.log('Parsed response data:', JSON.stringify(responseData, null, 2));
} catch (error) {
console.error('Failed to parse response:', error);
console.log('Invalid JSON response:', responseText.substring(0, 500) + (responseText.length > 500 ? '...' : ''));
continue;
}
if (!response.ok) {
if (responseData?.error === 'Invalid API key') {
throw new Error(ERROR.INVALID_API_KEY);
}
console.error('Error checking batch:', responseData);
continue;
}
// Debug: Log the structure of the response
console.log('Response success:', responseData.success);
if (responseData.data) {
console.log('Data length:', Array.isArray(responseData.data) ? responseData.data.length : 'not an array');
if (Array.isArray(responseData.data) && responseData.data.length > 0) {
// Log sample of first item
console.log('First item sample:', JSON.stringify(responseData.data[0], null, 2));
}
}
if (responseData.success && Array.isArray(responseData.data)) {
// Process each torrent in the response
responseData.data.forEach(torrent => {
if (torrent && torrent.hash) {
console.log('Found cached torrent with hash:', torrent.hash);
const hash = torrent.hash.toLowerCase();
results[hash] = {
cached: true,
files: torrent.files || [],
name: torrent.name || '',
size: torrent.size || 0,
torrentId: torrent.id,
service: 'TorBox' // Mark as TorBox service
};
}
});
} else {
console.log('No cached torrents found in this batch or unexpected response format');
}
// Set uncached status for hashes not in response
batchHashes.forEach(hash => {
const hashLower = hash.toLowerCase();
if (!results[hashLower]) {
results[hashLower] = {
cached: false,
files: [],
service: 'TorBox' // Still mark service even if uncached
};
}
});
if (i + BATCH_SIZE < hashes.length) {
await new Promise(resolve => setTimeout(resolve, 100));
}
}
const cachedCount = Object.values(results).filter(r => r.cached).length;
console.log(`\nTorBox found ${cachedCount} cached torrents out of ${hashes.length}`);
// Print a sample of results for debugging
const resultsSample = Object.entries(results).slice(0, 3);
console.log('Sample of results:', JSON.stringify(resultsSample, null, 2));
return results;
} catch (error) {
console.error('Cache check failed:', error);
return {};
}
}
async getStreamUrl(magnetLink) {
try {
console.log('\n📥 Using TorBox to process magnet:', magnetLink.substring(0, 100) + '...');
// Extract info hash from magnet link
const hash = magnetLink.match(/btih:([a-f0-9]+)/i)?.[1]?.toLowerCase();
if (!hash) {
throw new Error('Invalid magnet link');
}
console.log('Checking existing torrents...');
// First check if torrent already exists
const listResponse = await fetch(
`${this.#apiUrl}/torrents/mylist?bypass_cache=true`,
{
method: 'GET',
headers: {
'Authorization': `Bearer ${this.getKey()}`
}
}
);
// Log raw response for debugging
const listResponseText = await listResponse.text();
console.log('Raw mylist response:', listResponseText);
let listData;
try {
listData = JSON.parse(listResponseText);
console.log('Parsed mylist data:', JSON.stringify(listData, null, 2));
} catch (error) {
console.error('Failed to parse mylist response:', error);
throw new Error('Invalid response from TorBox API');
}
let torrentId;
// Check if torrent already exists
if (listData.success && Array.isArray(listData.data)) {
const existingTorrent = listData.data.find(t => t.hash.toLowerCase() === hash);
if (existingTorrent) {
console.log('Found existing torrent:', existingTorrent.id);
torrentId = existingTorrent.id;
}
}
// If torrent doesn't exist, create it
if (!torrentId) {
console.log('Creating new torrent...');
const formData = new FormData();
formData.append('magnet', `magnet:?xt=urn:btih:${hash}`);
const createResponse = await fetch(
`${this.#apiUrl}/torrents/createtorrent`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${this.getKey()}`
},
body: formData
}
);
const createResponseText = await createResponse.text();
console.log('Raw create response:', createResponseText);
let createData;
try {
createData = JSON.parse(createResponseText);
console.log('Parsed create data:', JSON.stringify(createData, null, 2));
} catch (error) {
console.error('Failed to parse create response:', error);
throw new Error('Invalid response from TorBox API');
}
if (!createResponse.ok || !createData.success || !createData.data?.torrent_id) {
throw new Error(`Failed to create torrent: ${createData.error || createData.detail || 'Unknown error'}`);
}
torrentId = createData.data.torrent_id;
}
// Get file list to find best file
const filesResponse = await fetch(
`${this.#apiUrl}/torrents/checkcached?hash=${hash}&format=list&list_files=true`,
{
method: 'GET',
headers: {
'Authorization': `Bearer ${this.getKey()}`
}
}
);
const filesResponseText = await filesResponse.text();
console.log('Raw files response:', filesResponseText);
let filesData;
try {
filesData = JSON.parse(filesResponseText);
console.log('Parsed files data:', JSON.stringify(filesData, null, 2));
} catch (error) {
console.error('Failed to parse files response:', error);
throw new Error('Invalid response from TorBox API');
}
if (!filesResponse.ok || !filesData.success || !filesData.data?.[0]?.files) {
throw new Error('Failed to get file list');
}
const files = filesData.data[0].files.map((file, index) => ({
id: index, // Use index as file ID
name: file.name.split('/')[1] || file.name, // Remove folder prefix
size: file.size,
path: file.name
}));
// Find best file
const mediaInfo = this.extractMediaInfo(magnetLink);
console.log('Media info:', mediaInfo);
const bestFile = findBestFile(files, mediaInfo.type);
if (!bestFile) {
throw new Error('No suitable video file found');
}
console.log('Selected file:', bestFile.name);
// Get download link
const dlUrl = `${this.#apiUrl}/torrents/requestdl?token=${this.getKey()}&torrent_id=${torrentId}&file_id=${bestFile.id}&zip=false`;
console.log('Requesting download URL:', dlUrl);
const dlResponse = await fetch(dlUrl, {
method: 'GET'
});
const dlResponseText = await dlResponse.text();
console.log('Raw download response:', dlResponseText);
let dlData;
try {
dlData = JSON.parse(dlResponseText);
console.log('Parsed download data:', JSON.stringify(dlData, null, 2));
} catch (error) {
console.error('Failed to parse download response:', error);
throw new Error('Invalid response from TorBox API');
}
if (!dlResponse.ok || !dlData.success || !dlData.data) {
throw new Error(`Failed to get download URL: ${dlData.error || dlData.detail || 'Unknown error'}`);
}
return dlData.data;
} catch (error) {
console.error('❌ Failed to get stream URL:', error);
throw error;
}
}
} |