Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
infrastructure
Hackspace Device Info
Commits
01a931d0
Commit
01a931d0
authored
May 15, 2014
by
Markus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added deamonize, interval...
parent
dc5dd511
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
16 deletions
+54
-16
hack_count
hack_count
+54
-16
No files found.
hack_count
View file @
01a931d0
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment