This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.py
98 lines (85 loc) · 2.68 KB
/
notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import urllib2
from urllib2 import URLError
import json
from subprocess import call
import os
import threading
import time
import platform
#home = getenv("HOME")
folder = os.path.dirname(os.path.realpath(__file__))
oldstatus=-1
olddate=0
os_type=''
def DisplayMessage(title, message):
if os_type == 'Linux':
call(['notify-send', title, message,'-t','3000','-i',folder+'/icon.png'])
elif os_type == 'Darwin':
call(['terminal-notifier', '-title', title, '-message',message,'-appIcon',folder+'/icon.png'])
#call(['notify-send', title, message,'-t','3000','-i',folder+'/icon.png'])
#call['notify-send', title+' '+message+', '+displ_time+' --hint=int:transient:1 -i ~/.pspace-notifier/pspace.png']
def EventCheck():
global olddate
current_ip = urllib2.urlopen('http://ip.42.pl/raw').read()
if current_ip != '195.97.37.145':
addr='http://pspace.dyndns.org:49004/report/?json&limit=1'
else:
addr='http://192.168.1.41/report/?json&limit=1'
try:
url = urllib2.urlopen(addr)
if url.getcode() != 200:
print 'problem occured. http status:', url.getcode()
else:
event = json.loads(url.read())
newdate = int(event['events'][0]['t'])
if(olddate!=newdate):
DisplayMessage('P-Space door event',event['events'][0]['extra'])
olddate=newdate
except URLError:
olddate=0
def StatusCheck():
global oldstatus
try:
url = urllib2.urlopen('http://www.p-space.gr/status')
if url.getcode() != 200:
print 'problem occured. http status:', url.getcode()
else:
newstatus = int(url.read())
if(oldstatus!=newstatus):
if(newstatus==1):
status='open'
else:
status='closed'
oldstatus=newstatus
DisplayMessage('P-Space status changed','P-Space is now '+status)
except URLError:
oldstatus=-1
def SetOpen(set):
try:
url = urllib2.urlopen('http://www.p-space.gr/status/set.php?'+set)
if url.getcode() != 200:
print 'problem occured. http status:', r1.status, r1.reason
except URLError:
print 'URL exception occured.'
def tick():
EventCheck()
StatusCheck()
parser = argparse.ArgumentParser(description='P-Space Status and Event Notifier for Linux and OS X - eparon 2014 v0.2')
parser.add_argument('-t','--time', help='Specify the refresh interval in seconds',type=int)
parser.add_argument('-r','--run', help='Set this flag in order to run in the background. Otherwise will only check once for status and last event.', action='store_true')
parser.add_argument('-s','--set',help='Set status - Use type \'open\' or \'close\'', choices=['open','close'])
args = parser.parse_args()
os_type=platform.system()
if args.set:
SetOpen(args.set)
if args.run:
if args.time:
ts=args.time
else:
ts=1.0
while 1==1:
tick()
time.sleep(ts)
else:
tick()