2019-07-28 16:18:49 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-12-08 20:45:42 +00:00
|
|
|
import base64
|
2019-07-28 16:18:49 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import sys
|
|
|
|
from copy import deepcopy
|
2019-11-30 22:49:14 +00:00
|
|
|
from datetime import datetime
|
2019-07-28 16:18:49 +00:00
|
|
|
from random import randint
|
|
|
|
from shutil import rmtree
|
|
|
|
from threading import Thread
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
from requests import post
|
|
|
|
|
2020-12-08 20:45:42 +00:00
|
|
|
import websocket
|
2019-07-28 16:18:49 +00:00
|
|
|
from compare_json import compare_json
|
2019-08-18 01:09:33 +00:00
|
|
|
from config import get_config
|
2019-07-28 16:18:49 +00:00
|
|
|
from dir_to_json import get_json
|
2020-12-08 20:45:42 +00:00
|
|
|
from misc import check_network, flatten_dirs
|
|
|
|
from pystray import Icon, Menu
|
|
|
|
from pystray import MenuItem as Item
|
|
|
|
from websocket import WebSocket
|
|
|
|
|
|
|
|
try:
|
|
|
|
import thread
|
|
|
|
except ImportError:
|
|
|
|
import _thread as thread
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-11-30 22:49:14 +00:00
|
|
|
_online_icon = Image.open("img/icons/online.png")
|
|
|
|
_offline_icon = Image.open("img/icons/offline.png")
|
|
|
|
_standby_icon = Image.open("img/icons/offline.png")
|
|
|
|
|
2020-12-08 20:45:42 +00:00
|
|
|
if not int(os.getenv("VERBOSE", 1)):
|
|
|
|
def X(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
print = X
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-12-03 09:24:36 +00:00
|
|
|
class MunyalClient(Icon):
|
2019-08-18 01:09:33 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(MunyalClient, self).__init__("Munyal")
|
2020-12-08 20:45:42 +00:00
|
|
|
self.__ws_break = False
|
|
|
|
self.ws = None
|
2019-11-30 22:49:14 +00:00
|
|
|
self.icon = _standby_icon
|
2019-08-18 01:09:33 +00:00
|
|
|
self.config = get_config()
|
|
|
|
self.stack = []
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-18 01:09:33 +00:00
|
|
|
def is_online(self):
|
2020-12-08 20:45:42 +00:00
|
|
|
# if not self.ws_online:
|
|
|
|
# self.icon = _standby_icon
|
|
|
|
# return False
|
2019-08-18 01:09:33 +00:00
|
|
|
ping = check_network("http://google.com", 443)
|
|
|
|
if ping:
|
2019-11-30 22:49:14 +00:00
|
|
|
self.icon = _online_icon
|
2019-07-28 16:18:49 +00:00
|
|
|
else:
|
2019-11-30 22:49:14 +00:00
|
|
|
self.icon = _offline_icon
|
2020-12-08 20:45:42 +00:00
|
|
|
print(f"La PC está {'on' if bool(ping) else 'off'}line")
|
|
|
|
return ping
|
|
|
|
|
|
|
|
def __ws_message(self, ws, message):
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
def __ws_error(self, ws, error):
|
|
|
|
print("Error: ", error)
|
|
|
|
|
|
|
|
def __ws_open(self):
|
|
|
|
print("WebSocket abierto")
|
|
|
|
thread.start_new_thread(self.listener, ())
|
2019-08-18 01:09:33 +00:00
|
|
|
|
|
|
|
def start(self):
|
2020-12-08 20:45:42 +00:00
|
|
|
websocket.enableTrace(True)
|
|
|
|
# ws.on_open = self.listener
|
2019-11-30 22:49:14 +00:00
|
|
|
thread_uploader = Thread(target=self.uploader,
|
|
|
|
name="uploader",
|
|
|
|
daemon=True)
|
|
|
|
thread_downloader = Thread(target=self.downloader,
|
|
|
|
name="downloader",
|
|
|
|
daemon=True)
|
2020-12-08 20:45:42 +00:00
|
|
|
# thread_listener = Thread(target=self.listener,
|
|
|
|
# name="listener",
|
|
|
|
# daemon=True)
|
2019-08-18 01:09:33 +00:00
|
|
|
thread_uploader.start()
|
2019-12-03 09:24:36 +00:00
|
|
|
thread_downloader.start()
|
2020-12-08 20:45:42 +00:00
|
|
|
# thread_listener.start()
|
|
|
|
|
2019-08-18 01:09:33 +00:00
|
|
|
self.run(self.__run)
|
2019-12-03 09:24:36 +00:00
|
|
|
self.menu = Menu(Item("Exit", lambda *args: sys.exit(0)))
|
2019-08-18 01:09:33 +00:00
|
|
|
|
|
|
|
def __run(self, icon):
|
|
|
|
icon.visible = True
|
2020-12-08 20:45:42 +00:00
|
|
|
while True:
|
|
|
|
print("Conectando al websocket")
|
|
|
|
ws = websocket.WebSocketApp(
|
|
|
|
"ws://127.0.0.1:12345",
|
|
|
|
on_message=print,
|
|
|
|
on_error=print,
|
|
|
|
on_close=lambda soc: self.__ws_close(ws))
|
|
|
|
ws.on_open = lambda soc: self.listener(soc)
|
|
|
|
ws.run_forever()
|
|
|
|
sleep(1)
|
2019-08-18 01:09:33 +00:00
|
|
|
|
2020-12-08 20:45:42 +00:00
|
|
|
def __ws_close(self, ws):
|
|
|
|
print("WebSocket cerrado")
|
|
|
|
self.__ws_break = True
|
|
|
|
|
|
|
|
def listener(self, ws):
|
|
|
|
self.ws = ws
|
|
|
|
|
|
|
|
def wrapper(*args):
|
|
|
|
while True:
|
|
|
|
print(self.stack)
|
|
|
|
if self.stack:
|
|
|
|
try:
|
|
|
|
data = self.stack[0]
|
|
|
|
folder = self.config["folder"]
|
|
|
|
if data["is_file"] and data["action"] == "add":
|
|
|
|
full_path = os.path.join(folder, data["name"])
|
|
|
|
data["file"] = base64.b85encode(
|
|
|
|
open(full_path, "rb").read()).decode("ascii")
|
|
|
|
self.ws.send(json.dumps(data))
|
|
|
|
self.stack.pop(0)
|
|
|
|
except Exception as e:
|
|
|
|
print(e.with_traceback())
|
|
|
|
print("Error uploading file, trying again")
|
|
|
|
if not self.is_online() or self.__ws_break:
|
|
|
|
self.__ws_break = False
|
|
|
|
self.ws = None
|
|
|
|
return
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
thread.start_new_thread(wrapper, ())
|
2019-08-18 01:09:33 +00:00
|
|
|
|
|
|
|
def uploader(self):
|
|
|
|
print("Uploader")
|
2019-12-02 08:48:52 +00:00
|
|
|
folder = self.config["folder"]
|
|
|
|
last = get_json(folder)
|
2019-08-18 01:09:33 +00:00
|
|
|
while True:
|
2019-12-02 08:48:52 +00:00
|
|
|
sleep(1)
|
|
|
|
actual = get_json(folder)
|
|
|
|
_actual = deepcopy(actual)
|
|
|
|
delete, add = compare_json(last, actual)
|
|
|
|
|
|
|
|
if delete:
|
|
|
|
print("Cosas eliminadas:")
|
|
|
|
print(delete)
|
2020-12-08 20:45:42 +00:00
|
|
|
delete = flatten_dirs(delete, action="delete")
|
|
|
|
for f in delete:
|
|
|
|
f["action"] = "delete"
|
|
|
|
self.stack.extend(delete)
|
2019-12-02 08:48:52 +00:00
|
|
|
if add:
|
|
|
|
print("Cosas agregadas:")
|
|
|
|
print(add)
|
2020-12-08 20:45:42 +00:00
|
|
|
add = flatten_dirs(add, action="add")
|
|
|
|
for f in add:
|
|
|
|
f["action"] = "add"
|
|
|
|
self.stack.extend(add)
|
2019-12-02 08:48:52 +00:00
|
|
|
last = _actual
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-18 01:09:33 +00:00
|
|
|
def __upload(self, path):
|
|
|
|
pass
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-18 01:09:33 +00:00
|
|
|
def downloader(self):
|
|
|
|
print("Downloader")
|
|
|
|
while True:
|
|
|
|
sleep(7)
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-18 01:09:33 +00:00
|
|
|
def __download(self, path):
|
|
|
|
pass
|
2019-07-28 16:18:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-08-18 01:09:33 +00:00
|
|
|
try:
|
|
|
|
client = MunyalClient()
|
|
|
|
client.start()
|
|
|
|
sys.exit(0)
|
|
|
|
except BaseException:
|
|
|
|
sys.exit(1)
|