Created
August 11, 2023 05:59
-
-
Save xxiz/c7a02619b2045d1eb8a2a12b9896a8d5 to your computer and use it in GitHub Desktop.
add torrents directly from nyaa.si to WebUI qBittorent instance
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
// ==UserScript== | |
// @name Nyaa to qBittorrent | |
// @namespace https://violentmonkey.github.io/ | |
// @version 1.0 | |
// @description Add a torrent to a qBittorrent instance via WebUI API | |
// @match https://nyaa.si/* | |
// @grant GM_xmlhttpRequest | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const qBittorrent = { | |
host: 'http://localhost:3000', | |
username: 'admin', | |
password: 'johnsmith' | |
}; | |
refresh(); | |
document.addEventListener('click', async function (e) { | |
if (e.target.classList.contains('fa-magnet')) { | |
e.preventDefault(); | |
const magnet = e.target.parentNode.href; | |
await addMagnet(magnet); | |
} | |
}); | |
function GM_promise(options) { | |
return new Promise((resolve, reject) => { | |
options.onload = resolve; | |
options.onerror = reject; | |
GM_xmlhttpRequest(options); | |
}); | |
} | |
async function authenticate() { | |
try { | |
let response = await GM_promise({ | |
method: 'POST', | |
url: `${qBittorrent.host}/api/v2/auth/login`, | |
headers: { | |
'Referer': qBittorrent.host, | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
data: `username=${qBittorrent.username}&password=${qBittorrent.password}` | |
}); | |
let sidMatch = response.responseHeaders.match(/SID=(.*?);/); | |
let sid = sidMatch ? sidMatch[1] : null; | |
if (!sid) { | |
console.error("Session already exists"); | |
debug(response); | |
return null; | |
} | |
console.log("Authenticated with qBittorrent, SID: " + sid); | |
return sid; | |
} catch (error) { | |
console.error(error); | |
console.error("Failed to authenticate with qBittorrent"); | |
return; | |
} | |
} | |
async function refresh() { | |
let sid = await GM_getValue("SID"); | |
if (!sid) { | |
sid = await authenticate(); | |
GM_setValue("SID", sid); | |
} else { | |
let isValid = await validate(sid); | |
if (!isValid) { | |
console.log("SID is invalid, authenticating with qBittorrent"); | |
sid = await authenticate(); | |
GM_setValue("SID", sid); | |
} | |
} | |
} | |
async function validate(SID) { | |
try { | |
let response = await GM_promise({ | |
method: 'GET', | |
url: `${qBittorrent.host}/api/v2/torrents/info`, | |
headers: { | |
'Cookie': `SID=${SID}` | |
} | |
}); | |
if (response.status == 200) { | |
return true; | |
} else { | |
console.error("SID " + SID + " is invalid"); | |
console.error(response); | |
return false; | |
} | |
} catch (error) { | |
console.error("SID is invalid"); | |
console.error(error); | |
return false; | |
} | |
} | |
async function addMagnet(magnet) { | |
let sid = await GM_getValue("SID"); | |
try { | |
let response = await GM_promise({ | |
method: 'POST', | |
url: `${qBittorrent.host}/api/v2/torrents/add`, | |
headers: { | |
'Cookie': `SID=${sid}`, | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
data: `urls=${magnet}` | |
}); | |
console.log("Added magnet to qBittorrent"); | |
} catch (error) { | |
console.error("Failed to add magnet to qBittorrent"); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment