Skip to content

Instantly share code, notes, and snippets.

@tyrelsouza
Created December 3, 2013 23:24
Show Gist options
  • Save tyrelsouza/7779553 to your computer and use it in GitHub Desktop.
Save tyrelsouza/7779553 to your computer and use it in GitHub Desktop.
Get Weather from worldweatheronline api.
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