Spaces:
Runtime error
Runtime error
Update remote_uploader.py
Browse files- remote_uploader.py +56 -123
remote_uploader.py
CHANGED
@@ -2,71 +2,21 @@ import requests
|
|
2 |
import argparse
|
3 |
import os
|
4 |
import time
|
5 |
-
from watchdog.observers import Observer
|
6 |
-
from watchdog.events import FileSystemEventHandler
|
7 |
|
8 |
-
class
|
9 |
def __init__(self, server_url, api_key, space_id):
|
10 |
self.server_url = server_url
|
11 |
self.api_key = api_key
|
12 |
self.space_id = space_id
|
13 |
-
self.uploaded_files = set() # 跟踪已上传的文件
|
14 |
|
15 |
-
def send_log(self, message):
|
16 |
-
"""发送日志到服务器"""
|
17 |
-
try:
|
18 |
-
log_url = f"{self.server_url.rstrip('/')}/api/remote_log"
|
19 |
-
headers = {
|
20 |
-
'X-API-Key': self.api_key,
|
21 |
-
'Content-Type': 'application/json'
|
22 |
-
}
|
23 |
-
data = {
|
24 |
-
'space_id': self.space_id,
|
25 |
-
'message': message,
|
26 |
-
'timestamp': time.time()
|
27 |
-
}
|
28 |
-
|
29 |
-
response = requests.post(log_url, headers=headers, json=data, timeout=5)
|
30 |
-
if response.status_code != 200:
|
31 |
-
print(f"发送日志失败: {response.status_code}")
|
32 |
-
except Exception as e:
|
33 |
-
print(f"发送日志异常: {e}")
|
34 |
-
|
35 |
-
def on_created(self, event):
|
36 |
-
"""当新文件被创建时触发"""
|
37 |
-
if not event.is_directory:
|
38 |
-
# 等待一段时间确保文件写入完成
|
39 |
-
time.sleep(1)
|
40 |
-
self.upload_file(event.src_path)
|
41 |
-
|
42 |
-
def on_modified(self, event):
|
43 |
-
"""当文件被修改时触发"""
|
44 |
-
if not event.is_directory:
|
45 |
-
# 等待一段时间确保文件写入完成
|
46 |
-
time.sleep(1)
|
47 |
-
self.upload_file(event.src_path)
|
48 |
-
|
49 |
def upload_file(self, file_path):
|
50 |
"""上传单个文件"""
|
51 |
if not os.path.exists(file_path):
|
52 |
-
|
53 |
-
|
54 |
-
self.send_log(message)
|
55 |
-
return
|
56 |
|
57 |
filename = os.path.basename(file_path)
|
58 |
-
|
59 |
-
# 避免重复上传同一个文件
|
60 |
-
if file_path in self.uploaded_files:
|
61 |
-
return
|
62 |
-
|
63 |
-
message = f"检测到新文件: {filename}"
|
64 |
-
print(message)
|
65 |
-
self.send_log(message)
|
66 |
-
|
67 |
-
message = f"正在上传到服务器..."
|
68 |
-
print(message)
|
69 |
-
self.send_log(message)
|
70 |
|
71 |
# 构建完整的 API 端点 URL
|
72 |
upload_url = f"{self.server_url.rstrip('/')}/api/remote_upload"
|
@@ -90,102 +40,85 @@ class FileUploadHandler(FileSystemEventHandler):
|
|
90 |
response = requests.post(upload_url, headers=headers, data=data, files=files)
|
91 |
|
92 |
# 打印服务器响应
|
93 |
-
|
94 |
-
print(message)
|
95 |
-
self.send_log(message)
|
96 |
-
|
97 |
try:
|
98 |
result = response.json()
|
99 |
print(result)
|
100 |
-
self.send_log(str(result))
|
101 |
-
|
102 |
if response.status_code == 200:
|
103 |
-
|
104 |
-
|
105 |
-
print(success_msg)
|
106 |
-
self.send_log(success_msg)
|
107 |
else:
|
108 |
-
|
109 |
-
|
110 |
-
self.send_log(error_msg)
|
111 |
except requests.exceptions.JSONDecodeError:
|
112 |
print(response.text)
|
113 |
-
|
114 |
|
115 |
except IOError as e:
|
116 |
-
|
117 |
-
|
118 |
-
self.send_log(error_msg)
|
119 |
except requests.exceptions.RequestException as e:
|
120 |
-
|
121 |
-
|
122 |
-
self.send_log(error_msg)
|
123 |
|
124 |
-
def
|
125 |
-
"""
|
126 |
if not os.path.exists(watch_dir):
|
127 |
-
print(f"
|
128 |
-
|
129 |
|
130 |
-
|
|
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
print(start_msg)
|
134 |
-
handler.send_log(start_msg)
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
|
144 |
-
|
145 |
-
print(wait_msg)
|
146 |
-
handler.send_log(wait_msg)
|
147 |
|
148 |
-
|
149 |
-
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
print("-" * 50)
|
163 |
-
handler.send_log("-" * 50)
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
observer.schedule(handler, watch_dir, recursive=False)
|
168 |
-
observer.start()
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
time.sleep(1)
|
173 |
-
except KeyboardInterrupt:
|
174 |
-
stop_msg = "\n停止监听..."
|
175 |
-
print(stop_msg)
|
176 |
-
handler.send_log(stop_msg)
|
177 |
-
observer.stop()
|
178 |
|
179 |
-
|
180 |
|
181 |
if __name__ == "__main__":
|
182 |
-
parser = argparse.ArgumentParser(description="
|
183 |
parser.add_argument("api_key", help="您的 API 密钥")
|
184 |
parser.add_argument("space_id", help="Space ID")
|
185 |
parser.add_argument("--server", default="http://127.0.0.1:5001", help="服务器的 URL 地址 (默认: http://127.0.0.1:5001)")
|
186 |
-
parser.add_argument("--
|
187 |
|
188 |
args = parser.parse_args()
|
189 |
|
190 |
-
#
|
191 |
-
|
|
|
2 |
import argparse
|
3 |
import os
|
4 |
import time
|
|
|
|
|
5 |
|
6 |
+
class FileUploader:
|
7 |
def __init__(self, server_url, api_key, space_id):
|
8 |
self.server_url = server_url
|
9 |
self.api_key = api_key
|
10 |
self.space_id = space_id
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def upload_file(self, file_path):
|
13 |
"""上传单个文件"""
|
14 |
if not os.path.exists(file_path):
|
15 |
+
print(f"文件不存在: {file_path}")
|
16 |
+
return False
|
|
|
|
|
17 |
|
18 |
filename = os.path.basename(file_path)
|
19 |
+
print(f"正在上传文件: {filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# 构建完整的 API 端点 URL
|
22 |
upload_url = f"{self.server_url.rstrip('/')}/api/remote_upload"
|
|
|
40 |
response = requests.post(upload_url, headers=headers, data=data, files=files)
|
41 |
|
42 |
# 打印服务器响应
|
43 |
+
print(f"服务器响应 ({response.status_code}):")
|
|
|
|
|
|
|
44 |
try:
|
45 |
result = response.json()
|
46 |
print(result)
|
|
|
|
|
47 |
if response.status_code == 200:
|
48 |
+
print(f"✅ 文件 '{filename}' 上传成功!")
|
49 |
+
return True
|
|
|
|
|
50 |
else:
|
51 |
+
print(f"❌ 上传失败: {result.get('error', '未知错误')}")
|
52 |
+
return False
|
|
|
53 |
except requests.exceptions.JSONDecodeError:
|
54 |
print(response.text)
|
55 |
+
return False
|
56 |
|
57 |
except IOError as e:
|
58 |
+
print(f"读取文件时出错: {e}")
|
59 |
+
return False
|
|
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
+
print(f"请求时出错: {e}")
|
62 |
+
return False
|
|
|
63 |
|
64 |
+
def upload_directory_once(watch_dir, server_url, api_key, space_id):
|
65 |
+
"""一次性扫描并上传目录中的所有文件"""
|
66 |
if not os.path.exists(watch_dir):
|
67 |
+
print(f"目录不存在: {watch_dir}")
|
68 |
+
return
|
69 |
|
70 |
+
print(f"🔍 开始扫描目录: {watch_dir}")
|
71 |
+
print(f"📡 服务器地址: {server_url}")
|
72 |
+
print(f"🔑 Space ID: {space_id}")
|
73 |
+
print("-" * 50)
|
74 |
|
75 |
+
uploader = FileUploader(server_url, api_key, space_id)
|
|
|
|
|
76 |
|
77 |
+
# 获取所有文件
|
78 |
+
all_files = []
|
79 |
+
for root, dirs, files in os.walk(watch_dir):
|
80 |
+
for file in files:
|
81 |
+
file_path = os.path.join(root, file)
|
82 |
+
if os.path.isfile(file_path):
|
83 |
+
all_files.append(file_path)
|
84 |
|
85 |
+
if not all_files:
|
86 |
+
print("📁 目录中没有找到任何文件")
|
87 |
+
return
|
88 |
|
89 |
+
print(f"📁 找到 {len(all_files)} 个文件,开始上传...")
|
|
|
|
|
90 |
|
91 |
+
success_count = 0
|
92 |
+
failed_count = 0
|
93 |
|
94 |
+
for file_path in all_files:
|
95 |
+
try:
|
96 |
+
if uploader.upload_file(file_path):
|
97 |
+
success_count += 1
|
98 |
+
else:
|
99 |
+
failed_count += 1
|
100 |
+
# 稍微延迟一下,避免服务器压力过大
|
101 |
+
time.sleep(0.5)
|
102 |
+
except Exception as e:
|
103 |
+
print(f"上传文件 {file_path} 时发生异常: {e}")
|
104 |
+
failed_count += 1
|
|
|
|
|
105 |
|
106 |
+
print("-" * 50)
|
107 |
+
print(f"📊 上传完成! 成功: {success_count}, 失败: {failed_count}")
|
|
|
|
|
108 |
|
109 |
+
if success_count > 0:
|
110 |
+
print("🎉 文件已成功上传到您的网盘!")
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
return success_count, failed_count
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
+
parser = argparse.ArgumentParser(description="一次性文件上传器 - 扫描并上传指定文件夹中的所有文件")
|
116 |
parser.add_argument("api_key", help="您的 API 密钥")
|
117 |
parser.add_argument("space_id", help="Space ID")
|
118 |
parser.add_argument("--server", default="http://127.0.0.1:5001", help="服务器的 URL 地址 (默认: http://127.0.0.1:5001)")
|
119 |
+
parser.add_argument("--upload-dir", default="output", help="要上传的目录 (默认: output)")
|
120 |
|
121 |
args = parser.parse_args()
|
122 |
|
123 |
+
# 开始一次性上传
|
124 |
+
upload_directory_once(args.upload_dir, args.server, args.api_key, args.space_id)
|