#!/usr/bin/env python3 # usage: # depends: `pip install qbittorrent-api` # step1: set `/path/to/move_bt_file.py "%C" "%I" "%F"` to qbittorent "Run external program on torrent completion" # step2: create directory for single file torrent file prefix in `DIST_PATH` # ps: only support single file torrent import os import sys import qbittorrentapi HOST = '127.0.0.1' PORT = 8080 USERNAME = 'admin' PASSWORD = '' DIST_PATH = '/home/database/Download/' # instantiate a Client using the appropriate WebUI configuration qbt_client = qbittorrentapi.Client(host=HOST, port=PORT, username=USERNAME, password=PASSWORD) # the Client will automatically acquire/maintain a logged in state in line with any request. # therefore, this is not necessary; however, you may want to test the provided login credentials. def list_path(p): res = [] for home, dirs, files in os.walk(p): for dir in dirs: res.append(dir) return res if __name__ == '__main__': num_file_str = sys.argv[1] # %C torrent_hash = sys.argv[2] # %I file_path = sys.argv[3] # %F if not num_file_str or not torrent_hash: print("ars not vaild") exit(0) num_file = int(num_file_str) if num_file != 1: print("not a single file torrent") exit(0) file_name = os.path.basename(file_path) try: qbt_client.auth_log_in() except qbittorrentapi.LoginFailed as e: print(e) exit(0) dist_paths = list_path(DIST_PATH) for p in dist_paths: if file_name.startswith(p): qbt_client.torrents_set_location(os.path.join(DIST_PATH, p), torrent_hashes=torrent_hash) print("set location %s", os.path.join(DIST_PATH, p)) break exit(0)