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)
@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