Commit d4d738a0 authored by Markus's avatar Markus

requirements updated, hack_count updated

- now can display users
- added itertools to requirements
parent 73912077
......@@ -6,12 +6,15 @@ import argparse
import os
from time import sleep
import sys
import itertools
USER_HOME=os.path.expanduser("~")
CACHE_FOLDER=os.path.join(str(USER_HOME),".cache/hack_count")
CACHE_FILE="hack_count.json"
CACHE_PATH=os.path.join(str(CACHE_FOLDER),str(CACHE_FILE))
USER_HOME = os.path.expanduser("~")
CACHE_FOLDER = os.path.join(str(USER_HOME),".cache/hack_count")
CACHE_PICTURE_FOLDER = os.path.join(str(USER_HOME),".cache/hack_count/pics")
CACHE_FILE = "hack_count.json"
CACHE_PATH = os.path.join(str(CACHE_FOLDER),str(CACHE_FILE))
count_changed=True
......@@ -24,8 +27,6 @@ array = ''
hint = ''
time = 10000 # Use seconds x 1000
app_name = ('Hack-Space User Count')
title = ('Hackspace User')
body = ('')
......@@ -38,6 +39,8 @@ if __name__ == '__main__':
libnotify (default=False) enable with -n",default=False)
parser.add_argument("-p","--printing",action="store_false",help="Enable or disable\
printing (default=True) disable with -p",default=True)
parser.add_argument("-u","--show_user",action="store_false",help="show count\
of users or device(default is user toggle with -u)",default=True)
parser.add_argument("-d","--daemonize",action="store_true",help="Daemonize the\
script (default=False) enable with -d set inteval with -i",default=False)
parser.add_argument("-i","--interval",type=int,help="Interval for\
......@@ -47,19 +50,22 @@ if __name__ == '__main__':
while True :
##fetch usercount
var = urllib.request.urlopen("http://hack-hro.de/api/users")
#var = urllib.urlopen("http://172.16.42.107:3000/api/users")
json_string = var.read()
#print(json_string)
json_dict = json.loads(json_string.decode('ascii'))
device_count = json_dict['deviceCount']
users = json_dict['users']
#####create cache folder if not exsits####
if not os.path.exists(CACHE_FOLDER):
os.makedirs(CACHE_FOLDER)
if not os.path.exists(CACHE_PICTURE_FOLDER) :
os.makedirs(CACHE_PICTURE_FOLDER)
for user in users :
user_image_path = os.path.join(CACHE_PICTURE_FOLDER,str(user['id'])+".jpg")
if not os.path.isfile(user_image_path) :
urllib.request.urlretrieve("http://hack-hro.de"+user['avatar'],user_image_path)
####fetch cached vals
try:
......@@ -68,37 +74,96 @@ if __name__ == '__main__':
except:
open(CACHE_PATH, 'a').close()
cached_data_dict={
"device_count" : 0
"device_count" : 0,
"users" : users
}
#compare saved and fetched value
if not (cached_data_dict['device_count'] == device_count ) :
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
cachedict= {
save_cache_dict= {
"device_count" : device_count,
"users" : users
}
if user_diff_fetched.__len__() > 0 :
count_changed = True
user_new = user_diff_fetched[0]
if user_diff_saved.__len__() > 0 :
count_changed = True
user_left = user_diff_saved[0]
### save to cachefile ####
with open(os.path.join(str(CACHE_FOLDER),str(CACHE_FILE)),"w+" ) as iofile:
json.dump(cachedict,iofile)
json.dump(save_cache_dict,iofile)
iofile.close()
##notification
##print count
if args.printing == True :
if args.show_user == False :
print ("HackHro Users: ",users.__len__())
else :
print ("HackHro Devcies: ",device_count)
try:
user_left
except NameError:
pass
else:
print ("User left: ",user_left)
try:
user_new
except NameError :
pass
else :
print ("User New: ",user_new)
##notification
if args.notify and count_changed == True :
###init dbus notifications
bus = dbus.SessionBus()
notif = bus.get_object(item, path)
notify = dbus.Interface(notif, interface)
notification_string = ""
if args.show_user == False :
title = "HackSpace Users Changed"
try:
user_left
except NameError:
pass
else:
icon = os.path.join(CACHE_PICTURE_FOLDER,str(user_left['id'])+".jpg")
notification_string = "the user "+str(user_left['name'])+" left."
#print ("User left: ",user_left)
try:
user_new
except NameError :
pass
else :
icon = os.path.join(CACHE_PICTURE_FOLDER,str(user_new['id'])+".jpg")
print(icon)
notification_string = "the user "+str(user_new['name'])+" just came in."
else :
title = "HackSpace Devices Changed"
notification_string = str(device_count)
#create notification
notify.Notify(app_name, 0, icon, title, str(device_count), array, hint, time)
notify.Notify(app_name, 0, icon, title, notification_string, array, hint, time)
count_changed = False
##print count
if args.printing == True:
print ("HackHro Users: ",device_count)
if not args.daemonize == True :
sys.exit(0)
sleep(args.interval)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment