Commit 01a931d0 authored by Markus's avatar Markus

added deamonize, interval...

parent dc5dd511
......@@ -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)
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