Skip to content

Instantly share code, notes, and snippets.

@joshhodgson
Created May 15, 2016 13:51
Show Gist options
  • Save joshhodgson/3d40e8d021701c13fe843ad59990d776 to your computer and use it in GitHub Desktop.
Save joshhodgson/3d40e8d021701c13fe843ad59990d776 to your computer and use it in GitHub Desktop.
Yahoo Weather getter
import requests
def getWeather(city):
query = "select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22"+ city +"%22)%20and%20u%3D%27c%27&format=json"
url = "https://query.yahooapis.com/v1/public/yql?q=" + query
r=requests.get(url)
if r.json()['query']['results']!=None:
results = r.json()['query']['results']['channel']
foundcity = results['location']['city']
forecast = results['item']['forecast']
current = results['item']['condition']
else:
print("Error! Could not find results for " + city+ ". Instead, here is Bristol")
return(getWeather('Bristol'))
return({'city': foundcity, 'forecast': forecast, 'current': current})
data = getWeather('Bristol')
'''
Things that getWeather() gives you:
data['city'] : the name of the city it actually found
data['forecast'] : a list of different forecasts
data['forecast'][0] is todays forcast and you can put at the end ['something'] where something is text, low, date, day, high
data['forecast'][3]['high'] is the highest temperature for in 3 days time
data['current']['something'] where something is temp or text
'''
#here is an example of what you could do
print("The temperature in " + data['city'] + " is currently " + data['current']['temp'] + " degrees.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment