munyal-client/client.py

100 lines
2.4 KiB
Python
Raw Normal View History

2019-07-28 16:18:49 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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
2019-08-18 01:09:33 +00:00
import pystray
2019-07-28 16:18:49 +00:00
from PIL import Image, ImageTk
from requests import post
2019-08-18 01:09:33 +00:00
from websocket 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
2019-08-18 01:09:33 +00:00
from misc import check_network
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")
2019-07-28 16:18:49 +00:00
2019-08-18 01:09:33 +00:00
class MunyalClient(pystray.Icon):
def __init__(self):
super(MunyalClient, self).__init__("Munyal")
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):
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
2019-08-18 01:09:33 +00:00
def start(self):
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)
2019-08-18 01:09:33 +00:00
thread_uploader.start()
# thread_downloader.start()
2019-08-18 01:09:33 +00:00
self.run(self.__run)
def __run(self, icon):
icon.visible = True
sleep(60)
self.stop()
def listener(self):
pass
def uploader(self):
print("Uploader")
folder = self.config["folder"]
last = get_json(folder)
2019-08-18 01:09:33 +00:00
while True:
sleep(1)
actual = get_json(folder)
_actual = deepcopy(actual)
delete, add = compare_json(last, actual)
if delete:
print("Cosas eliminadas:")
print(delete)
if add:
print("Cosas agregadas:")
print(add)
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)