-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·395 lines (331 loc) · 12.9 KB
/
Copy pathmain.py
File metadata and controls
executable file
·395 lines (331 loc) · 12.9 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13,<3.14"
# dependencies = [
# "httpx>=0.28.1,<1.0",
# "python-decouple>=3.8",
# ]
# [tool.uv]
# exclude-newer = "2026-03-31T00:00:00Z"
# ///
"""
Manage Resilio Sync folders via the web UI API.
Supports multiple servers (src, dst) configured via environment variables.
Usage:
rsl list Show folders on all servers
rsl src list Show folders on src server only
rsl dst list Show folders on dst server only
rsl <server> add <name|path> Add folder as new sync folder
rsl <server> connect <name> [path]
Connect folder on specified server
rsl <server> connect-all Connect all disconnected folders on server
rsl <server> remove <name> Remove sync folder
rsl <server> set-pref <key> <value>
Set preference on all folders
rsl <server> set-pref <name> <key> <value>
Set preference on one folder
rsl <server> set-setting <key> <value>
Set global power user preference
Environment (per server, replace <SRV> with SRC or DST):
RSL_<SRV>_HOST Resilio host (default: localhost)
RSL_<SRV>_PORT Resilio port (default: 8888)
RSL_<SRV>_USER Web UI username (default: admin)
RSL_<SRV>_PASS Web UI password (required)
RSL_<SRV>_BASE_PATH Base path for folders (default: /media/shares)
"""
import httpx
import re
import ssl
import sys
from pathlib import Path
from urllib.parse import quote
SERVERS = ("src", "dst")
def _decouple_config():
env_file = Path.cwd() / ".env"
if env_file.exists():
from decouple import Config, RepositoryEnv
return Config(RepositoryEnv(env_file))
else:
from decouple import config
return config
class ResilioAPI:
def __init__(self, name: str):
config = _decouple_config()
prefix = f"RSL_{name.upper()}_"
self.name = name
self.host = config(f"{prefix}HOST", default="localhost")
self.port = config(f"{prefix}PORT", default="8888")
self.user = config(f"{prefix}USER", default="admin")
self.password = config(f"{prefix}PASS", default="")
self.base_path = config(f"{prefix}BASE_PATH", default="/media/shares")
self.base_url = f"https://{self.host}:{self.port}"
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
self.client = httpx.Client(
verify=ctx,
auth=(self.user, self.password),
)
self._token: str | None = None
def _get_token(self) -> str:
resp = self.client.get(f"{self.base_url}/gui/token.html?t=0")
resp.raise_for_status()
match = re.search(r"id='token'[^>]*>([^<]+)<", resp.text)
if not match:
raise RuntimeError(f"Failed to extract token from: {resp.text}")
return match.group(1)
def api(self, params: str) -> dict:
if self._token is None:
self._token = self._get_token()
resp = self.client.get(f"{self.base_url}/gui/?token={self._token}&{params}&t=0")
resp.raise_for_status()
return resp.json()
def get_disconnected(self) -> list[dict]:
result = self.api("action=getdisconnectedfolders")
return result.get("value", [])
def get_connected(self) -> list[dict]:
result = self.api("action=getsyncfolders&discovery=1")
# discovery=1 includes discovered-but-not-connected folders that lack
# most keys (e.g. no 'path'). Filter to only fully connected folders.
return [f for f in result.get("folders", []) if "path" in f]
def add_folder(self, path: str) -> dict:
encoded = quote(path, safe="")
return self.api(f"action=addsyncfolder&path={encoded}")
def connect_folder(self, folderid: str, path: str) -> dict:
encoded = quote(path, safe="")
return self.api(f"action=adddisconnectedfolder&folderid={folderid}&selectivesync=0&path={encoded}&force=true")
def remove_folder(self, folderid: str) -> dict:
return self.api(f"action=removefolder&folderid={folderid}")
def set_folder_pref(self, folderid: str, key: str, value: str) -> dict:
encoded_value = quote(value, safe="")
return self.api(f"action=setfolderpref&folderid={folderid}&{key}={encoded_value}")
def set_setting(self, key: str, value: str) -> dict:
encoded_value = quote(value, safe="")
return self.api(f"action=setsettings&{key}={encoded_value}")
def _check_result(result: dict) -> str | None:
"""Return error message if the API response indicates failure, else None."""
if result.get("status") != 200:
return str(result)
value = result.get("value", {})
if isinstance(value, dict) and value.get("error"):
return value.get("message", f"error code {value['error']}")
return None
def _normalize_value(value: str) -> str:
"""Convert true/false to 1/0 for the Resilio API."""
match value.lower():
case "true":
return "1"
case "false":
return "0"
case _:
return value
def cmd_add(api: ResilioAPI, path: str):
if not path.startswith("/"):
path = f"{api.base_path}/{path}"
connected = api.get_connected()
if any(f["path"] == path for f in connected):
print(f"Already synced: {path}")
return 0
print(f"Adding sync folder: {path}")
result = api.add_folder(path)
error = _check_result(result)
if error:
print(f" Failed: {error}")
return 1
print(" OK")
return 0
def cmd_list(names: tuple[str, ...]):
for name in names:
api = ResilioAPI(name)
print(f"=== {name.upper()} ({api.host}:{api.port}) ===")
disconnected = api.get_disconnected()
if disconnected:
print(f"Disconnected ({len(disconnected)}):")
for f in sorted(disconnected, key=lambda x: x["name"]):
print(f" {f['name']} (id: {f['folderid']})")
else:
print("No disconnected folders.")
connected = api.get_connected()
if connected:
print(f"Connected ({len(connected)}):")
for f in sorted(connected, key=lambda x: x["name"]):
print(f" {f['name']} -> {f['path']}")
if name != names[-1]:
print()
def cmd_connect(api: ResilioAPI, name: str, path: str | None = None):
if path is None:
path = f"{api.base_path}/{name}"
disconnected = api.get_disconnected()
match = [f for f in disconnected if f["name"] == name]
if not match:
print(f"Error: '{name}' not in disconnected list")
available = [f["name"] for f in disconnected]
if available:
print(f"Available: {', '.join(sorted(available))}")
return 1
folderid = match[0]["folderid"]
print(f"Connecting '{name}' (id: {folderid}) -> {path}")
result = api.connect_folder(folderid, path)
error = _check_result(result)
if error:
print(f" Failed: {error}")
return 1
print(" OK")
return 0
def cmd_connect_all(api: ResilioAPI):
disconnected = api.get_disconnected()
if not disconnected:
print("No disconnected folders.")
return
print(f"Connecting {len(disconnected)} folders to {api.base_path}/...\n")
errors = 0
for f in sorted(disconnected, key=lambda x: x["name"]):
result = cmd_connect(api, f["name"])
if result:
errors += 1
if errors:
print(f"\n{errors} folder(s) failed.")
def cmd_remove(api: ResilioAPI, name: str):
connected = api.get_connected()
match = [f for f in connected if f["name"] == name]
if not match:
print(f"Error: '{name}' not in connected folders")
available = [f["name"] for f in connected]
if available:
print(f"Available: {', '.join(sorted(available))}")
return 1
folderid = match[0]["folderid"]
print(f"Removing '{name}' (id: {folderid})")
result = api.remove_folder(folderid)
error = _check_result(result)
if error:
print(f" Failed: {error}")
return 1
print(" OK")
return 0
def cmd_set_pref(api: ResilioAPI, args: list[str]):
# 2 args: key value (all folders)
# 3 args: name key value (single folder)
if len(args) == 2:
key, value = args
value = _normalize_value(value)
connected = api.get_connected()
if not connected:
print("No connected folders.")
return 0
print(f"Setting {key}={value} on {len(connected)} folder(s)")
errors = 0
for f in sorted(connected, key=lambda x: x["name"]):
result = api.set_folder_pref(f["folderid"], key, value)
error = _check_result(result)
if error:
print(f" {f['name']}: Failed: {error}")
errors += 1
else:
print(f" {f['name']}: OK")
return 1 if errors else 0
elif len(args) == 3:
name, key, value = args
value = _normalize_value(value)
connected = api.get_connected()
match = [f for f in connected if f["name"] == name]
if not match:
print(f"Error: '{name}' not in connected folders")
available = [f["name"] for f in connected]
if available:
print(f"Available: {', '.join(sorted(available))}")
return 1
folderid = match[0]["folderid"]
print(f"Setting {key}={value} on '{name}' (id: {folderid})")
result = api.set_folder_pref(folderid, key, value)
error = _check_result(result)
if error:
print(f" Failed: {error}")
return 1
print(" OK")
return 0
else:
print("Usage: rsl <server> set-pref [<name>] <key> <value>")
return 1
def cmd_set_setting(api: ResilioAPI, args: list[str]):
if len(args) != 2:
print("Usage: rsl <server> set-setting <key> <value>")
return 1
key, value = args
value = _normalize_value(value)
print(f"Setting {key}={value} on {api.name}")
result = api.set_setting(key, value)
error = _check_result(result)
if error:
print(f" Failed: {error}")
return 1
print(" OK")
return 0
def main():
args = sys.argv[1:]
# Extract server name if first arg is a known server
server: str | None = None
if args and args[0] in SERVERS:
server = args.pop(0)
cmd = args[0] if args else "help"
match cmd:
case "list" | "ls":
names = (server,) if server else SERVERS
cmd_list(names)
case "add":
if server is None:
print("Error: server required (e.g. rsl src add Books)")
sys.exit(1)
if len(args) < 2:
print(f"Usage: rsl {server} add <name|path>")
sys.exit(1)
api = ResilioAPI(server)
sys.exit(cmd_add(api, args[1]) or 0)
case "connect":
if server is None:
print("Error: server required (e.g. rsl src connect <name>)")
sys.exit(1)
if len(args) < 2:
print(f"Usage: rsl {server} connect <name> [path]")
sys.exit(1)
api = ResilioAPI(server)
name = args[1]
path = args[2] if len(args) > 2 else None
sys.exit(cmd_connect(api, name, path) or 0)
case "connect-all":
if server is None:
print("Error: server required (e.g. rsl src connect-all)")
sys.exit(1)
api = ResilioAPI(server)
cmd_connect_all(api)
case "remove" | "rm":
if server is None:
print("Error: server required (e.g. rsl src remove Books)")
sys.exit(1)
if len(args) < 2:
print(f"Usage: rsl {server} remove <name>")
sys.exit(1)
api = ResilioAPI(server)
sys.exit(cmd_remove(api, args[1]) or 0)
case "set-pref":
if server is None:
print("Error: server required (e.g. rsl src set-pref selectivesync true)")
sys.exit(1)
if len(args) < 3:
print(f"Usage: rsl {server} set-pref [<name>] <key> <value>")
sys.exit(1)
api = ResilioAPI(server)
sys.exit(cmd_set_pref(api, args[1:]) or 0)
case "set-setting":
if server is None:
print("Error: server required (e.g. rsl src set-setting worker_threads_count 1)")
sys.exit(1)
if len(args) < 3:
print(f"Usage: rsl {server} set-setting <key> <value>")
sys.exit(1)
api = ResilioAPI(server)
sys.exit(cmd_set_setting(api, args[1:]) or 0)
case _:
print(__doc__)
if __name__ == "__main__":
main()