Guest User

Openbox Weather Pipe Menu

a guest
Feb 9th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # This is GNU whatever blah blah. Simple stuff
  5. # Pass this around, credit my good self ([email protected]) or /u/jampola on reddit
  6. # All I wanted was a simple weather script to use as a pipe menu
  7. # and here it is.
  8. # ps: openbox rulz y0!!!111
  9. #
  10. # INSTRUCTIONS:
  11. # change the city_id (city list is here: http://www.openweathermap.org/)
  12. # to you chosen city and the temp var to your chosen unit
  13. # of measure.
  14. # add the following to your meny.xml file:
  15. # <menu id="proc-menu" label="Weather" execute="~/code/test.py" />
  16. # and you're done.
  17. # Enjoy!
  18.  
  19. import urllib2
  20. import xml.etree.ElementTree as ET
  21. import sys
  22.  
  23. city_id = "Bangkok" # Enter your city
  24. temp_unit = 1 # 1 = Celcius (272.15), 2 = Fahrenheit(457.87)
  25.  
  26. #url for weather api
  27. url = 'http://api.openweathermap.org/data/2.5/weather?q='+city_id+'&mode=xml'
  28.  
  29. request = urllib2.Request(url, headers={"Accept" : "application/xml"})
  30. u = urllib2.urlopen(request)
  31. tree = ET.parse(u)
  32. root = tree.getroot()
  33.  
  34. for child in root.findall('temperature'):
  35.     attr = child.attrib
  36.     kelvin = attr.get('value')
  37.     kelvin_to_float = float(kelvin)
  38.  
  39.     if temp_unit == 1:
  40.         temp_converted = (kelvin_to_float - 273)
  41.         temp_converted_to_s = round(temp_converted,2)
  42.         temp_symbol = "°C"
  43.     elif temp_unit == 2:
  44.         temp_converted = (((kelvin_to_float - 273) * 1.8 ) + 32)
  45.         temp_converted_to_s = round(temp_converted,2)
  46.         temp_symbol = "°F"
  47.  
  48. for child in root.findall('city'):
  49.     attr = child.attrib
  50.     city_name = attr.get('name')
  51.     country_name = child.find('country').text
  52.  
  53. for child in root.findall('lastupdate'):
  54.     attr = child.attrib
  55.     last_updated = attr.get('value')
  56.  
  57. # print "<openbox_pipe_menu>Currently in " + city_name + "," + country_name + " it is " + str(temp_converted_to_s) + temp_symbol +"</openbox_pipe_menu>"
  58. print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  59. print "<openbox_pipe_menu>"
  60. print "  <separator />"
  61. print "<item label=" + '"' + city_name + "," + country_name + '"' + "/>"
  62. print "<item label=" + '"Currently ' + str(temp_converted_to_s) + temp_symbol + '"' + "/>"
  63. print "<item label=" + '"Last Updated ' + last_updated + '"' + "/>"
  64. print "  <separator />"
  65. print "</openbox_pipe_menu>"
Advertisement
Add Comment
Please, Sign In to add comment