diff --git a/client.py b/client.py index 441e77e..dca36c8 100755 --- a/client.py +++ b/client.py @@ -47,6 +47,7 @@ class MunyalClient(Icon): self.icon = _standby_icon self.config = get_config() self.stack = [] + self.ignored = [] def is_online(self): # if not self.ws_online: @@ -96,7 +97,7 @@ class MunyalClient(Icon): ws = websocket.WebSocketApp( f"ws://{self.config['login']['user']}.loca.lt", # "ws://127.0.0.1:12345", - on_message=print, + on_message=self.__download, on_error=print, on_close=lambda soc: self.__ws_close(ws)) ws.on_open = lambda soc: self.listener(soc) @@ -111,12 +112,17 @@ class MunyalClient(Icon): self.ws = ws def wrapper(*args): + folder = self.config["folder"] + uuid = self.config["uuid"] while True: print(self.stack) if self.stack: try: data = self.stack[0] - folder = self.config["folder"] + data["uuid"] = uuid + if data["name"] in self.ignored: + self.ignored.remove(data["name"]) + self.stack.pop(0) if data["is_file"] and data["action"] == "add": full_path = os.path.join(folder, data["name"]) data["file"] = base64.b85encode( @@ -124,7 +130,7 @@ class MunyalClient(Icon): self.ws.send(json.dumps(data)) self.stack.pop(0) except Exception as e: - print(e.with_traceback()) + print(e) print("Error uploading file, trying again") if not self.is_online() or self.__ws_break: self.__ws_break = False @@ -163,13 +169,32 @@ class MunyalClient(Icon): def __upload(self, path): pass - def downloader(self): - print("Downloader") - while True: - sleep(7) + def downloader(self, data): + self.ignored.append(data["name"]) + full_path = os.path.join(self.config["folder"], name) + if data["is_file"]: + if data["action"] == "add": + directory = os.path.split(full_path)[0] + if not os.path.exists(directory): + os.makedirs(directory, exist_ok=True) + with open(full_path, "wb") as f: + f.write(base64.b85decode(data["file"].encode("ascii"))) + else: + os.remove(full_path) + else: + if data["action"] == "add": + os.mkdir(full_path) + else: + shutil.rmtree(full_path) - def __download(self, path): - pass + + def __download(self, message): + try: + data = json.loads(message) + except Exception: + return + if data["uuid"] != self.config["uuid"]: + downloader(data) if __name__ == '__main__': diff --git a/config.py b/config.py index 1d36fc2..fe42ac7 100644 --- a/config.py +++ b/config.py @@ -3,9 +3,12 @@ import shutil import sys import pathlib import pickle + from hashlib import sha256 from tkinter import Button, Entry, Label, StringVar, Tk, filedialog +from uuid import uuid4 + from PIL import Image, ImageTk from misc import alert, get_os @@ -70,7 +73,8 @@ def _set_config(root, user, password, folder): "user": user, "password": passwd_hash }, - "folder": folder + "folder": folder, + "uuid": str(uuid4()) })) root.destroy()