NAVIGATION
Home
Gallery
Java
Linux
Web
Scripts And Utilities
Mobile And Sms
Misc
Contact
pixelWIKI
Nabaz Tag




<<

Nab Py Weather

Simple python script to read the weather from the BBC and get the Nabaz Tag to read it. I use a cron job to trigger it at set times. Before 5pm it reads today's weather, after 5pm it reads tomorrows weather

Other Nabaztag Scripts


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:
#! /usr/bin/python

import urllib
import re
import time

# your bbc weather id, from the URL at http://bbc.co.uk/weather - e.g. http://www.bbc.co.uk/weather/5day.shtml?id=3549
bbc_id = "3549"

# your nabaztag IDs
nab_sn = "000000c0ffee"
nab_token = "1234567890"
nab_voice = "UK-Mistermuggles"

# if you have a proxy server, set it here
px = {}
#px = {'http':'http://domain:3128'}

# the main code
def send( data ):
        url = "http://api.nabaztag.com/vl/FR/api.jsp"
        values = { "sn" : nab_sn,
                "token": nab_token,
                "voice": nab_voice,
                "tts": data
        }
        print( urllib.FancyURLopener(proxies=px).open(url, urllib.urlencode( values )).read() )

def cleanup( data ):
        data = data.replace( "n", "" )
        data = data.replace("°C", " degrees celcius")
        data = data.replace("mph", " miles per hour")

        data = re.compile( "(dd°F)" ).sub( "", data )

        data = re.compile( "(Pressure:s*d*)mb" ).sub( "g<1>", data )

        data = re.compile( "(.*?)Humidity.*" ).sub( "g<1>", data )

        p = re.compile( "Direction:(.*?)," )
        m = p.search(data)
        mid = data[m.start()+10:m.end()-1].replace( "N", "North " ).replace( "W", "West " ).replace( "S", "South ").replace( "E", "East ")
        data = data[0:m.start()+10] + mid + data[m.end()-1:]
        #print( m.span() )

        data = re.compile( "Temp:" ).sub( "temperature", data )
        return data


server = "feeds.bbc.co.uk"
url = "http://" + server + "/weather/feeds/rss/5day/id/" + bbc_id + ".xml"

data = urllib.FancyURLopener(proxies=px).open(url).read()

titlePattern = re.compile( "<title>(.*day):s*(.*?,).*?</title>" )
descPattern = re.compile( "<description>(.*?)</description>", re.M | re.S )

whichLoop = 1;
# after 5pm show tomorrow's weather
if ( time.localtime()[3] >= 17 ) :
        whichLoop = 2

start = 0
loop =0
while True:
        loop += 1
        result = titlePattern.search( data, start )
        if ( result != None ):
                start = result.start()
                res2 = descPattern.search( data, start )
                if ( loop == whichLoop ) :
                        send( "The weather for " + result.group(1) + " is: " + result.group(2) + cleanup( res2.group(1) ) )
                start = res2.start()

        else:
                break