-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_autorun_preferences_allaims.py
165 lines (153 loc) · 6.24 KB
/
set_autorun_preferences_allaims.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import os
import requests
import subprocess
import shutil
# 服务器地址、用户名和密码
BASE_URL = "******:8080"
USERNAME = "***"
PASSWORD = "******"
def get_torrent_info_by_hash(sid, info_hash):
"""
通过 info hash 获取种子信息
"""
url = f"{BASE_URL}/api/v2/torrents/info"
params = {"hashes": info_hash}
headers = {"Cookie": f"SID={sid}"}
try:
response = requests.get(url, params=params, headers=headers)
response.raise_for_status()
torrent_info = response.json()
if torrent_info:
file_info = torrent_info[0]
return file_info.get("name", ""), file_info.get("content_path", ""), file_info.get("tags", ""), file_info.get("save_path", "")
else:
print("未找到与 info hash 相关的种子信息")
return None, None, None, None
except requests.exceptions.RequestException as e:
print(f"获取种子信息失败: {e}")
return None, None, None, None
def process_bdmv_folders(save_path):
"""
递归处理包含 BDMV 文件夹的目录
"""
for root, dirs, files in os.walk(save_path):
for d in dirs:
if d == "BDMV":
mv_path = os.path.dirname(os.path.join(root, d))
mv_name = os.path.basename(mv_path)
iso_file = os.path.join(os.path.dirname(mv_path), f"{mv_name}.iso")
log_file = f"/home/boxbox/logs/{mv_name}.log"
# 打印正在处理的目录路径
with open("/home/boxbox/ttlog", "a") as log_file:
log_file.write(f"处理目录:{mv_path}\n")
# 生成 ISO 文件并删除原始目录
try:
subprocess.run(["genisoimage", "-o", iso_file, "-iso-level", "4", "-allow-lowercase", "-l", "-udf", "-allow-limited-size", mv_path], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
shutil.rmtree(mv_path)
with open("/home/boxbox/ttlog", "a") as log_file:
log_file.write(f"{mv_name}.iso 文件生成成功并原始目录已删除\n")
except subprocess.CalledProcessError as e:
with open("/home/boxbox/ttlog", "a") as log_file:
log_file.write(f"生成 {mv_name}.iso 文件失败: {e}\n")
def has_bdmv_folder(save_path):
"""
检查 save_path 及其子文件夹是否含有 BDMV 文件夹
"""
for root, dirs, files in os.walk(save_path):
if "BDMV" in dirs:
return True
return False
def process_non_bdmv_folders(save_path, name, tags):
"""
处理非 BDMV 文件夹的逻辑
"""
print("未找到 BDMV 文件夹,执行非原盘操作")
command = [
"python3",
"/home/boxbox/torcp/tp.py",
os.path.join(save_path, name), # 使用 save_path 和 name 结合
"-d",
f"/home/boxbox/Emby/{name}/",
"-s"
]
if tags:
command.extend(["--imdbid", tags])
command.extend([
"--tmdb-api-key",
"1f749b3a822f0982bf853b1c5c145824",
"--origin-name",
"--emby-bracket"
])
log_command = " ".join(command)
print(f"执行命令: {log_command}")
with open('/home/boxbox/qbittorrent_script.log', 'a') as log_file:
log_file.write(f"执行命令: {log_command}\n")
try:
with open("/home/boxbox/tp_log.log", "a") as log_file:
subprocess.run(command, stdout=log_file, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError as e:
print(f"命令执行失败: {e}")
# 构建并执行 rclone 命令
rclone_command = [
"rclone",
"move",
f"/home/boxbox/Emby/{name}/",
"/home/boxbox/MyEmby/",
"-v",
"--stats",
"2000s",
"--transfers",
"3",
"--drive-chunk-size",
"32M",
"--log-file=/home/boxbox/ttclone.log",
"--delete-empty-src-dirs"
]
log_rclone_command = " ".join(rclone_command)
print(f"执行 rclone 命令:{log_rclone_command}")
with open('/home/boxbox/qbittorrent_script.log', 'a') as log_file:
log_file.write(f"执行 rclone 命令:{log_rclone_command}\n")
try:
subprocess.run(rclone_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
# 清理空文件夹
cleanup_command = ["find", "/home/boxbox/Emby", "-type", "d", "-empty", "-exec", "rmdir", "{}", "+"]
subprocess.run(cleanup_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
except subprocess.CalledProcessError as e:
print(f"rclone 命令执行失败: {e}")
def main():
# 从命令行获取种子信息的 info_hash 参数
if len(sys.argv) < 2:
print("未提供 info hash 参数")
sys.exit(1)
info_hash_v1 = sys.argv[1]
# 登录到 qBittorrent
try:
login_url = f"{BASE_URL}/api/v2/auth/login"
data = {"username": USERNAME, "password": PASSWORD}
response = requests.post(login_url, data=data)
response.raise_for_status()
sid = response.cookies.get("SID")
if sid:
print("登录成功")
# 获取种子信息
name, content_path, tags, save_path = get_torrent_info_by_hash(sid, info_hash_v1)
if name and save_path:
# 记录种子信息到日志文件
with open('/home/boxbox/qbittorrent_script.log', 'a') as log_file:
log_file.write(f"种子信息: name={name}, content_path={content_path}, tags={tags}, save_path={save_path}\n")
# 检查是否存在 BDMV 文件夹
if has_bdmv_folder(save_path):
print("发现 BDMV 文件夹,执行 BDMV 文件夹处理逻辑")
process_bdmv_folders(save_path)
else:
# 执行处理非 BDMV 文件夹的逻辑
process_non_bdmv_folders(save_path, name, tags)
else:
print("未获取到种子信息或者保存路径")
else:
print("登录失败")
except requests.exceptions.RequestException as e:
print(f"登录到 qBittorrent 失败: {e}")
if __name__ == "__main__":
main()