Last active
October 16, 2021 14:50
-
-
Save qinghon/61e3b95799211ba11b9e6b0ba285b2aa to your computer and use it in GitHub Desktop.
move single file torrent to path
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 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) |
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 python3 | |
# 整理已经下载好的torrent, | |
# usage: | |
# depends: `pip install qbittorrent-api` | |
# step1: create directory for single file torrent file prefix in `DIST_PATH` | |
# step2: python3 organize_torrents.py | |
# 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__': | |
try: | |
qbt_client.auth_log_in() | |
except qbittorrentapi.LoginFailed as e: | |
print(e) | |
exit(0) | |
dist_paths = list_path(DIST_PATH) | |
for torrent in qbt_client.torrents_info(): | |
if torrent['category'] == "new" and torrent['save_path'] == DIST_PATH: | |
if os.path.isfile(torrent['content_path']): | |
file_name = os.path.basename(torrent['content_path']) | |
torrent_hash = torrent['hash'] | |
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 {0}".format(os.path.join(DIST_PATH, p))) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment