lunes, 13 de diciembre de 2010

El clima con Pidgin (Actualización con reporte del viento y sensación térmica)


Una pequeña actualización para
la barra de status de Pidgin.










Ahora además de la temperatura, tambien nos da un reporte de la velocidad del viento.
¡Que por cierto hoy aqui en Río Gallegos tenemos un verdadero temporal de viento,
96 Km/h con ráfagas de 120Km/h !!!




aquí el bloque de código para el script en Python (ejecutado desde una consola)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import dbus,time,commands,string,sys

# Autor: Julio Alberto Lascano
# web: https://drcalambre.blogspot.com
# Fecha: 15 Diciembre de 2010
# requiere: python, weather-util
# web clima = http://www.weather.gov/tg/siteloc.shtml

# python por defecto considera que los caracteres con los que trabajamos son formato ASCII de 7 bits (ni siquiera el extendido). 
# Por supuesto, en estos tristes 7 bits no entran ni la ñ ni los acentos, ni un montón de caracteres más. 
# Es por esto que nosotros los hispano hablantes necesitamos decirle a Python que hay más caracteres y que los queremos interpretar :D
# Para esto hacemos: -*- coding: utf-8 -*-


def set_status(message):
  bus = dbus.SessionBus()
  obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
  current = purple.PurpleSavedstatusGetType(purple.PurpleSavedstatusGetCurrent())
  status = purple.PurpleSavedstatusNew("", current)
  purple.PurpleSavedstatusSetMessage(status, message)
  purple.PurpleSavedstatusActivate(status)

while True:
  horaFecha = commands.getoutput("date +'%H:%M %d/%m/%Y'")
  reporteClima = string.split(commands.getoutput("weather -vi SAWG"),'\n')
  tamanio = len(reporteClima)
  for i in range(0, tamanio):
   if string.find(reporteClima[i],"Temperature:") == 0:
    idTemperatura = i
   elif string.find(reporteClima[i],"Pressure") == 0:
    idPresion = i
   elif string.find(reporteClima[i],"Windchill:") == 0:
    idTermica = i
   elif string.find(reporteClima[i],"Wind:") == 0:
    #El viento está en calma?
    if string.find(reporteClima[i],"Calm") == 6:
     velocidadViento = 0
     posRafagasStar = 0
     rafagas = 0
    else:
     idViento = i
     tamanioidViento = len(reporteClima[idViento])
     posVientoStar = int(string.find(reporteClima[idViento],"at"))
     posVientoEnd = int(string.find(reporteClima[idViento],"MPH"))
     posRafagasStar = int(string.find(reporteClima[idViento],"gusting to"))
     # 1 MP/H = 1.609 KM/H
     velocidadViento = int(float(reporteClima[idViento][posVientoStar+3:posVientoEnd-1])*1.609)
     if posRafagasStar > 0:
      rafagas = int(float(reporteClima[idViento][posRafagasStar+11:posRafagasStar+13])*1.609)
     else:
      rafagas = 0
  temperatura = int(reporteClima[idTemperatura][string.find(reporteClima[idTemperatura],"(")+1:string.find(reporteClima[idTemperatura],")")-1])
  try:
   termica = int(reporteClima[idTermica][string.find(reporteClima[idTermica],"(")+1:string.find(reporteClima[idTermica],")")-1])
  except NameError:
   termica = 0
  try:
   presion = reporteClima[idPresion][string.find(reporteClima[idPresion],"hPa")-5:string.find(reporteClima[idPresion],"hPa")-1]
  except NameError:
   presion = 0

  if temperatura > 15:
   carita = r"(=^.^=) "
  elif temperatura <= 15 and temperatura >= 10:
   carita = r"^_^'' "
  elif temperatura <= 9 and temperatura >= 5:
   carita = r"^^) "
  elif temperatura <= 4 and temperatura >= 1:
   carita = r"(O.o) "
  elif temperatura == 0:
   carita = r"(Y_Y) "
  elif temperatura <= -4 and temperatura >= -1:
   carita = r"(>.<) " 
  else:
   carita = r"(+_+) "
  
  try:
   if velocidadViento == 0:
    set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Viento: En calma - Presión atmosférica: '+presion+' hPa. - '+horaFecha)
   else:
    if termica == 0: 
     if rafagas > 0:
      set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Viento: '+str(velocidadViento)+' Km/h (ráfagas de '+str(rafagas)+'Km/h) - Presión atmosférica: '+presion+' hPa. - '+horaFecha)
     else:
      if presion > 0:
       set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Viento: '+str(velocidadViento)+'Km/h - Presión atmosférica: '+presion+' hPa. - '+horaFecha)
      else:
       set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Viento: '+str(velocidadViento)+'Km/h - '+horaFecha)
    else:
     if rafagas > 0:
      set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Térmica: '+str(termica)+'°C - Viento: '+str(velocidadViento)+' Km/h (ráfagas de '+str(rafagas)+'Km/h) - Presión atmosférica: '+presion+' hPa. - '+horaFecha)
     else:
      set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Térmica: '+str(termica)+'°C - Viento: '+str(velocidadViento)+' Km/h - Presión atmosférica: '+presion+' hPa. - '+horaFecha)
  except NameError:
   set_status(carita+'Río Gallegos: '+str(temperatura)+'°C - Viento: '+str(velocidadViento)+' Km/h - Presión atmosférica: '+presion+' hPa. - ['+horaFecha+']')
  time.sleep(300)



aquí el enlace para descargar la actualización:
Descargar (4 KB)

Para entender como configurar el resto leer el post anterior aqui:
http://drcalambre.blogspot.com/2010/06/como-el-clima-con-pidgin.html