#教程#qBittorrent + Rclone 自动上传脚本
闲着无聊写了个qb+Rclone自动上传到Google Drive的脚本
大佬们那么多无限容量Google Drive,不塞点东西浪费了
水平有限,写得有点烂,各位大佬见笑了
支持qBittorrent v3.2.0+
不支持v3.1.x
实测v4.2.0Beta1功能可用
其他版本未测,等大佬测试反馈!
配合RSS+磁盘配额控制的话,应该可以全自动,坐等大佬写
使用方法:
qB WEB UI 勾选“Torrent 完成时运行外部程序”
填上:
bash /root/qb_auto.sh "%N" "%F" "%R" "%D" "%C" "%Z" "%I"

自行配置好rclone即可
脚本:
qb_auto.sh
#!/bin/sh
torrent_name=$1
content_dir=$2
root_dir=$3
save_dir=$4
files_num=$5
torrent_size=$6
file_hash=$7
qb_version="4.2.0"#如4.0.4、4.1.9.1、4.2.0等,不支持3.1.X
qb_username="hostloc"#qb用户名
qb_password="hostloc.com"#qb密码
qb_web_url="https://hostloc.com"#QB web路径,可以填写本地http://localhost:8080
leeching_mode="true"#吸血模式 设为true 上传完后自动删除种子及数据 否则不删除,继续做种
log_dir="/root/qbauto"#日志目录
rclone_dest="gdrive"#rclone destination关键字 运行rclone config查看name字段即可
rclone_parallel="32"#rclone上传线程 默认4
auto_del_flag="rclone"#添加标签或者分类来标识已上传的种子 v4.0.4+版本添加标签“rclone”,低版本通过添加分类“rclone”标识
if [ ! -d ${log_dir} ]
then
mkdir -p ${log_dir}
fi
version=$(echo $qb_version | grep -P -o "([0-9]\.){2}[0-9]" | sed s/\\.//g)
function qb_login(){
if [ ${version} -gt 404 ]
then
qb_v="1"
cookie=$(curl -i --header "Referer: ${qb_web_url}" --data "username=${qb_username}&password=${qb_password}" "${qb_web_url}/api/v2/auth/login" | grep -P -o 'SID=\S{32}')
if [ -n ${cookie} ]
then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录成功!cookie:${cookie}" >> ${log_dir}/autodel.log
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录失败!" >> ${log_dir}/autodel.log
fi
elif [[ ${version} -le 404 && ${version} -ge 320 ]]
then
qb_v="2"
cookie=$(curl -i --header "Referer: ${qb_web_url}" --data "username=${qb_username}&password=${qb_password}" "${qb_web_url}/login" | grep -P -o 'SID=\S{32}')
if [ -n ${cookie} ]
then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录成功!cookie:${cookie}" >> ${log_dir}/autodel.log
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录失败" >> ${log_dir}/autodel.log
fi
elif [[ ${version} -ge 310 && ${version} -lt 320 ]]
then
qb_v="3"
echo "陈年老版本,请及时升级"
exit
else
qb_v="0"
exit
fi
}
function qb_del(){
if [ ${leeching_mode} == "true" ]
then
if [ ${qb_v} == "1" ]
then
curl "${qb_web_url}/api/v2/torrents/delete?hashes=${file_hash}&deleteFiles=true" --cookie ${cookie}
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 删除成功!种子名称:${torrent_name}" >> ${log_dir}/qb.log
elif [ ${qb_v} == "2" ]
then
curl -X POST -d "hashes=${file_hash}" "${qb_web_url}/command/deletePerm" --cookie ${cookie}
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 删除成功!种子文件:${torrent_name}" >> ${log_dir}/qb.log
echo "qb_v=${qb_v}" >> ${log_dir}/qb.log
fi
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 不自动删除已上传种子" >> ${log_dir}/qb.log
fi
}
function rclone_copy(){
if [ ${type} == "file" ]
then
rclone_copy_cmd=$(rclone -v copy --transfers ${rclone_parallel} --log-file ${log_dir}/qbauto_copy.log "${content_dir}" ${rclone_dest}:qbauto/)
elif [ ${type} == "dir" ]
then
rclone_copy_cmd=$(rclone -v copy --transfers ${rclone_parallel} --log-file ${log_dir}/qbauto_copy.log "${content_dir}"/ ${rclone_dest}:qbauto/"${torrent_name}")
fi
}
function qb_add_auto_del_tags(){
if [ ${qb_v} == "1" ]
then
curl -X POST -d "hashes=${file_hash}&tags=${auto_del_flag}" "${qb_web_url}/api/v2/torrents/addTags" --cookie "${cookie}"
elif [ ${qb_v} == "2" ]
then
curl -X POST -d "hashes=${file_hash}&category=${auto_del_flag}" "${qb_web_url}/command/setCategory" --cookie ${cookie}
else
echo "qb_v=${qb_v}" >> ${log_dir}/qb.log
fi
}
if [ -f "${content_dir}" ]
then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 类型:文件" >> ${log_dir}/qb.log
type="file"
rclone_copy
qb_login
qb_add_auto_del_tags
qb_del
elif [ -d "${content_dir}" ]
then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 类型:目录" >> ${log_dir}/qb.log
type="dir"
rclone_copy
qb_login
qb_add_auto_del_tags
qb_del
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 未知类型,取消上传" >> ${log_dir}/qb.log
fi
echo "种子名称:${torrent_name}" >> ${log_dir}/qb.log
echo "内容路径:${content_dir}" >> ${log_dir}/qb.log
echo "根目录:${root_dir}" >> ${log_dir}/qb.log
echo "保存路径:${save_dir}" >> ${log_dir}/qb.log
echo "文件数:${files_num}" >> ${log_dir}/qb.log
echo "文件大小:${torrent_size}Bytes" >> ${log_dir}/qb.log
echo "HASH:${file_hash}" >> ${log_dir}/qb.log
echo "Cookie:${cookie}" >> ${log_dir}/qb.log
echo -e "-------------------------------------------------------------\n" >> ${log_dir}/qb.log
ratio_mon.sh
#!/bin/sh
qb_version="4.2.0"
qb_username="hostloc"
qb_password="hostloc.com"
qb_web_url="http://localhost:8080"
log_dir="/root/qbauto"
expect_ratio=0.3
auto_del_flag="rclone"
if [ ! -d ${log_dir} ]
then
mkdir -p ${log_dir}
fi
version=$(echo ${qb_version} | grep -P -o "([0-9]\.){2}[0-9]" | sed s/\\.//g)
function qb_login(){
if [ ${version} -gt 404 ]
then
qb_v="1"
cookie=$(curl -i --header "Referer: ${qb_web_url}" --data "username=${qb_username}&password=${qb_password}" "${qb_web_url}/api/v2/auth/login" | grep -P -o 'SID=\S{32}')
if [ -n ${cookie} ]
then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录成功!cookie:${cookie}" >> ${log_dir}/autodel.log
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录失败!" >> ${log_dir}/autodel.log
fi
elif [[ ${version} -le 404 && ${version} -ge 320 ]]
then
qb_v="2"
cookie=$(curl -i --header "Referer: ${qb_web_url}" --data "username=${qb_username}&password=${qb_password}" "${qb_web_url}/login" | grep -P -o 'SID=\S{32}')
if [ -n ${cookie} ]
then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录成功!cookie:${cookie}" >> ${log_dir}/autodel.log
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 登录失败" >> ${log_dir}/autodel.log
fi
elif [[ ${version} -ge 310 && ${version} -lt 320 ]]
then
qb_v="3"
echo "陈年老版本,请及时升级"
exit
else
qb_v="0"
exit
fi
}
function qb_del(){
if [ ${qb_v} == "1" ]
then
curl "${qb_web_url}/api/v2/torrents/delete?hashes=$1&deleteFiles=true" --cookie ${cookie}
elif [ ${qb_v} == "2" ]
then
curl -X POST -d "hashes=$1" "${qb_web_url}/command/deletePerm" --cookie ${cookie}
else
echo "删除错误" >> ${log_dir}/autodel.log
echo "qb_v=${qb_v}" >> ${log_dir}/autodel.log
fi
}
function qb_get_status(){
qb_login
if [ ${qb_v} == "1" ]
then
completed_torrents_num=$(curl "${qb_web_url}/api/v2/torrents/info?filter=completed" --cookie "${cookie}" | jq '.[] | length' | wc -l)
for((i=0;i<${completed_torrents_num};i++));
do
del_flag=$(curl "${qb_web_url}/api/v2/torrents/info?filter=completed" --cookie "${cookie}" | jq ".[$i] | .tags" | sed s/\"//g | grep -P -o "${auto_del_flag}")
if [ -z "${del_flag}" ]
then
del_flag="null"
fi
if [ ${del_flag} == "${auto_del_flag}" ]
then
real_ratio=$(curl "${qb_web_url}/api/v2/torrents/info?filter=completed" --cookie "${cookie}" | jq ".[$i] | .ratio" | grep -P -o "^[0-9]\.[0-9]{2}")
if [ -z "${real_ratio}" ]
then
real_ratio=0
fi
del_hash=$(curl "${qb_web_url}/api/v2/torrents/info?filter=completed" --cookie "${cookie}" | jq ".[$i] | .hash" | sed s/\"//g)
if [ $(echo "${real_ratio} >= ${expect_ratio}"|bc) = 1 ]
then
del_hash=$(curl "${qb_web_url}/api/v2/torrents/info?filter=completed" --cookie "${cookie}" | jq ".[$i] | .hash" | sed s/\"//g)
qb_del ${del_hash}
echo "做种率${real_ratio}≥${expect_ratio},自动删除种子,种子Hash:${del_hash}" >> ${log_dir}/autodel.log
fi
fi
done
elif [ ${qb_v} == "2" ]
then
completed_torrents_num=$(curl "${qb_web_url}/query/torrents?filter=completed" --cookie "${cookie}" | jq '.[] | length' | wc -l)
for((i=0;i<${completed_torrents_num};i++));
do
del_flag=$(curl "${qb_web_url}/query/torrents?filter=completed" --cookie "${cookie}" | jq ".[$i] | .category" | sed s/\"//g)
if [ -z "${del_flag}" ]
then
del_flag="null"
fi
if [ ${del_flag} == "${auto_del_flag}" ]
then
real_ratio=$(curl "${qb_web_url}/query/torrents?filter=completed" --cookie "${cookie}" | jq ".[$i] | .ratio" | grep -P -o "^[0-9]\.[0-9]{2}")
if [ -z "${real_ratio}" ]
then
real_ratio=0
fi
del_hash=$(curl "${qb_web_url}/query/torrents?filter=completed" --cookie "${cookie}" | jq ".[$i] | .hash" | sed s/\"//g)
if [ $(echo "${real_ratio} >= ${expect_ratio}"|bc) = 1 ]
then
del_hash=$(curl "${qb_web_url}/query/torrents?filter=completed" --cookie "${cookie}" | jq ".[$i] | .hash" | sed s/\"//g)
qb_del ${del_hash}
echo "做种率${real_ratio}≥${expect_ratio},自动删除种子,种子Hash:${del_hash}" >> ${log_dir}/autodel.log
fi
fi
done
else
echo "获取错误" >> ${log_dir}/autodel.log
echo "qb_v=${qb_v}" >> ${log_dir}/autodel.log
fi
}
qb_get_status
其它说明:
通过添加标签或者分类,再添加定时任务,可以指定种子分享率达到期望值再删除数据,避免吸血
请自行安装jq
确保两个脚本的auto_del_flag变量一致,否则没办法删除分享率达标的种子
只吸血的同学只需要将qu_auto.sh的leeching_mode设为true即可
需要控制分享率的需要两个脚本
并添加监控任务:
*/1 * * * * bash /root/ratio_mon.sh
作者:xcy1020
查看原帖:https://www.hostloc.com/thread-612238-1-1.html



御坂
2021年7月27日 上午11:33
grep -P是什么参数