def raw_yahoo_weather(location_id, metric=True): """ Fetches weather report from Yahoo! Note that this returns a lot more than weather. Parameters location_id: The WOEID of the location, get_woeid() can retreive it. metric: type of units. True for metric and False for US units Note that choosing metric units changes all the weather units to metric, for example, wind speed will be reported as kilometers per hour and barometric pressure as millibars. Returns: weather_data: a dictionary of weather data that exists in XML feed. See http://developer.yahoo.com/weather/#channel """ params = dict(w=location_id, u='c' if metric else 'f') url = api.build_url(api.API_YAHOO_WEATHER, params) handler = urllib2.urlopen(url) dom = minidom.parse(handler) handler.close() try: weather_data = {} weather_data['title'] = dom.getElementsByTagName('title')[0].firstChild.data weather_data['link'] = dom.getElementsByTagName('link')[0].firstChild.data ns_data_structure = { 'location': ('city', 'region', 'country'), 'units': ('temperature', 'distance', 'pressure', 'speed'), 'wind': ('chill', 'direction', 'speed'), 'atmosphere': ('humidity', 'visibility', 'pressure', 'rising'), 'astronomy': ('sunrise', 'sunset'), 'condition': ('text', 'code', 'temp', 'date') } for (tag, attrs) in ns_data_structure.iteritems(): weather_data[tag] = xml_get_ns_yahoo_tag(dom, api.API_YAHOO_WEATHER_NS, tag, attrs) weather_data['geo'] = {} weather_data['geo']['lat'] = dom.getElementsByTagName('geo:lat')[0].firstChild.data weather_data['geo']['long'] = dom.getElementsByTagName('geo:long')[0].firstChild.data weather_data['condition']['title'] = dom.getElementsByTagName('item')[0].getElementsByTagName('title')[0].firstChild.data weather_data['html_description'] = dom.getElementsByTagName('item')[0].getElementsByTagName('description')[0].firstChild.data forecasts = [] for forecast in dom.getElementsByTagNameNS(api.API_YAHOO_WEATHER_NS, 'forecast'): forecasts.append(xml_get_attrs(forecast,('date', 'low', 'high', 'text', 'code'))) weather_data['forecasts'] = forecasts dom.unlink() except IndexError: raise ValueError('Invalid Location') #weather_data['astronomy'] = dict([i, datetime.time(x) for i, x in weather_data['astronomy'].iteritems()]) return weather_data
def get_weather() -> str: while True: city_id = input('Input city id (q to quit): ') if city_id == 'q': break units = input('Input units (metric or imperial, default Kelvin): ') try: data = api.get_result(api.build_url(city_id, units)) if units == 'metric': output.get_output(data, 'C') elif units == 'imperial': output.get_output(data, 'F') else: output.get_output(data, 'K') except: print('Error')
def get_info_on_location(location): params = {'q': 'select * from geo.places where text="{0}"'.format(location.replace('"', r'\"')), 'format': 'json'} h = urllib2.urlopen(api.build_url(api.API_QUERY_CITY_DATA, params)) page = h.read() h.close() try: data = json.loads(page) except ValueError: raise ValueError('Invalid Location!') try: return data['query']['results']['place'][0] except KeyError: pass try: return data['query']['results']['place'] except KeyError: pass raise ValueError('Invalid Location!')
def proc_api(self, api_type, filtered): 'process user request' if api_type == 'weather': # get city id from api city = self.detect_location(filtered) url = api.build_url(self.api_list[api_type], city) response = api.api_get(url) if response == False: return response else: return filters.weather(response) else: joke = api.api_get(self.api_list[api_type]['url']) response = joke['value'] return response