diff --git a/hc_main.py b/hc_main.py new file mode 100644 index 0000000000000000000000000000000000000000..288dfc4d43088f9e8e4a7b9b3fef24afad72f0bc --- /dev/null +++ b/hc_main.py @@ -0,0 +1,242 @@ +# -*- coding: utf-8 -*- + +import os +import sys +import dbus +import json +import itertools +import urllib.error +import urllib.request +from time import sleep + +import hc_head +import hc_data + + +class Main(object): + """Class representing the main procedures.""" + + def __init__(self, args): + """""" + + # store command line arguments + self.args = args + + # create variable-object + self.header = hc_head.Variables() + + # create date-interface + try: + # import hc_data here, to save one try-except clause :D + import hc_data + + # create data_interface + self.data_interface = hc_data.Data() + except urllib.error.HTTPError as e: + print('Could not connect to url "http://hack-hro.de/api/users":') + print(e) + exit(1) + + # initiate lists + self.user_left_list = None + self.user_new_list = None + + def caching_routine(): + """""" + + ####################################################################### + # fetch-routine + #### fetch cached vals + #TODO: check, when data from server is available + try: + cached_data_dict = json.loads(open(hc_head.CACHE_PATH).read()) + self.header.count_changed = False + except: + open(hc_head.CACHE_PATH, 'a').close() + cached_data_dict = { + "device_count": 0, + "users": users + } + + #compare saved and fetched value + if (self.args.show_user and + cached_data_dict['device_count'] != device_count): + self.header.count_changed = True + + #diff saved and new recieved list + user_diff_fetched = list( + itertools.filterfalse(lambda x: x in cached_data_dict['users'], + users)) + user_diff_saved = list( + itertools.filterfalse(lambda x: x in users, + cached_data_dict['users'])) + + #####prepare to save value + save_cache_dict = { + "device_count": device_count, + "users": users + } + + if len(user_diff_fetched) > 0: + self.header.count_changed = True + self.user_new_list = user_diff_fetched + + if len(user_diff_saved) > 0: + self.header.count_changed = True + self.user_left_list = user_diff_saved + + ### save to cachefile + with open(os.path.join(str(hc_head.CACHE_FOLDER), + str(hc_head.CACHE_FILE)), + "w+") as iofile: + json.dump(save_cache_dict, iofile) + iofile.close() + # end of fetch-routine + ####################################################################### + + def printing_routine(): + """""" + + ####################################################################### + # printing routine + ## print count + if self.args.printing: + if not self.args.show_user: + print("HackHro Users: {} ".format(len(users))) + + if self.user_left_list is not None: + print("Users left: ", flush=True, end="") + for user in user_left_list: + print(user['name'] + " ", flush=True) + + if self.user_new_list is not None: + print("Users new: ", flush=True, end="") + for user in user_new_list: + print(user['name'] + " ", end="", flush=True) + else: + print("HackHro Devcies: ", device_count, flush=True) + # end of printing routine + ####################################################################### + + def notification_routine(): + """""" + + ####################################################################### + # notification routine + if self.args.notify and self.header.count_changed: + ###init dbus notifications + bus = dbus.SessionBus() + notif = bus.get_object(self.header.item, self.header.path) + notify = dbus.Interface(notif, self.header.interface) + notification_string = "" + + if not self.args.show_user: + title = "HackSpace Users Changed" + if self.user_left_list is not None: + for user in self.user_left_list: + self.header.icon = os.path.join( + hc_head.CACHE_PICTURE_FOLDER, + str(user['id']) + ".jpg") + + if not os.path.isfile(self.header.icon): + self.header.icon = os.path.join( + hc_head.CACHE_PICTURE_FOLDER, + "default.png") + + notification_string = "the user " +\ + str(user['name']) +\ + " left." + + #create notification + notify.Notify(self.header.app_name, + 0, + self.header.icon, + title, + notification_string, + self.header.array, + self.header.hint, + self.header.time) + + if self.user_new_list is not None: + for user in self.user_new_list: + self.header.icon = os.path.join(hc_head. + hc_head.CACHE_PICTURE_FOLDER, + str(user['id']) + ".jpg") + + if not os.path.isfile(self.header.icon): + self.header.icon = os.path.join( + hc_head.CACHE_PICTURE_FOLDER, + "default.png") + + notification_string = "the user " +\ + str(user['name']) +\ + " just came in." + #create notification + notify.Notify(self.header.app_name, + 0, + self.header.icon, + title, + notification_string, + self.header.array, + self.header.hint, + self.header.time) + else: + title = "HackSpace Devices Changed" + notification_string = str(device_count) + #create notification + notify.Notify(self.header.app_name, + 0, + self.header.icon, + title, + notification_string, + self.header.array, + self.header.hint, + self.header.time) + + self.header.count_changed = False + self.user_left_list = None + self.user_new_list = None + # end of notification routine + ################################################################### + + def start(self): + """""" + + while True: + # update variables from url + try: + self.data_interface.update() + # server might be down, so catch HTTPErrors + except urllib.error.HTTPError as e: + print('Could not connect to url: \ +"http://hack-hro.de/api/users":') + print(e) + exit(1) + + # for each user logged in + for user in data_interface.users: + # create a path of its avatar + user_image_path = os.path.join(hc_head.CACHE_PICTURE_FOLDER, + str(user['id']) + ".jpg") + # check, whether this avatar exists + if not os.path.isfile(user_image_path): + # if not, check whether there are resources available + if 'avatar' in user: + # and retrieve them + urllib.request.urlretrieve( + "http://hack-hro.de" + user['avatar'], + user_image_path) + + # fetch data from cache/write data into cache + caching_routine() + + # print user/device infos + printing_routine() + + # notify user/device infos + notification_routine() + + # break/continue loop depending on daemonize-value + if not self.args.daemonize: + sys.exit(0) + sleep(self.args.interval) \ No newline at end of file