Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / sensors / weather_ @ e5ce7492

Historique | Voir | Annoter | Télécharger (1,25 ko)

1
#!/usr/bin/python
2

    
3
import os
4
import re
5
import sys
6
import urllib
7

    
8
url = 'http://www.weather.com/weather/today/%s'
9

    
10
re_tmp = re.compile('realTemp: "(\d+)"')
11
re_hum = re.compile('relativeHumidity: "(\d+)"')
12
re_loc = re.compile('locName: "([\w ]+)"')
13

    
14
#code = sys.argv[0][(sys.argv[0].rfind('_') + 1):]
15
code = os.environ.get('code', sys.argv[0][(sys.argv[0].rfind('_') + 1):])
16

    
17
if code == None: sys.exit(1)
18

    
19
if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
20
	print "yes"
21
elif len(sys.argv) == 2 and sys.argv[1] == "config":
22
	u = urllib.urlopen(url % code)
23
	txt = u.read()
24
	u.close()
25

    
26
	LOC_list = re_loc.findall(txt)
27
	
28
	if len(LOC_list):
29
		LOC = LOC_list[0]
30
	else:
31
		LOC = "Unknown"
32

    
33
	print 'graph_title Weather in %s' % LOC
34
	print 'graph_vlabel Temperature and Humidity'
35
	print 'graph_category sensors'
36

    
37
	print 'temperature.label Temperature'
38
	print 'humidity.label Humidity'
39

    
40
	print 'graph_args --base 1000 -l 0'
41
else:
42
	u = urllib.urlopen(url % code)
43
	txt = u.read()
44
	u.close()
45

    
46
	TMP_F_list = re_tmp.findall(txt)
47
	HUM_list = re_hum.findall(txt)
48

    
49
	if len(HUM_list):
50
		HUM = HUM_list[0]
51
	else:
52
		sys.exit(1)
53

    
54
	if len(TMP_F_list):
55
		TMP_F = TMP_F_list[0]
56
		TMP_C = (int(TMP_F) - 32) * 5/9
57
	else:
58
		sys.exit(1)
59

    
60
	print 'temperature.value %s' % TMP_C
61
	print 'humidity.value %s' % HUM