# WWeather by 2boom 2012 v.0.1
# mod by olegha 10.12.12
# xml from weather.ua
from Components.Converter.Converter import Converter
from Components.Element import cached
from Tools.Directories import fileExists
from Poll import Poll
import time
import os
# 03 -
weather_city = '3036'
lang = 'es'
time_update = 30
time_update_ms = 30000
class WWeather(Poll, Converter, object):
City = 0
Country = 1
Cloud = 2
Pict = 3
Temperature = 4
Pressure = 5
WindSpeed = 6
WindDirect = 7
Humidity = 8
def __init__(self, type):
Converter.__init__(self, type)
Poll.__init__(self)
if type == "City":
self.type = self.City
elif type == "Country":
self.type = self.Country
elif type == "Cloud":
self.type = self.Cloud
elif type == "Pict":
self.type = self.Pict
elif type == "Temperature":
self.type = self.Temperature
elif type == "Pressure":
self.type = self.Pressure
elif type == "WindSpeed":
self.type = self.WindSpeed
elif type == "WindDirect":
self.type = self.WindDirect
elif type == "Humidity":
self.type = self.Humidity
self.poll_interval = time_update_ms
self.poll_enabled = True
@cached
def getText(self):
direct = 0
info = ""
if fileExists("/tmp/weather.xml"):
if int((time.time() - os.stat("/tmp/weather.xml").st_mtime)/60) >= time_update:
os.system("rm /tmp/weather.xml")
os.system("wget -P /tmp 'http://xml.weather.co.ua/1.2/forecast/%s?dayf=1&lang=%s&userid=yoursite_com' -O /tmp/weather.xml" % (weather_city, lang))
else:
os.system("wget -P /tmp 'http://xml.weather.co.ua/1.2/forecast/%s?dayf=1&lang=%s&userid=yoursite_com' -O /tmp/weather.xml" % (weather_city, lang))
if not fileExists("/tmp/weather.xml"):
os.system("echo -e 'None' >> /tmp/weather.xml")
if not fileExists("/tmp/weather.xml"):
return 'N/A'
for line in open("/tmp/weather.xml"):
if self.type == self.City and line.find('<name>') > -1:
info = _("%s") % line.split('<')[1].split('>')[1]
break
for line in open("/tmp/weather.xml"):
if self.type == self.Country and line.find('<name>') > -1:
info = _("%s") % line.split('<')[1].split('>')[1]
elif self.type == self.Cloud and line.find('<pict>') > -1:
direct = int(line.split('<')[1].split('>')[1].split('_')[1])
info = (_("clear"), _("mostly sunny"), _("cloudy"), _("Heavy clouds"), _("shower"), _("rain"), _("thunderstorm"), _("hailstorm"), _("sleet"), _("snow"), _("heavy snow"), "NA", "")[direct]
elif self.type == self.Pict and line.find('<pict>') > -1:
info = line.split('<')[1].split('>')[1].split('.')[0]
elif self.type == self.Temperature and line.find('<t>') > -1:
info = "%s%s" % (line.split('<')[1].split('>')[1], unichr(176).encode("latin-1"))
elif self.type == self.Pressure and line.find('<p>') > -1:
info = _("%s mm") % line.split('<')[1].split('>')[1]
elif self.type == self.WindSpeed and line.find('<w>') > -1:
info = _("%s m/s") % line.split('<')[1].split('>')[1]
elif self.type == self.WindDirect and line.find('<w_rumb>') > -1:
direct = int(line.split('<')[1].split('>')[1])
if direct >= 0 and direct <= 20:
info = _('N')
elif direct >= 21 and direct <= 35:
info = _('NNE')
elif direct >= 36 and direct <= 55:
info = _('NE')
elif direct >= 56 and direct <= 70:
info = _('ENE')
elif direct >= 71 and direct <= 110:
info = _('E')
elif direct >= 111 and direct <= 125:
info = _('ESE')
elif direct >= 126 and direct <= 145:
info = _('SE')
elif direct >= 146 and direct <= 160:
info = _('SSE')
elif direct >= 161 and direct <= 200:
info = _('S')
elif direct >= 201 and direct <= 215:
info = _('SSW')
elif direct >= 216 and direct <= 235:
info = _('SW')
elif direct >= 236 and direct <= 250:
info = _('WSW')
elif direct >= 251 and direct <= 290:
info = _('W')
elif direct >= 291 and direct <= 305:
info = _('WNW')
elif direct >= 306 and direct <= 325:
info = _('NW')
elif direct >= 326 and direct <= 340:
info = _('NNW')
elif direct >= 341 and direct <= 360:
info = _('N')
elif self.type == self.Humidity and line.find('<h>') > -1:
info = "%s%%" % line.split('<')[1].split('>')[1]
elif line.find('<forecast>') > -1:
break
return info
text = property(getText)
def changed(self, what):
Converter.changed(self, (self.CHANGED_POLL,))