Spaces:
Runtime error
Runtime error
Update remote_uploader.py
Browse files- remote_uploader.py +87 -147
remote_uploader.py
CHANGED
@@ -1,157 +1,97 @@
|
|
1 |
import asyncio
|
2 |
import websockets
|
3 |
-
import json
|
4 |
-
import logging
|
5 |
-
import sys
|
6 |
-
import base64
|
7 |
import os
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
logger = logging.getLogger(__name__)
|
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 |
-
if not os.path.exists(upload_dir):
|
83 |
-
print(f"目录不存在: {upload_dir}")
|
84 |
-
|
85 |
-
|
86 |
-
print(f"🔍 开始扫描目录: {upload_dir}")
|
87 |
-
print(f"
|
88 |
-
print(f"🔑 Space ID: {space_id}")
|
89 |
print("-" * 50)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
# 获取所有文件
|
94 |
-
all_files = []
|
95 |
-
for root, dirs, files in os.walk(upload_dir):
|
96 |
-
for file in files:
|
97 |
-
file_path = os.path.join(root, file)
|
98 |
-
if os.path.isfile(file_path):
|
99 |
-
all_files.append(file_path)
|
100 |
-
|
101 |
-
if not all_files:
|
102 |
-
print("📁 目录中没有找到任何文件")
|
103 |
-
return
|
104 |
-
|
105 |
-
print(f"📁 找到 {len(all_files)} 个文件,开始上传...")
|
106 |
-
|
107 |
-
success_count = 0
|
108 |
-
failed_count = 0
|
109 |
-
|
110 |
-
for file_path in all_files:
|
111 |
-
try:
|
112 |
-
if await uploader.upload_file(file_path):
|
113 |
-
success_count += 1
|
114 |
-
else:
|
115 |
-
failed_count += 1
|
116 |
-
# 稍微延迟一下,避免服务器压力过大
|
117 |
-
await asyncio.sleep(0.5)
|
118 |
-
except Exception as e:
|
119 |
-
print(f"上传文件 {file_path} 时发生异常: {e}")
|
120 |
-
failed_count += 1
|
121 |
-
|
122 |
print("-" * 50)
|
123 |
-
print(f"📊 上传完成! 成功: {
|
124 |
-
|
125 |
-
if success_count > 0:
|
126 |
-
print("🎉 文件已成功上传到您的网盘!")
|
127 |
-
|
128 |
-
return success_count, failed_count
|
129 |
-
|
130 |
-
async def main():
|
131 |
-
if len(sys.argv) < 3:
|
132 |
-
print("使用方法: python3 remote_uploader.py <api_key> <space_id> [--server <server_url>] [--upload-dir <directory>]")
|
133 |
-
sys.exit(1)
|
134 |
-
|
135 |
-
api_key = sys.argv[1]
|
136 |
-
space_id = sys.argv[2]
|
137 |
-
|
138 |
-
# 解析可选参数
|
139 |
-
server_url = "https://gkbtyo-rqvays-5001.preview.cloudstudio.work"
|
140 |
-
upload_dir = "output"
|
141 |
-
|
142 |
-
i = 3
|
143 |
-
while i < len(sys.argv):
|
144 |
-
if sys.argv[i] == "--server" and i + 1 < len(sys.argv):
|
145 |
-
server_url = sys.argv[i + 1]
|
146 |
-
i += 2
|
147 |
-
elif sys.argv[i] == "--upload-dir" and i + 1 < len(sys.argv):
|
148 |
-
upload_dir = sys.argv[i + 1]
|
149 |
-
i += 2
|
150 |
-
else:
|
151 |
-
i += 1
|
152 |
-
|
153 |
-
# 开始异步上传
|
154 |
-
await upload_directory_once(upload_dir, server_url, api_key, space_id)
|
155 |
|
156 |
-
if
|
157 |
-
|
|
|
1 |
import asyncio
|
2 |
import websockets
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
+
import time
|
5 |
+
import base64
|
6 |
+
import logging
|
7 |
+
import argparse
|
8 |
+
import json
|
9 |
+
import urllib.parse
|
10 |
|
11 |
+
# Configure logging
|
12 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
async def upload_directory(space_id, api_key, upload_dir):
|
16 |
+
# 使用查询参数传递 api_key
|
17 |
+
encoded_secret = urllib.parse.quote(api_key)
|
18 |
+
uri = f"wss://remote-terminal-worker.nianxi4563.workers.dev/terminal/{space_id}?secret={encoded_secret}"
|
19 |
+
logger.info(f"Attempting to connect to {uri}")
|
20 |
+
|
21 |
+
try:
|
22 |
+
async with websockets.connect(uri, ping_interval=20, ping_timeout=60) as websocket:
|
23 |
+
logger.info("Connected to WebSocket")
|
24 |
+
|
25 |
+
# 发送机器注册信息(尽管去除 machine_id,但保持兼容)
|
26 |
+
machine_info = {"type": "machine", "card_id": space_id}
|
27 |
+
await websocket.send(json.dumps(machine_info))
|
28 |
+
logger.debug(f"Sent machine registration: {machine_info}")
|
29 |
+
|
30 |
+
# 不等待 machine_id,直接继续
|
31 |
+
|
32 |
+
# 获取所有文件
|
33 |
+
all_files = []
|
34 |
+
for root, dirs, files in os.walk(upload_dir):
|
35 |
+
for file in files:
|
36 |
+
file_path = os.path.join(root, file)
|
37 |
+
if os.path.isfile(file_path):
|
38 |
+
all_files.append(file_path)
|
39 |
+
|
40 |
+
if not all_files:
|
41 |
+
logger.info("No files found in directory")
|
42 |
+
return 0, 0
|
43 |
+
|
44 |
+
logger.info(f"Found {len(all_files)} files, starting upload...")
|
45 |
+
|
46 |
+
success_count = 0
|
47 |
+
failed_count = 0
|
48 |
+
|
49 |
+
for file_path in all_files:
|
50 |
+
try:
|
51 |
+
with open(file_path, 'rb') as f:
|
52 |
+
file_content = f.read()
|
53 |
+
file_b64 = base64.b64encode(file_content).decode('utf-8')
|
54 |
+
file_msg = {
|
55 |
+
"type": "file_upload",
|
56 |
+
"filename": os.path.basename(file_path),
|
57 |
+
"content": file_b64
|
58 |
+
}
|
59 |
+
await websocket.send(json.dumps(file_msg))
|
60 |
+
logger.info(f"Uploaded file: {file_path}")
|
61 |
+
success_count += 1
|
62 |
+
except Exception as e:
|
63 |
+
logger.error(f"Error uploading file {file_path}: {e}")
|
64 |
+
failed_count += 1
|
65 |
+
# 稍微延迟,避免服务器压力过大
|
66 |
+
await asyncio.sleep(0.5)
|
67 |
+
|
68 |
+
logger.info(f"Upload completed! Success: {success_count}, Failed: {failed_count}")
|
69 |
+
return success_count, failed_count
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
logger.error(f"Failed to connect or upload: {e}")
|
73 |
+
return 0, len(os.listdir(upload_dir)) if os.path.exists(upload_dir) else 0
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
parser = argparse.ArgumentParser(description="一次性文件上传器 - 使用 WebSocket 扫描并上传指定文件夹中的所有文件")
|
77 |
+
parser.add_argument("api_key", help="您的 API 密钥 (作为用户 token)")
|
78 |
+
parser.add_argument("space_id", help="Space ID (作为 card_id)")
|
79 |
+
parser.add_argument("--upload-dir", default="output", help="要上传的目录 (默认: output)")
|
80 |
+
|
81 |
+
args = parser.parse_args()
|
82 |
+
|
83 |
+
if not os.path.exists(args.upload_dir):
|
84 |
+
print(f"目录不存在: {args.upload_dir}")
|
85 |
+
exit(1)
|
86 |
+
|
87 |
+
print(f"🔍 开始扫描目录: {args.upload_dir}")
|
88 |
+
print(f"🔑 Space ID (card_id): {args.space_id}")
|
|
|
89 |
print("-" * 50)
|
90 |
+
|
91 |
+
success, failed = asyncio.run(upload_directory(args.space_id, args.api_key, args.upload_dir))
|
92 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
print("-" * 50)
|
94 |
+
print(f"📊 上传完成! 成功: {success}, 失败: {failed}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
if success > 0:
|
97 |
+
print("🎉 文件已成功上传到您的网盘!")
|