-
Notifications
You must be signed in to change notification settings - Fork 1
/
gather_wx_data.py
75 lines (64 loc) · 2.28 KB
/
gather_wx_data.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
import re
import sys
import json
import requests
def main(args):
url="http://k7msh-lakemountain-wx.local.mesh/"
html = ""
for i in range(3):
try:
html = requests.get(url).text
break
except Exception as err:
i+=1
if i > 2:
print "Failed to connect. Exiting."
raise err
print "Error connecting, trying again..."
make_json(html)
def make_json(html):
try:
data = {}
data['windSpeed'] = get_wind_speed(html)
data['windKnots'] = get_wind_knots(html)
data['windDirection'] = get_direction(html)
data['windDirectionCompass'] = get_compass_direction(html)
data['windStrength'] = get_strength(html)
data['tempInside'] = get_temp_inside(html)
data['tempOutside'] = get_temp_outside(html)
json_data = json.dumps(data)
print str(json_data)
return json_data
except Exception as err:
print "Failed creating JSON object: "+str(err)
raise err
def get_wind_speed(html):
m = re.search('Speed: (\d*\.\d*)', html)
return str(m.group(1)).strip() if m else None
def get_wind_knots(html):
m = re.search('Knots: (\d*\.\d*)', html)
return str(m.group(1)).strip() if m else None
def get_strength(html):
m = re.search('Strength: (.*)<br />', html)
return str(m.group(1)).strip() if m else None
def get_direction(html):
m = re.search('Direction: (\d*)', html)
return str(m.group(1)).strip() if m else None
def get_compass_direction(html):
m = re.search('Compas Direction: (\D*)<br />', html)
return str(m.group(1)).strip() if m else None
def get_temp_inside(html):
m = re.search('Temp inside building: (\d*\.\d*)', html)
return str(m.group(1)).strip() if m else None
def get_temp_outside(html):
m = re.search('temp outside building: (\d*\.\d*)', html)
return str(m.group(1)).strip() if m else None
def strip_encoded_chars(html):
try:
ascii_html = html.encode('ascii', 'ignore').decode('ascii')
return ascii_html
except Exception as err:
print "Failed to strip encoded chars: "+str(err)
raise err
if __name__ == '__main__':
main(sys.argv)