Projet

Général

Profil

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

root / plugins / weather / weather_ @ a7139bca

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

1
#!/usr/bin/env python
2

    
3
import os
4
import re
5
import sys
6
from urllib.request import urlopen
7

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

    
10
re_tmp = re.compile(r'realTemp: "(\d+)"')
11
re_hum = re.compile(r'relativeHumidity: "(\d+)"')
12
re_loc = re.compile(r'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 not code:
18
    sys.exit(1)
19
elif 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 = 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 = 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)