import sys import json import urllib.request def upload(ip, script_id, code_file): # Read JS code with open(code_file, 'r', encoding='utf-8') as f: code = f.read() import re # Remove comments and whitespace to save Shelly RAM # Remove multi-line comments code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL) # Remove single-line comments (ignoring http:// or https://) code = re.sub(r'(? 0 print(f"Uploading chunk {idx + 1}/{len(chunks)} (append={append})...") payload = { "id": 1, "method": "Script.PutCode", "params": { "id": int(script_id), "code": chunk, "append": append } } req = urllib.request.Request( url, data=json.dumps(payload).encode('utf-8'), headers={'Content-Type': 'application/json'} ) with urllib.request.urlopen(req) as resp: res = json.loads(resp.read().decode('utf-8')) if "error" in res: raise Exception(f"Failed to upload chunk: {res['error']}") print("Script upload complete!") if __name__ == "__main__": if len(sys.argv) < 4: print("Usage: python3 upload.py ") sys.exit(1) upload(sys.argv[1], sys.argv[2], sys.argv[3])