Skip to content

Instantly share code, notes, and snippets.

@hassanabidpk
Forked from allieus/sujinlee_weather_yahoo.py
Created November 29, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassanabidpk/a9c48483b3c060a9712b to your computer and use it in GitHub Desktop.
Save hassanabidpk/a9c48483b3c060a9712b to your computer and use it in GitHub Desktop.
이수진님 날씨 스크립트 개선
# https://github.com/sujinleeme/my-python-journey/blob/master/PR4E/yahoo-weatherAPI.py
import requests
def get_weather(city):
baseurl = 'https://query.yahooapis.com/v1/public/yql'
yql_query = "SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places(1) WHERE text='%s')" % city
params = {'q': yql_query, 'format': 'json'}
data = requests.get(baseurl, params=params).json()
if not data['query']['count']:
return 'Not Found'
try:
channel = data['query']['results']['channel']
location = channel['location']
location_summary = ', '.join((location['city'], location['country'], location['region']))
condition = channel['item']['condition']
word = condition['text']
temp = '%sF/%dC' % (condition['temp'], int(celsius(condition['temp'])))
time = condition['date']
return '''Current weather in %s: %s, %s (%s)''' % (location_summary, word, temp, time)
except KeyError:
return 'Not Found'
def celsius(fahrenheit):
'''change Fahrenheit to Celsius'''
return ((int(fahrenheit) - 32) * 5/9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment