Created
December 7, 2024 07:05
-
-
Save maksii/3d615216a18036c55c8ca162708f57fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function () { | |
// Helper function to download JSON as a file | |
function downloadJSON(data, filename) { | |
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); | |
const url = URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.href = url; | |
a.download = filename; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
URL.revokeObjectURL(url); | |
} | |
// Fetch list of torrents | |
async function fetchTorrents() { | |
const response = await fetch(`${window.location.origin}/qbittorrent/api/v2/torrents/info`, { | |
method: 'GET' | |
}); | |
if (!response.ok) { | |
throw new Error('Failed to fetch torrents.'); | |
} | |
return response.json(); // Return torrents as JSON | |
} | |
// Fetch files for a specific torrent | |
async function fetchTorrentFiles(hash) { | |
const response = await fetch(`${window.location.origin}/qbittorrent/api/v2/torrents/files?hash=${hash}`, { | |
method: 'GET' | |
}); | |
if (!response.ok) { | |
throw new Error(`Failed to fetch files for torrent with hash: ${hash}`); | |
} | |
return response.json(); // Return files as JSON | |
} | |
// Main logic | |
try { | |
// Fetch all torrents | |
const torrents = await fetchTorrents(); | |
// Prepare data for saving | |
const torrentsData = []; | |
for (const torrent of torrents) { | |
const { name, size, progress, category, hash, comment } = torrent; | |
const files = await fetchTorrentFiles(hash).then((fileList) => | |
fileList.map((file) => file.name) | |
); | |
torrentsData.push({ name, size, progress, category, hash, comment, files }); | |
} | |
// Save and download the data as torrents.json | |
downloadJSON(torrentsData, 'torrents.json'); | |
console.log('Torrent data saved as torrents.json'); | |
} catch (error) { | |
console.error('Error:', error.message); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment