weathers.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/python2
  2. import sys
  3. import urllib2
  4. from string import maketrans
  5. #from xml.sax import make_parser, handler
  6. from xml.sax import handler, parseString
  7. class ElementProcesser(handler.ContentHandler):
  8. def startElement(self, name, attrs):
  9. if name == "city":
  10. print "<separator label=\"" + attrs["data"].encode('utf-8','replace') + "\" />"
  11. elif name == "current_conditions":
  12. print "<separator label='Current conditions' />"
  13. elif name == "condition":
  14. print "<item label='Weather: " + attrs["data"] + "' />"
  15. elif name == "humidity":
  16. print "<item label='" + attrs["data"] + "' />"
  17. elif name == "wind_condition":
  18. print "<item label='" + attrs["data"] + "' />"
  19. elif name == "day_of_week":
  20. print "<separator label='" + self.getDayOfWeek(attrs["data"]) + "' />"
  21. #Celsius
  22. #elif name == "temp_c":
  23. #print "<item label='Temperature " + attrs["data"] + " C' />"
  24. #elif name == "low":
  25. #print "<item label='Minimum " + attrs["data"] + " C' />"
  26. #elif name == "high":
  27. #print "<item label='Maximum " + attrs["data"] + " C' />"
  28. #Fahrenheit
  29. elif name == "temp_f":
  30. print "<item label='Temperature " + attrs["data"] + " F' />"
  31. elif name == "low":
  32. print "<item label='Minimum " + attrs["data"] + " F' />"
  33. elif name == "high":
  34. print "<item label='Maximum " + attrs["data"] + " F' />"
  35. def endElement(self, name):
  36. if name == "current_conditions":
  37. print "<separator label='Forecast' />"
  38. def startDocument(self):
  39. print '<openbox_pipe_menu>'
  40. def endDocument(self):
  41. print '</openbox_pipe_menu>'
  42. def getDayOfWeek(self,day):
  43. #English
  44. if day == "Mon":
  45. return "Monday"
  46. elif day == "Tue":
  47. return "Tuesday"
  48. elif day == "Wed":
  49. return "Wednesday"
  50. elif day == "Thu":
  51. return "Thursday"
  52. elif day == "Fri":
  53. return "Friday"
  54. elif day == "Sat":
  55. return "Saturday"
  56. elif day == "Sun":
  57. return "Sunday"
  58. else:
  59. return day
  60. # You should use your local version of google to have the messages in your language and metric system
  61. for arg in sys.argv: str = arg
  62. f = urllib2.urlopen("http://www.google.com/ig/api?ie=utf8&oe=utf8&weather="+str)
  63. xml = f.read()
  64. f.close()
  65. #Avoid problems with non english characters
  66. trans=maketrans("\xe1\xe9\xed\xf3\xfa","aeiou")
  67. xml = xml.translate(trans)
  68. #print xml
  69. #parser.parse("http://www.google.es/ig/api?weather="+sys.argv[1])
  70. parseString(xml,ElementProcesser())