Beispiel #1
0
 def seo_suggest(self, keywords=None, lang=None):
     language = lang.split("_")
     url = "http://google.com/complete/search"
     try:
         req = urllib2.Request(
             "%s?%s"
             % (
                 url,
                 werkzeug.url_encode(
                     {
                         "ie": "utf8",
                         "oe": "utf8",
                         "output": "toolbar",
                         "q": keywords,
                         "hl": language[0],
                         "gl": language[1],
                     }
                 ),
             )
         )
         request = urllib2.urlopen(req)
     except (urllib2.HTTPError, urllib2.URLError):
         return []
     xmlroot = ET.fromstring(request.read())
     return json.dumps([sugg[0].attrib["data"] for sugg in xmlroot if len(sugg) and sugg[0].attrib["data"]])
    def _do_request(self,
                    cr,
                    uid,
                    uri,
                    params={},
                    headers={},
                    type='POST',
                    preuri="https://www.googleapis.com",
                    context=None):
        if context is None:
            context = {}
        """ Return a tuple ('HTTP_CODE', 'HTTP_RESPONSE') """
        _logger.debug(
            "Uri: %s - Type : %s - Headers: %s - Params : %s !" %
            (uri, type, headers,
             werkzeug.url_encode(params) if type == 'GET' else params))

        status = 418
        response = ""
        ask_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        try:
            if type.upper() == 'GET' or type.upper() == 'DELETE':
                data = werkzeug.url_encode(params)
                req = urllib2.Request(preuri + uri + "?" + data)
            elif type.upper() == 'POST' or type.upper(
            ) == 'PATCH' or type.upper() == 'PUT':
                req = urllib2.Request(preuri + uri, params, headers)
            else:
                raise (
                    'Method not supported [%s] not in [GET, POST, PUT, PATCH or DELETE]!'
                    % (type))
            req.get_method = lambda: type.upper()

            request = urllib2.urlopen(req, timeout=TIMEOUT)
            status = request.getcode()

            if int(status) in (204, 404):  # Page not found, no response
                response = False
            else:
                content = request.read()
                response = simplejson.loads(content)

            try:
                ask_time = datetime.strptime(request.headers.get('date'),
                                             "%a, %d %b %Y %H:%M:%S %Z")
            except:
                pass
        except urllib2.HTTPError, e:
            if e.code in (204, 404):
                status = e.code
                response = ""
            else:
                _logger.exception("Bad google request : %s !" % e.read())
                if e.code in (400, 401, 410):
                    raise e
                raise self.pool.get('res.config.settings').get_config_warning(
                    cr,
                    _("Something went wrong with your request to google"),
                    context=context)
Beispiel #3
0
 def seo_suggest(self, keywords):
     url = "http://google.com/complete/search"
     try:
         req = urllib2.Request("%s?%s" % (url, werkzeug.url_encode({
             'ie': 'utf8', 'oe': 'utf8', 'output': 'toolbar', 'q': keywords})))
         request = urllib2.urlopen(req)
     except (urllib2.HTTPError, urllib2.URLError):
         return []
     xmlroot = ET.fromstring(request.read())
     return json.dumps([sugg[0].attrib['data'] for sugg in xmlroot if len(sugg) and sugg[0].attrib['data']])
Beispiel #4
0
 def seo_suggest(self, keywords):
     url = "http://google.com/complete/search"
     try:
         req = urllib2.Request("%s?%s" % (url, werkzeug.url_encode({
             'ie': 'utf8', 'oe': 'utf8', 'output': 'toolbar', 'q': keywords})))
         request = urllib2.urlopen(req)
     except (urllib2.HTTPError, urllib2.URLError):
         return []
     xmlroot = ET.fromstring(request.read())
     return json.dumps([sugg[0].attrib['data'] for sugg in xmlroot if len(sugg) and sugg[0].attrib['data']])
Beispiel #5
0
    def _do_request(
        self, cr, uid, uri, params={}, headers={}, type="POST", preuri="https://www.googleapis.com", context=None
    ):
        if context is None:
            context = {}

        """ Return a tuple ('HTTP_CODE', 'HTTP_RESPONSE') """
        _logger.debug(
            "Uri: %s - Type : %s - Headers: %s - Params : %s !"
            % (uri, type, headers, werkzeug.url_encode(params) if type == "GET" else params)
        )

        status = 418
        response = ""
        ask_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        try:
            if type.upper() == "GET" or type.upper() == "DELETE":
                data = werkzeug.url_encode(params)
                req = urllib2.Request(preuri + uri + "?" + data)
            elif type.upper() == "POST" or type.upper() == "PATCH" or type.upper() == "PUT":
                req = urllib2.Request(preuri + uri, params, headers)
            else:
                raise ("Method not supported [%s] not in [GET, POST, PUT, PATCH or DELETE]!" % (type))
            req.get_method = lambda: type.upper()

            request = urllib2.urlopen(req, timeout=TIMEOUT)
            status = request.getcode()

            if int(status) in (204, 404):  # Page not found, no response
                response = False
            else:
                content = request.read()
                response = json.loads(content)

            try:
                ask_time = datetime.strptime(request.headers.get("date"), "%a, %d %b %Y %H:%M:%S %Z")
            except:
                pass
        except urllib2.HTTPError, e:
            if e.code in (204, 404):
                status = e.code
                response = ""
            else:
                _logger.exception("Bad google request : %s !" % e.read())
                if e.code in (400, 401, 410):
                    raise e
                raise self.pool.get("res.config.settings").get_config_warning(
                    cr, _("Something went wrong with your request to google"), context=context
                )
Beispiel #6
0
    def _do_request(self, cr, uid, uri, params={}, headers={}, type='POST', preuri="https://www.googleapis.com", context=None):
        context = dict(context or {})

        """ Return a tuple ('HTTP_CODE', 'HTTP_RESPONSE') """
        _logger.debug("Uri: %s - Type : %s - Headers: %s - Params : %s !" % (uri, type, headers, werkzeug.url_encode(params) if type == 'GET' else params))

        status = 418
        response = ""
        try:
            if type.upper() == 'GET' or type.upper() == 'DELETE':
                data = werkzeug.url_encode(params)
                req = urllib2.Request(preuri + uri + "?" + data)
            elif type.upper() == 'POST' or type.upper() == 'PATCH' or type.upper() == 'PUT':
                req = urllib2.Request(preuri + uri, params, headers)
            else:
                raise ('Method not supported [%s] not in [GET, POST, PUT, PATCH or DELETE]!' % (type))
            req.get_method = lambda: type.upper()

            request = urllib2.urlopen(req)
            status = request.getcode()

            if int(status) in (204, 404):  # Page not found, no response
                response = False
            else:
                content = request.read()
                response = simplejson.loads(content)

            if context.get('ask_time'):
                try:
                    date = datetime.strptime(request.headers.get('date'), "%a, %d %b %Y %H:%M:%S %Z")
                except:
                    date = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
                context['ask_time'] = date
        except urllib2.HTTPError, e:
            if e.code in (400, 401, 410):
                raise e

            _logger.exception("Bad google request : %s !" % e.read())
            raise self.pool.get('res.config.settings').get_config_warning(cr, _("Something went wrong with your request to google"), context=context)