def _normalize_outlook( outlook ):
# if we're debugging xbmc module is not available
tid = "F"
sid = "mph"
if ( not DEBUG ):
tid = xbmc.getRegion( id="tempunit" )[ -1 ]
sid = xbmc.getRegion( id="speedunit" )
# enumerate thru and localize values
for count, tmp_outlook in enumerate( outlook ):
# calculate the localized temp if C is required
temps = re.findall( "[0-9]+F", tmp_outlook )
for temp in temps:
tmp_outlook = re.sub( temp, _localize_unit( temp ) + tid, tmp_outlook, 1 )
# calculate the localized temp ranges if C is required
temps = re.findall( "[low|mid|high]+ [0-9]+s", tmp_outlook )
add = { "l": 3, "m": 6, "h": 9 }
for temp in temps:
new_temp = _localize_unit( str( int( re.findall( "[0-9]+", temp )[ 0 ] ) + add.get( temp[ 0 ], 3 ) ) )
temp_int = int( float( new_temp ) / 10 ) * 10
temp_rem = int( float( new_temp ) % 10 )
temp_text = ( "low %ds", "mid %ds", "high %ds", )[ ( temp_rem >= 4 ) + ( temp_rem >= 6 ) ]
tmp_outlook = re.sub( temp, temp_text % ( temp_int, ), tmp_outlook, 1 )
# calculate the localized wind if C is required
winds = re.findall( "[0-9]+ to [0-9]+ mph", tmp_outlook )
for wind in winds:
speeds = re.findall( "[0-9]+", wind )
for speed in speeds:
wind = re.sub( speed, _localize_unit( speed, "speed" ).split( " " )[ 0 ], wind, 1 )
tmp_outlook = re.sub( "[0-9]+ to [0-9]+ mph", wind.replace( "mph", sid ), tmp_outlook, 1 )
# add our text back to the main variable
outlook[ count ] = tmp_outlook
# return normalized text
return outlook
def _localize_unit( value, unit="temp" ):
# replace any invalid characters
value = value.replace( chr(176), "" ).replace( "°", "" ).replace( "F", "" )
# do not convert invalid values
if ( not value or value.startswith( "N/A" ) ):
return value
# time conversion
if ( unit == "time" ):
# set default time
time = value
# set our default temp unit
id = "h:mm:ss xx"
# if we're debugging xbmc module is not available
if ( not DEBUG ):
id = xbmc.getRegion( id="time" )
if ( id == "h:mm:ss xx" ):
return time
# 24 hour ?
if ( id.startswith( "H" ) ):
hour = int( value.split( ":" )[ 0 ] ) + ( 12 * ( value.split( " " )[ 1 ] == "PM" ) )
time = "%d:%s" % ( hour, value.split( " " )[ 0 ].split( ":" )[ 1 ], )
if ( id.split( " " )[ -1 ] == "xx" ):
time = "%s %s" % ( time, value.split( " " )[ 1 ], )
return time
else:
# we need an float
value = float( value.replace( chr(176), "" ).replace( "°", "" ).replace( "F", "" ).replace( "mph", "" ).replace( "in.", "" ) )
# temp conversion
if ( unit == "temp" or unit == "tempdiff" ):
# set our default temp unit
id = "F"
# if we're debugging xbmc module is not available
if ( not DEBUG ):
id = xbmc.getRegion( id="tempunit" )[ -1 ]
# calculate the localized temp if C is required
if ( id == "C" ):
# C/F difference or temperature conversion
if ( unit == "tempdiff" ):
# 9 degrees of F equal 5 degrees of C
value = round( float( 5 * value ) / 9 )
else:
# convert to celcius
value = round( ( value - 32 ) * ( float( 5 ) / 9 ) )
# get our sign, only + is needed for tempdiff
sign = ( "", "+", )[ value >= 0 and unit == "tempdiff" ]
# return localized temp
return sign + str( int( value ) )
# speed conversion
elif ( unit == "speed" ):
# set our default temp unit
id = "mph"
# if we're debugging xbmc module is not available
if ( not DEBUG ):
id = xbmc.getRegion( id="speedunit" )
# calculate the localized speed
if ( id == "km/h" ):
value = round( value * 1.609344 )
elif ( id == "m/s" ):
value = round( value * 0.45 )
elif ( id == "ft/min" ):
value = round( value * 88 )
elif ( id == "ft/s" ):
value = round( value * 1.47 )
elif ( id == "yard/s" ):
value = round( value * 0.4883 )
# return localized speed
return "%d %s" % ( int( value ), id, )
# length conversion
elif ( unit == "length" ):
# set our default length unit
id = "in."
# if we're debugging xbmc module is not available
if ( not DEBUG ):
id = ( "in.", "cm", )[ xbmc.getRegion( id="tempunit" )[ -1 ] == "C" ]
# calculate the localized length
if ( id == "cm" ):
value = float( value * 2.54 )
# return localized length
return "%.2f%s" % ( value, id, )