From 01a931d0fd5dd8abde5501ca9c0b7de7fc11b77e Mon Sep 17 00:00:00 2001 From: Markus Date: Thu, 15 May 2014 00:17:25 +0200 Subject: [PATCH] added deamonize, interval... --- hack_count | 70 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/hack_count b/hack_count index 0993db5..90343ca 100755 --- a/hack_count +++ b/hack_count @@ -3,6 +3,13 @@ import urllib import json import pynotify import argparse +import os +import time + + +CACHE_FOLDER="~/.cache/hack_count/" +CACHE_FILE="hack_count.json" +CACHE_PATH=os.path.join(str(CACHE_FOLDER),str(CACHE_FILE)) if __name__ == '__main__': @@ -10,23 +17,54 @@ if __name__ == '__main__': parser.add_argument("-n","--notify",action="store_true",help="Create a notification using\ libnotify",default=False) - parser.add_argument("-p","--printing",action="store_true",help="Create a notification using\ - libnotify",default=True) + parser.add_argument("-p","--printing",action="store_true",help="Enable or disable\ + printing",default=True) + parser.add_argument("-d","--daemonize",action="store_true",help="Daemonize the\ + script",default=False) + parser.add_argument("-i","--interval",type=int,help="Interval for\ + update",default=30) args= parser.parse_args() - ##fetch usercount - var=urllib.urlopen("http://hack-hro.de/api/users") - device_count=json.loads(var.read().decode())['deviceCount'] - - if args.notify: - if not pynotify.init("HackHro"): - sys.exit(1) - #create notification - n = pynotify.Notification("DeviceCount", str(device_count)+" Devices") - if not n.show(): - print ("Failed to send notification") - sys.exit(1) - if args.printing: - print "HackHro Users: ",device_count + while_val=False + if args.daemonize == True: + while_val=True + + while while_val == True : + ##fetch usercount + var=urllib.urlopen("http://hack-hro.de/api/users") + device_count=json.loads(var.read().decode())['deviceCount'] + + ####fetch cached vals + if os.path.exists(CACHE_PATH): + cached_data_dict=json.loads(open(CACHE_PATH).read()) + #print cached_data_dict['device_count'] + + ### if diffrent save and create notification + if not cached_data_dict['device_count'] == device_count : + #####prepare save value + cachedict= { + "device_count" : device_count, + } + + #####create cache folder if not exsits#### + if not os.path.exists(CACHE_FOLDER): + os.makedirs(CACHE_FOLDER) + + ### save to cachefile #### + with open(os.path.join(str(CACHE_FOLDER),str(CACHE_FILE)),"w" ) as iofile: + json.dump(cachedict,iofile) + iofile.close() + if args.notify: + if not pynotify.init("HackHro"): + sys.exit(1) + #create notification + n = pynotify.Notification("DeviceCount", str(device_count)+" Devices") + if not n.show(): + print ("Failed to send notification") + sys.exit(1) + ##print count + if args.printing==True: + print "HackHro Users: ",device_count + time.sleep(args.interval) -- GitLab