Forked from oltodosel/qbittorrent_plugin_filter_files.py
Last active
August 6, 2023 06:27
-
-
Save talanov/29bacef1ba1cb7aa89caa4c0bc9cb9bd to your computer and use it in GitHub Desktop.
qBittorrent Cleaning Script
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
#! /usr/bin/env python | |
import getopt | |
import sys | |
import requests | |
import json | |
import time | |
# ---------- NFO ---------- # | |
""" | |
qBittorrent Cleaning Script | |
This script filters out files in torrents by extension in qBittorrent. | |
Make sure to enable the web interface for this script to work properly. | |
Instructions: | |
1. Enable the qBittorrent web interface in the settings. | |
2. Obtain the necessary cookies (SID) either from Web Debug Tools or by calling a designated API. | |
2.1 (optional) curl -i --data 'username=admin&password=adminadmin' http://localhost:8080/api/v2/auth/login | |
3. Call this script. | |
Note: The web interface _must be_ accessible for the script to function correctly. | |
This script supports the following arguments: | |
--host or -h: Use this argument with an IP address or hostname. Example: --host 127.0.0.1 or -h my.torrent.server | |
--port or -p: Use this argument with a port number. Example: --port 8080 or -p 80 | |
--auth or -a: Use this argument with the SID (auth) cookie. Example: --auth "A/RAND/+RAND+c" or -a "SID=A/RAND/+RAND+c" | |
--word or -w: Use this argument with the words that need to be cleaned. Example: --word "Downloaded" or -w "rarbg" | |
--ext or -e: Use this argument with the extensions that need to be cleaned. Example: --ext "exe" --ext "nfo" or -e "exe" -e "nfo" Note: Defaults to 'nfo', 'exe', 'jpg', 'png', 'txt'. | |
--https or -s: Use this argument if you wish to call API via HTTPS. Example: --https or -s | |
./cleanqb.py --host 127.0.0.1 --port 8080 --auth "A/RAND/+RAND+c" --ext "exe" --ext "nfo" --word "rarbg" | |
""" | |
# ---------- Preparation ---------- # | |
host: str = "127.0.0.1" | |
port: str = "8080" | |
auth: str = "" | |
blockExtensions: list[str] = [] | |
blockWordsInFilenames: list[str] = [] # ignorecase # not in dir names | |
isHttps: bool = bool(False) | |
try: | |
arguments, values = getopt.getopt( | |
sys.argv[1:], | |
"h:p:a:e:w:s", | |
["host=", "port=", "auth=", "ext=", "word=", "https"] | |
) | |
for currentArgument, currentValue in arguments: | |
if currentArgument in ("-h", "--host"): | |
host = currentValue | |
elif currentArgument in ("-p", "--port"): | |
port = currentValue | |
elif currentArgument in ("-a", "--auth"): | |
auth = currentValue | |
if currentValue.startswith("SID="): | |
auth = currentValue[4:] | |
elif currentArgument in ("-e", "--ext"): | |
blockExtensions.append(currentValue) | |
elif currentArgument in ("-w", "--word"): | |
blockWordsInFilenames.append(currentValue) | |
elif currentArgument in ("-s", "--https"): | |
isHttps = bool(True) | |
except getopt.error as err: | |
raise ValueError(err) | |
url: str = "http" | |
if isHttps: | |
url += "s" | |
url += "://" + host + ":" + port + "/" | |
authorization = {'SID': auth} | |
if len(blockExtensions) == 0: | |
blockExtensions = ['nfo', 'exe', 'jpg', 'png', 'txt'] | |
if len(host) == 0: | |
raise ValueError("Host can't be empty.") | |
if len(port) == 0: | |
raise ValueError("Port can't be empty.") | |
print("Connecting to " + url + "...") | |
print("Will use this list: " + ', '.join(blockExtensions)) | |
# ---------- Getting File List ---------- # | |
class TorrentFile(object): | |
def __init__(self, hash): | |
self.hash = hash | |
def __str__(self): | |
return "{0}".format(self.hash) | |
torrentData = requests.get( | |
url + 'api/v2/torrents/info', | |
cookies=authorization | |
).text | |
if torrentData == "Forbidden": | |
raise ValueError("Cookie File is invalid. Set it like this: ./cleanqb.py --auth 'xxx'.") | |
torrentData = json.loads(torrentData) | |
# ---------- Getting Contents of each Torrent ---------- # | |
for torrentDataObject in torrentData: | |
torrentHash = TorrentFile(torrentDataObject['hash']) | |
data = requests.get( | |
url + 'api/v2/torrents/files?hash=%s' % torrentHash, | |
cookies=authorization | |
).text | |
data = json.loads(data) | |
for fileId, fileData in enumerate(data): | |
for ext in blockExtensions: | |
if fileData['name'].lower()[-len(ext):] == ext.lower(): | |
print(fileData['name']) | |
r = requests.post( | |
url + 'api/v2/torrents/filePrio', | |
data={'hash': torrentHash, 'id': str(fileId), 'priority': '0'}, | |
cookies=authorization | |
) | |
# Without pauses qBittorent may choke on requests | |
time.sleep(0.05) | |
for word in blockWordsInFilenames: | |
if word.lower() in fileData['name'].rsplit('/', 1)[1].lower(): | |
print(fileData['name']) | |
r = requests.post( | |
url + 'api/v2/torrents/filePrio', | |
data={'hash': torrentHash, 'id': str(fileId), 'priority': '0'}, | |
cookies=authorization | |
) | |
# Without pauses qBittorent may choke on requests | |
time.sleep(0.05) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment