Exemple #1
0
    def _api_request(self, location, lang):
        """
            makes a api-request and parses result
        """
        result = {'info':{}, 'current':{},'forecast':[]}
        #lang='en'
        try:
            url_term = urllib.urlencode({'weather':location.encode('utf-8').lower(),'hl':lang})
            
            api_url = 'http://www.google.de/ig/api?%s' % (url_term)

            response = load_url(api_url)
            if response:
                response = self._fix_invalid_xml(response)
                weather = ElementTree.fromstring(response)

                for e in weather.find('weather/forecast_information').getchildren():
                    result['info'][e.tag] = e.attrib.get('data').encode('utf-8','ignore')

                for e in weather.find('weather/current_conditions').getchildren():
                    result['current'][e.tag] = e.attrib.get('data').encode('utf-8','ignore')

                match = 'weather/forecast_conditions'
                forecasts = [e.getchildren() for e in weather.findall(match)]
                for fcast in forecasts:
                    day = {}
                    for e in fcast:
                        day[e.tag] = e.attrib.get('data').encode('utf-8','ignore')
                    result['forecast'].append(day)
        except:
            raise EnvironmentError, 'Forecast failed'
        return result
Exemple #2
0
    def _api_request(self, mode, params):
            
        url_params = urlencode( params )
        api_url = 'http://ajax.googleapis.com/ajax/services/search/'+mode+'?%s' % (url_params)

        response = load_url(api_url)
        api_response  = simplejson.loads(response)
        if api_response.get('responseStatus') == 200:
            return api_response
        else:
            return None
Exemple #3
0
    def _api_request(self, term, target_lang, source_lang=''):
        result = ''
        detected_lang = None
        try:
            url_term = quote_plus(term.encode('utf-8').lower())
            api_url = 'http://www.google.com/uds/Gtranslate?context=22&q=%s&langpair=%s|%s&key=notsupplied&v=1.0' % (url_term, source_lang, target_lang)

            response = load_url(api_url)
            api_response  = simplejson.loads(response)

            if api_response.get('responseStatus') == 200:
                translate_data = api_response.get('responseData')
                if not source_lang:
                    source_lang = translate_data.get('detectedSourceLanguage')

                result = translate_data.get('translatedText')
        except:
            raise EnvironmentError, 'Translation failed'
        return result, source_lang
Exemple #4
0
    def request(self, query, **kwargs):

        base_url = "http://query.yahooapis.com/v1/public/yql?q=%s&format=json"
        base_url += "&diagnostics=false&callback="
        community_param = None
        if self.community:
            community_param = "&env=" + urllib.quote_plus("store://datatables.org/alltableswithkeys")

        yql_query = urllib.quote_plus(query.encode("utf-8"))
        final_url = base_url % (yql_query)

        if self.community:
            final_url += "&env=" + urllib.quote_plus("store://datatables.org/alltableswithkeys")

        response = load_url(final_url)

        api_data = simplejson.loads(response)
        result = api_data.get("query", {}).get("results", {})

        return result