2023-11-09
无米之炊
00

目录

背景
具体思路

背景

如果有超过本地硬盘容量的资源需要下载的话,比如说 100T 的资源,而本地只有 1 T 的磁盘空间,我们需要实现边下载边删除的设计。

具体思路

  1. 使用 qbittorrent-nox 监控某个文件夹下的 torrent 文件,然后把他们自动加入下载,但是务必需要设置不自动开始。

  2. 使用 Rclone 配置远程存储。

  3. 写一个脚本定期执行,实现监控 qbittorrent-nox 里面的任务下载情况,如果检测到了下载完毕的任务将资源使用 Rclone 上传到远程存储,然后删除下载的文件,同时恢复以特定规则排序下前 N 名的任务的下载。

Python
import qbittorrentapi import os import re import logging import subprocess import threading import shutil class QbittorrentClient: def __init__(self, host='127.0.0.1', port=8080, username='<username>', password='<password>'): self.client = qbittorrentapi.Client(host=host, port=port) self.client.auth_log_in(username=username, password=password) def close(self): self.client.auth_log_out() def get_all_torrents(self): print('Get all torrents') return self.client.torrents_info() def resume_first_torrent(self): print('Check if we need a new resume download.') all_torrents = self.get_all_torrents() sorted_all_torrents = sorted(all_torrents, key=lambda t: t['name']) if sorted_all_torrents: first_torrent = sorted_all_torrents[0] self.client.torrents.resume([first_torrent.hash]) def rclone_copy(self, town_path): cmd = ['/usr/bin/rclone', '--gcs-bucket-policy-only', 'copy', town_path, 'gcs://sci-hub', '-P'] try: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, universal_newlines=True) logging.basicConfig(filename='/mnt/ares-data/upload.log', level=logging.INFO, format='%(message)s') logging.info(output) print(town_path + '上传成功') try: shutil.rmtree(town_path) print(town_path, "删除成功") except OSError as e: print(town_path, "删除失败:", e) except subprocess.CalledProcessError as e: logging.basicConfig(filename='/mnt/ares-data/upload.log', level=logging.INFO, format='%(message)s') logging.error(e.output) print(town_path, '上传失败:', e) def check_completed_torrents(self): torrents = self.get_all_torrents() completed_torrents = [torrent for torrent in torrents if torrent['progress'] == 1.0] for torrent in completed_torrents: file_path = '/mnt/ares-data/download/' + torrent['name'] town_path = '/mnt/ares-data/town/' + torrent['name'] if os.path.exists(file_path): try: print('Moving...') shutil.move(file_path, town_path) except: print('Moving Failed') print('Remove torrent:' + torrent['name']) self.client.torrents_delete(delete_files=False, torrent_hashes=torrent.hash) logging.basicConfig(filename='/mnt/ares-data/remove.log', level=logging.INFO) logging.info('Torrent {} removed'.format(torrent['name'])) self.resume_first_torrent() print('Ready upload:' + torrent['name']) self.rclone_copy(town_path) else: logging.basicConfig(filename='/mnt/ares-data/error.log', level=logging.ERROR) logging.error('File {} not found'.format(file_path)) if __name__ == "__main__": qb = QbittorrentClient() qb.resume_first_torrent() qb.check_completed_torrents() qb.close()

本文作者:XiaFan

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!