def __init__(self, key, location, system = 'm'): BaseAPI.__init__(self, location, system) from urllib2 import urlopen, quote wu = urlopen('http://api.wunderground.com/api/%s/conditions/forecast/q/%s.json' % (str(key), quote(self.location()))) rawResponse = wu.read() wu.close() from json import loads response = loads(rawResponse) if 'error' not in response: if response['response']['version'] == self.API_VERSION: self.__currentWeather = response['current_observation'] self.__forecast = response['forecast']['simpleforecast']['forecastday'] self.__fullLocation = '' self.__temp = '' self.__text = '' self.__symbol = '' self.__humidity = '' self.__daysList = [] self.__symbolsList = [] self.__tempsList = [] self.__textsList = [] else: raise Exception('Unable to retrieve weather information for location %s' % self.location()) else: raise Exception('Unable to find weather information for %s.\nWeatherUngerground responded: %s' % (self.location(), response['error']['description']))
def __init__(self, location, system = 'm'): BaseAPI.__init__(self, location, system) from urllib2 import urlopen, quote # ask Google googleSocket = urlopen('http://www.google.com/ig/api?weather=%s&hl=en-gb' % quote(self.location())) # Google sends a one-line file that needs to be decoded from CP1252 & re-encoded in proper UTF-8 rawResponse = googleSocket.readline().decode('CP1252').encode('UTF-8') googleSocket.close() from xml.dom.minidom import parseString response = parseString(rawResponse) # we got a correct reply if response.hasChildNodes(): reply = response.firstChild # if reply is compatible if reply.getAttribute('version') == self.API_VERSION : # if there has been no error on Google's side weather = reply.firstChild if weather.firstChild.tagName != 'problem_cause': self.__currentWeather = weather.getElementsByTagName('current_conditions')[0] self.__forecast = weather.getElementsByTagName('forecast_conditions') self.__fullLocation = weather.firstChild.firstChild.getAttribute('data') self.__temp = '' self.__text = '' self.__symbol = '' self.__humidity = '' self.__daysList = [] self.__symbolsList = [] self.__tempsList = [] self.__textsList = [] else: raise Exception('Unable to retrieve weather information for location %s' % self.location()) else: raise Exception('API version %s not supported' % reply.getAttribute('version')) else: raise Exception('Empty reply')
def __init__(self, partnerkey, partnersecret): BaseAPI.__init__(self, partnerkey, partnersecret)