Skip to content

Instantly share code, notes, and snippets.

@oltodosel
Created November 27, 2019 16:26
Show Gist options
  • Save oltodosel/566e051191f3a58b905db2cc6980656f to your computer and use it in GitHub Desktop.
Save oltodosel/566e051191f3a58b905db2cc6980656f to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import json
import requests
import os
import time
# plugin for qBittorrent to filter out files in torrents by name or extension
# Enable web-interface.
# Check "Bypass authentication for localhost"
url = 'http://127.0.0.1:8080/'
torrent_hashes = '''
65c5d8a71a7b824acf39232f8068647cdaaff159
6b887370d8b52925d436d1bfec040df84c8dd645
'''
# ignorecase
block_extensions = ['psd']
# ignorecase # not in dir names
block_words_in_filenames = []
########################################################
if url[-1] != '/':
url += '/'
for torrent_hash in [ x.strip() for x in torrent_hashes.split('\n') if len(x.strip()) != 0 ]:
data = requests.get(url + 'api/v2/torrents/files?hash=%s' % torrent_hash).text
data = json.loads(data)
for file_id, file_data in enumerate(data):
for ext in block_extensions:
if file_data['name'].lower()[-len(ext):] == ext.lower():
print(file_data['name'])
r = requests.post(url + 'api/v2/torrents/filePrio',
data = {'hash' : torrent_hash, 'id' : str(file_id), 'priority' : '0'})
# without pauses qBittorent can choke on requests
time.sleep(0.05)
for word in block_words_in_filenames:
if word.lower() in file_data['name'].rsplit('/', 1)[1].lower():
print(file_data['name'])
r = requests.post(url + 'api/v2/torrents/filePrio',
data = {'hash' : torrent_hash, 'id' : str(file_id), 'priority' : '0'})
# without pauses qBittorent can choke on requests
time.sleep(0.05)
@oltodosel
Copy link
Author

@Unix-Kernel
Copy link

So where below the "for ext in block_extensions:" of that small portion of the code do I enter three different types of extensions that I want blocked/filtered-out?
How do I run this from the "WEB-UI" of QBT?

@oltodosel
Copy link
Author

You do not edit the code there if you don't know what you're doing.
block_extensions = ['psd', 'zzz'] <- in here are the extensions. Line 21.
You do not run it from the web-ui. You run it from the command line python qbittorrent_plugin_filter_files.py

@Unix-Kernel
Copy link

Python36-32>python qbittorrent_plugin_filter_files.py
Traceback (most recent call last):
File "qbittorrent_plugin_filter_files.py", line 4, in
import requests
ModuleNotFoundError: No module named 'requests'

@Unix-Kernel
Copy link

I used pythonw instead and there were no errors, however, nothing happens.

@oltodosel
Copy link
Author

Install module named requests
sudo pacman -S python-requests something like that.

@oltodosel
Copy link
Author

I've no idea what's pythonw. If the script works, files of torrents with given extensions should be unchecked.
I won't give you step-by-step instructions what to do and stop responding. Tough luck. Forget my script and uncheck unneeded files manually.

@Unix-Kernel
Copy link

i have already 'insalled' pip and 'requests', but before i install your script, how/where do insert the three extensions?

@oltodosel
Copy link
Author

block_extensions = ['psd', 'zzz'] <- in here are the extensions. Line 21

This is really my last reply.

@Unix-Kernel
Copy link

well, unfortunately it did not work:

Traceback (most recent call last):
File "qbittorrent_plugin_filter_files.py", line 34, in
data = json.loads(data)
File \Python\Python36-32\lib\json_init_.py", line 354, in loads
return _default_decoder.decode(s)
File \Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File \Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@Bangaio65
Copy link

@linuxlad Did you edit the torrent_hashes variable? If you have only one hash put it between single quotes, such as torrent_hashes= 'abc'

@oltodosel Thanks for the script, I used it as a base to write my own. One thing to note is that you can specify multiple file ids by separating them with |. Unfortunately I've run into bugs with v4.2.1 so I had to revert to 3.3.16, where this is not possible.

@Unix-Kernel
Copy link

@Bangaio65,
No, I have several different hashes but I never made it that far since I was unable to make the script work. In all the excitement to add this script, I did not consider the possibility that the filtering would create incomplete part files for every torrent so I would have had to do exactly what I ended up doing which was to create an executable to handle three file extensions(two by name and one by extension) and set the client option to silently execute it immediately upon the completion of each torrent. So this prevents any post manual clean-up or accumulation of junk files. If the regex 'either' symbol requires v4+ then it would not have worked for me either since I'm on 3.6.

@talanov
Copy link

talanov commented Aug 6, 2023

Hey, for those who still need it: I've made some updates to the script, including automatically fetching the torrent list and some other QoL improvements: https://gist.github.com/talanov/29bacef1ba1cb7aa89caa4c0bc9cb9bd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment