Commit 9cb69ea6 authored by Markus's avatar Markus

added more helptext, fixed things :)

parent 01a931d0
......@@ -5,66 +5,80 @@ import pynotify
import argparse
import os
import time
import sys
CACHE_FOLDER="~/.cache/hack_count/"
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))
count_changed=True
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process stuff')
###parser
parser = argparse.ArgumentParser(description='Access Peoplecounter in\
Hackspace and create notification or print ')
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="Enable or disable\
printing",default=True)
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("-d","--daemonize",action="store_true",help="Daemonize the\
script",default=False)
script (default=False) enable with -d set inteval with -i",default=False)
parser.add_argument("-i","--interval",type=int,help="Interval for\
update",default=30)
args= parser.parse_args()
while_val=False
if args.daemonize == True:
while_val=True
update default=30",default=30)
args = parser.parse_args()
while while_val == True :
while True :
##fetch usercount
var=urllib.urlopen("http://hack-hro.de/api/users")
device_count=json.loads(var.read().decode())['deviceCount']
var = urllib.urlopen("http://hack-hro.de/api/users")
device_count = json.loads(var.read().decode())['deviceCount']
#####create cache folder if not exsits####
if not os.path.exists(CACHE_FOLDER):
os.makedirs(CACHE_FOLDER)
####fetch cached vals
if os.path.exists(CACHE_PATH):
try:
cached_data_dict=json.loads(open(CACHE_PATH).read())
#print cached_data_dict['device_count']
except:
open(CACHE_PATH, 'a').close()
cached_data_dict={
"device_count" : 0
}
### 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)
#compare saved and fetched value
if not (cached_data_dict['device_count'] == device_count ) :
count_changed=True
### save to cachefile ####
with open(os.path.join(str(CACHE_FOLDER),str(CACHE_FILE)),"w" ) as iofile:
json.dump(cachedict,iofile)
iofile.close()
#####prepare to save value
cachedict= {
"device_count" : device_count,
}
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():
### save to cachefile ####
with open(os.path.join(str(CACHE_FOLDER),str(CACHE_FILE)),"w+" ) as iofile:
json.dump(cachedict,iofile)
iofile.close()
##notification
if args.notify:
if not pynotify.init("HackHro"):
sys.exit(1)
#create notification
n = pynotify.Notification("DeviceCount", str(device_count)+" Devices")
if count_changed :
if not n.show() :
print ("Failed to send notification")
sys.exit(1)
count_changed = False
##print count
if args.printing==True:
if args.printing == True:
print "HackHro Users: ",device_count
if not args.daemonize == True :
sys.exit(0)
time.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