Skip to content

Instantly share code, notes, and snippets.

@aaronlelevier
Created April 6, 2015 20:17
Show Gist options
  • Select an option

  • Save aaronlelevier/c20b96b4d34c1957bbc9 to your computer and use it in GitHub Desktop.

Select an option

Save aaronlelevier/c20b96b4d34c1957bbc9 to your computer and use it in GitHub Desktop.
Make Yahoo! Weather API call for the weather in Las Vegas
def get_weather(url="http://weather.yahooapis.com/forecastrss?w=12795483&u=f"):
try:
r = requests.get(url)
# parse bytes into a text string
text = r.content.decode("utf-8")
#convert xml text to OrderedDict
doc = xmltodict.parse(text)
# item contains all the main temp info
title = doc['rss']['channel']['title']
condition = doc['rss']['channel']['item']['yweather:condition']
weather = condition['@text']
temp = condition['@temp']
return "{0}. {1}. {2}°F.".format(title, weather, temp)
except requests.ConnectionError:
return "Sorry, the weather connection failed. Weather currently unavailable."
except KeyError:
return "Weather currently unavailable."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment