Skip to content

Instantly share code, notes, and snippets.

@shirohako
Last active September 22, 2022 05:20
Show Gist options
  • Save shirohako/d0bd55d5b6f4408e2ea08a63dd52cb86 to your computer and use it in GitHub Desktop.
Save shirohako/d0bd55d5b6f4408e2ea08a63dd52cb86 to your computer and use it in GitHub Desktop.
Export qBittorrent torrents and sort by categories / readable title
import qbittorrentapi
import os.path
from os import path
from shutil import copyfile
# Export qBittorrent torrents and sort by categories / readable title
# 将 hash 命名的种子文件, 重命名为种子标题, 并按分类导出
# 需要先安装 qbittorrent-api, 并配置 host / webapi 用户名&密码
# 需要指定存放种子的文件夹 source
# pip install qbittorrent-api
# instantiate a Client using the appropriate WebUI configuration
qbt_client = qbittorrentapi.Client(host='192.168.50.101:2017', username='admin', password='')
# the Client will automatically acquire/maintain a logged in state in line with any request.
# therefore, this is not necessary; however, you many want to test the provided login credentials.
try:
qbt_client.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(e)
# source 存放种子的目录 destination 导出的目录
# Where does qBittorrent save its settings?
# https://github.com/qbittorrent/qBittorrent/wiki/Frequently-Asked-Questions#Where_does_qBittorrent_save_its_settings
# Windows: source='C:\\Users\\user\\AppData\\Local\\qBittorrent\\BT_backup'
# GUN Linux: source='~/.local/share/data/qBittorrent/BT_backup'
# MacOS: source='~/Library/ApplicationSupport/qBittorrent/BT_backup'
source='./BT_backup/'
destination='./qBittorrent_Backup/'
print(f'qBittorrent: {qbt_client.app.version}')
print(f'qBittorrent Web API: {qbt_client.app.web_api_version}')
print(f'-----------------')
categories=qbt_client.torrents_categories() #分类
categories_len=len(categories) #分类总数
print(f"一共{categories_len}个分类,正在列出所有分类:")
print(f'-----------------')
for idx, c in enumerate(categories.values()):
print(f'Category#{idx} -> Name: {c.name}, savePath: {c.savePath}')
print(f'-----------------')
print("正在创建分类应对目录")
if path.exists(destination):
print("目标已存在,跳过")
else:
print("目标不存在,正在创建")
os.mkdir(destination, 0o666)
print("正在创建子目录")
for c in categories.values():
if path.exists(destination+str(c.name)):
print(f'{c.name} 目标已存在, 跳过')
else:
os.mkdir(destination+str(c.name), 0o666)
print(f'-----------------')
print(f'开始导出')
#遍历所有分类
for c in categories:
#得到该分类下所有种子
torrent_list = qbt_client.torrents.info(category=c)
print(f"正在导出分类{c},共计{len(torrent_list)}个种子")
#遍历分类下种子
for torrent in torrent_list:
src=os.path.join(source,torrent.hash+'.torrent')
if path.exists(src):
#复制对应hash的文件,并重命名为种子标题
dst=os.path.join(destination,c,torrent.name+'.torrent')
copyfile(src,dst)
else:
#没有找到文件时候
print(f"⚠没有找到{torrent.name}的种子文件哦!QvQ")
print(f"没找到的源文件是:{src}")
print(f"导出分类{c}已结束")
print("导出完成!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment