Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # This is GNU whatever blah blah. Simple stuff
- # Pass this around, credit my good self ([email protected]) or /u/jampola on reddit
- # All I wanted was a simple weather script to use as a pipe menu
- # and here it is.
- # ps: openbox rulz y0!!!111
- #
- # INSTRUCTIONS:
- # change the city_id (city list is here: http://www.openweathermap.org/)
- # to you chosen city and the temp var to your chosen unit
- # of measure.
- # add the following to your meny.xml file:
- # <menu id="proc-menu" label="Weather" execute="~/code/test.py" />
- # and you're done.
- # Enjoy!
- import urllib2
- import xml.etree.ElementTree as ET
- import sys
- city_id = "Bangkok" # Enter your city
- temp_unit = 1 # 1 = Celcius (272.15), 2 = Fahrenheit(457.87)
- #url for weather api
- url = 'http://api.openweathermap.org/data/2.5/weather?q='+city_id+'&mode=xml'
- request = urllib2.Request(url, headers={"Accept" : "application/xml"})
- u = urllib2.urlopen(request)
- tree = ET.parse(u)
- root = tree.getroot()
- for child in root.findall('temperature'):
- attr = child.attrib
- kelvin = attr.get('value')
- kelvin_to_float = float(kelvin)
- if temp_unit == 1:
- temp_converted = (kelvin_to_float - 273)
- temp_converted_to_s = round(temp_converted,2)
- temp_symbol = "°C"
- elif temp_unit == 2:
- temp_converted = (((kelvin_to_float - 273) * 1.8 ) + 32)
- temp_converted_to_s = round(temp_converted,2)
- temp_symbol = "°F"
- for child in root.findall('city'):
- attr = child.attrib
- city_name = attr.get('name')
- country_name = child.find('country').text
- for child in root.findall('lastupdate'):
- attr = child.attrib
- last_updated = attr.get('value')
- # print "<openbox_pipe_menu>Currently in " + city_name + "," + country_name + " it is " + str(temp_converted_to_s) + temp_symbol +"</openbox_pipe_menu>"
- print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
- print "<openbox_pipe_menu>"
- print " <separator />"
- print "<item label=" + '"' + city_name + "," + country_name + '"' + "/>"
- print "<item label=" + '"Currently ' + str(temp_converted_to_s) + temp_symbol + '"' + "/>"
- print "<item label=" + '"Last Updated ' + last_updated + '"' + "/>"
- print " <separator />"
- print "</openbox_pipe_menu>"
Advertisement
Add Comment
Please, Sign In to add comment