Created
April 6, 2015 20:17
-
-
Save aaronlelevier/c20b96b4d34c1957bbc9 to your computer and use it in GitHub Desktop.
Make Yahoo! Weather API call for the weather in Las Vegas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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