/
tempalert.py
119 lines (94 loc) · 3.53 KB
/
tempalert.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
import sys, re
import httplib, libxml2
## some parameters to tune if you want
HTTP_TIMEOUT = 10 #in seconds
HTTP_CONNECTION_ATTEMPTS = 3 #number of attempts to connect
# complete the http connect and return connection object if successful
def http_connect(server):
conn_try = 0
while True:
conn = httplib.HTTPConnection(server, timeout=HTTP_TIMEOUT)
conn_try+=1
try:
conn.request("GET", "/xmlfeed.rb")
return conn
except:
if conn_try >= HTTP_CONNECTION_ATTEMPTS:
print 'Unable to complete HTTP request after %s attempts' % HTTP_CONNECTION_ATTEMPTS
sys.exit(1)
# calculate celcius from fahrenheit. if celcius is provided, just round.
def calc_celcius(temp):
if (unit.lower() == "f"):
# calculate celcius (5/9) * (F-32) and round to 1 decimal place
return round(((5.0/9) * (eval(temp) - 32)), 1)
else:
# round celcius to 1 decminal place
return round(eval(temp),1)
# calculate fahrenheit from celcius. if fahrenheit is provided, just round.
def calc_fahrenheit(temp):
global unit
if (unit.lower() == "c"):
# calculate fahrenheit (C) * (9/5) + 32 and round to 1 decimal place
return round((eval(temp) * (9/5.0) + 32),1)
else:
# round fahrenheit to 1 decimal place
return round(eval(temp),1)
# print temperature results to standard out
def print_results(type,content):
if type == "temperature":
c = calc_celcius(content)
f = calc_fahrenheit(content)
print "fahrenheit:"+str(f)+" celcius:"+str(c)
else:
print "humidity:"+str(content)
# pull the currentReading out of the XML child provided
def find_current_reading_details(type,doc):
for children in doc:
if children.name == "currentReading":
print_results(type,children.content)
break
# validate that the content type is either temperature or humidity
def validate_content_type(type):
if type.lower() == "temperature" or type.lower() == "temp":
return "temperature"
elif type.lower() == "humidity":
return "humidity"
else:
print "Invalid content_type. Only temperature and humidity supported at this time."
sys.exit(1)
# the business end. main logic in here \o/
def main():
content_type = validate_content_type(sys.argv[2])
conn = http_connect(sys.argv[1])
req = conn.getresponse()
# check response code
if req.status == 200:
doc = libxml2.parseDoc(req.read())
root = doc.children
for child in root:
# check what units are configured for reporting
if content_type == "temperature" and child.name == "tempUnits":
global unit
unit = "f" if str(child.content).lower() == "fahrenheit" else "c"
# found temperature elements!
if content_type == "temperature" and (child.type == "element" and re.search("temperature",str(child.properties))):
find_current_reading_details("temperature",child)
# found humidity elements!
if content_type == "humidity" and (child.type == "element" and re.search("humidity",str(child.properties))):
find_current_reading_details("humidity",child)
# close XML document
doc.freeDoc()
# non HTTP/200 OK was returned
else:
print "An HTTP/200 OK was NOT returned."
sys.exit(1)
# close connection
conn.close()
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Usage: tempalert.py <server> <condition_type>"
print " - condition type: temperature OR humidity"
sys.exit(1)
else:
main()