Skip to content

Instantly share code, notes, and snippets.

@maksii
Last active November 27, 2024 15:31
Show Gist options
  • Save maksii/feceeb5042c88f38c994948e9835ee59 to your computer and use it in GitHub Desktop.
Save maksii/feceeb5042c88f38c994948e9835ee59 to your computer and use it in GitHub Desktop.
(async function() {
const qbittorrentUrl = window.location.origin; // Automatically gets the base URL of the Web UI
const apiPath = '/api/v2';
const trackerToAdd = "NEW TRACKER URL HERE"; // Replace with the actual tracker URL
const specificTrackerToRemove = "OLD TRACKER URL HERE"; // What should be removed
// Helper function to make API requests
async function apiRequest(endpoint, method = 'GET', body = null) {
const options = {
method: method,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
};
if (body) {
options.body = new URLSearchParams(body).toString();
}
const response = await fetch(`${qbittorrentUrl}${apiPath}${endpoint}`, options);
if (!response.ok) {
console.error(`Error with API request to ${endpoint}:`, response.statusText);
}
return response.json().catch(() => null); // Return JSON if possible, otherwise null
}
// Step 1: Get all torrents
const torrents = await apiRequest('/torrents/info'); // change to if need category /torrents/info?category=utopia
if (!torrents) {
console.error("Failed to fetch torrent information.");
return;
}
// Step 2: Filter torrents by tag "utp"
//const utpTorrents = torrents.filter(torrent => torrent.tags.split(',').includes('UTP')); //uncomment this line if you need by tag
const utpTorrents = torrents;
console.log(`Found ${utpTorrents.length} torrents.`);
// Step 3: For each torrent, remove all trackers and add the new tracker
for (const torrent of utpTorrents) {
const hash = torrent.hash;
var toAdd = false;
// Step 3a: Remove only URL-based trackers (skip DHT, PeX, LSD)
const trackers = await apiRequest(`/torrents/trackers?hash=${hash}`);
if (trackers && trackers.length > 0) {
for (const tracker of trackers) {
const trackerUrl = tracker.url;
// Skip non-URL trackers (DHT, PeX, LSD)
if (!(trackerUrl.startsWith('http://') || trackerUrl.startsWith('https://'))) {
continue;
}
// If a specific tracker URL is provided, only remove it
if (specificTrackerToRemove) {
if (trackerUrl === specificTrackerToRemove) {
console.log(`Removing specific tracker: ${trackerUrl} for torrent: ${torrent.name}`);
await apiRequest('/torrents/removeTrackers', 'POST', {
hash: hash,
urls: trackerUrl
});
toAdd = true;
} else {
// console.log(`Skipping tracker: ${trackerUrl} (does not match specific URL) for torrent: ${torrent.name}`);
}
}
}
} else {
console.log(`No trackers found for torrent: ${torrent.name}`);
}
// Step 3b: Add the new tracker
if(!toAdd) continue;
console.log(`Adding tracker: ${trackerToAdd} to torrent: ${torrent.name}`);
await apiRequest('/torrents/addTrackers', 'POST', {
hash: hash,
urls: trackerToAdd
});
}
console.log("Operation completed.");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment