Created
December 3, 2013 23:24
-
-
Save tyrelsouza/7779553 to your computer and use it in GitHub Desktop.
Get Weather from worldweatheronline api.
This file contains 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
import urllib2 | |
import json | |
import re | |
def getWeather(self, area_dirty="Keene, NH"): | |
""" | |
Returns the current weather in a given location | |
""" | |
API_KEY = "API KEY HERE" | |
#try: | |
if True: | |
area = re.sub('[\W_]+', '+', area_dirty) | |
url = 'http://free.worldweatheronline.com/feed/weather.ashx?q=%s&format=json&num_of_days=2&key=%s' % (area, API_KEY) | |
print url | |
file = urllib2.urlopen(url) | |
data = json.loads(file.read()) | |
file.close() | |
current = data['data']['current_condition'][0] | |
# Compile the great string to say the weather. | |
print current | |
weather = "In %s it's %s C (%s F). Most recent conditions are: %s" % ( | |
area_dirty, | |
current['temp_C'], | |
current['temp_F'], | |
current['weatherDesc'][0]['value'], | |
) | |
#except Exception, e: | |
#print e | |
#return "Cannot find weather information for %s" % area_dirty | |
return weather |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment