Commit 01a931d0 authored by Markus's avatar Markus

added deamonize, interval...

parent dc5dd511
...@@ -3,6 +3,13 @@ import urllib ...@@ -3,6 +3,13 @@ import urllib
import json import json
import pynotify import pynotify
import argparse 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__': if __name__ == '__main__':
...@@ -10,23 +17,54 @@ if __name__ == '__main__': ...@@ -10,23 +17,54 @@ if __name__ == '__main__':
parser.add_argument("-n","--notify",action="store_true",help="Create a notification using\ parser.add_argument("-n","--notify",action="store_true",help="Create a notification using\
libnotify",default=False) libnotify",default=False)
parser.add_argument("-p","--printing",action="store_true",help="Create a notification using\ parser.add_argument("-p","--printing",action="store_true",help="Enable or disable\
libnotify",default=True) 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() args= parser.parse_args()
##fetch usercount while_val=False
var=urllib.urlopen("http://hack-hro.de/api/users") if args.daemonize == True:
device_count=json.loads(var.read().decode())['deviceCount'] while_val=True
if args.notify: while while_val == True :
if not pynotify.init("HackHro"): ##fetch usercount
sys.exit(1) var=urllib.urlopen("http://hack-hro.de/api/users")
#create notification device_count=json.loads(var.read().decode())['deviceCount']
n = pynotify.Notification("DeviceCount", str(device_count)+" Devices")
if not n.show(): ####fetch cached vals
print ("Failed to send notification") if os.path.exists(CACHE_PATH):
sys.exit(1) cached_data_dict=json.loads(open(CACHE_PATH).read())
if args.printing: #print cached_data_dict['device_count']
print "HackHro Users: ",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