2010-08-08 01:44:31 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2010-09-27 09:55:59 +02:00
|
|
|
import os
|
2010-08-08 01:44:31 +02:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
url = 'http://www.weather.com/weather/today/%s'
|
|
|
|
|
|
|
|
re_tmp = re.compile('realTemp: "(\d+)"')
|
|
|
|
re_hum = re.compile('relativeHumidity: "(\d+)"')
|
|
|
|
re_loc = re.compile('locName: "([\w ]+)"')
|
|
|
|
|
2010-09-27 09:55:59 +02:00
|
|
|
#code = sys.argv[0][(sys.argv[0].rfind('_') + 1):]
|
|
|
|
code = os.environ.get('code', sys.argv[0][(sys.argv[0].rfind('_') + 1):])
|
2010-08-08 01:44:31 +02:00
|
|
|
|
|
|
|
if code == None: sys.exit(1)
|
|
|
|
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
|
|
|
|
print "yes"
|
|
|
|
elif len(sys.argv) == 2 and sys.argv[1] == "config":
|
|
|
|
u = urllib.urlopen(url % code)
|
|
|
|
txt = u.read()
|
|
|
|
u.close()
|
|
|
|
|
|
|
|
LOC_list = re_loc.findall(txt)
|
|
|
|
|
|
|
|
if len(LOC_list):
|
|
|
|
LOC = LOC_list[0]
|
|
|
|
else:
|
|
|
|
LOC = "Unknown"
|
|
|
|
|
|
|
|
print 'graph_title Weather in %s' % LOC
|
|
|
|
print 'graph_vlabel Temperature and Humidity'
|
|
|
|
print 'graph_category sensors'
|
|
|
|
|
|
|
|
print 'temperature.label Temperature'
|
|
|
|
print 'humidity.label Humidity'
|
|
|
|
|
|
|
|
print 'graph_args --base 1000 -l 0'
|
|
|
|
else:
|
|
|
|
u = urllib.urlopen(url % code)
|
|
|
|
txt = u.read()
|
|
|
|
u.close()
|
|
|
|
|
|
|
|
TMP_F_list = re_tmp.findall(txt)
|
|
|
|
HUM_list = re_hum.findall(txt)
|
|
|
|
|
|
|
|
if len(HUM_list):
|
|
|
|
HUM = HUM_list[0]
|
|
|
|
else:
|
2010-09-27 09:55:59 +02:00
|
|
|
sys.exit(1)
|
2010-08-08 01:44:31 +02:00
|
|
|
|
|
|
|
if len(TMP_F_list):
|
|
|
|
TMP_F = TMP_F_list[0]
|
|
|
|
TMP_C = (int(TMP_F) - 32) * 5/9
|
|
|
|
else:
|
2010-09-27 09:55:59 +02:00
|
|
|
sys.exit(1)
|
2010-08-08 01:44:31 +02:00
|
|
|
|
|
|
|
print 'temperature.value %s' % TMP_C
|
|
|
|
print 'humidity.value %s' % HUM
|