Debian在docker下安装qbittorrent并配合rclone上传
因为在设置 qBittorrent 的时候发现有一个“Torrent 完成时运行外部程序”的设置,我就在想能不能用这个方法下载 PT 然后自动传到 Google Drive,一番寻找找到了方法,在此记录一下过程。
首先先在设置中的 Web UI 中把“对本地主机上的客户端跳过身份验证”勾上,我一开始没有勾上,获取不了 cookie 加不了标签。先勾上有备无患。
没有勾上获取不了 cookie
新建一个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="admin" #qb用户名
qb_password="adminadmin" #qb密码
qb_web_url="http://localhost:8080" #QB web路径
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给予权限
chmod +x qb_auto.shqB WEB UI 勾选“Torrent 完成时运行外部程序”,填上
bash /root/qb_auto.sh "%N" "%F" "%R" "%D" "%C" "%Z" "%I"新建:ratio_mon.sh 写入
#!/bin/sh
qb_version="4.2.0" #如4.0.4、4.1.9.1、4.2.0等,不支持3.1.X
qb_username="admin" #qb用户名
qb_password="adminadmin" #qb密码
qb_web_url="http://localhost:8080" #QB web路径
log_dir="/root/qbauto"
expect_ratio=0.1
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给予权限
chmod +x ratio_mon.sh添加crontab计划任务
*/1 * * * * bash /root/ratio_mon.sh本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
小菜鸡
评论已关闭