views:

91

answers:

4

I want to create simple application that detects language(using Google API) of phrase and send it to corresponded search engine. For example, if search query is in Russian then I need to redirect it to Yandex.ru in all other cases to Google.

That's how I do this:

def get(self):                                                                        
   decoded = unicode(unquote(self.request.query), "windows-1251")
   text = decoded.encode("utf-8")            
   url = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q="+ quote(text)

   try:
      data = json.loads(urllib2.urlopen(url).read())                                
      redirectUrl = "http://www.google.com/search?q=" + text                        
      if data["responseData"]["language"] == 'ru':
          redirectUrl = "http://yandex.ru/yandsearch?text=" + text          
      self.redirect(redirectUrl)                                                           
    except urllib2.HTTPError, e:
      self.response.out.write( "HTTP error: %d" % e.code )
    except urllib2.URLError, e:
      self.response.out.write( "Network error: %s" % e.reason.args[1])

When I request this url "http://findinrightplace.appspot.com/q?test query" it redirects to google but redirection to yandex doesn't work (http://findinrightplace.appspot.com/q?тестовый запрос).

What I'm doing wrong?

A: 

I suggest using the Google Prediction API [http://code.google.com/apis/predict/]. You'll notice that the example on the main page is exactly what you are trying to do.

Travis J Webb
I don't see how this can help me. Google Language Detection is enough for me. I just need to understand why redirect to Yandex doesn't work.
Orsol
A: 

You're not quoting text when building redirectUrl. Try:

  ...
  redirectUrl = "http://www.google.com/search?q=" + quote(text)
  if data["responseData"]["language"] == 'ru':
      redirectUrl = "http://yandex.ru/yandsearch?text=" + quote(text)
  ...
adw
+1  A: 

You need to remove quote() from url = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q="+ quote(text), its returning a bad result for your Russian query.

I tested your code in my local python shell and it worked without quote(), but did not work with quote().

Scott
A: 

You are incorrect in assuming that the query string will be windows-1251 encoded. In the link you give, it's up to the web browser on how to encode it (since HTTP is also silent as to what the encoding of URLs should be). However, today, most browsers will assume that the URL must be encoded in UTF-8. As then language/detect also assumes that the query string is UTF-8 encoded (and URL escaped), you neither need to unquote nor decode the string at all. Also, yandex supports UTF-8 encoded query strings just fine. So putting this all together: try

def get(self):                                                                        
   text = self.request.query
   url = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=" + text

   try:
      data = json.loads(urllib2.urlopen(url).read())                                
      redirectUrl = "http://www.google.com/search?q=" + text                        
      if data["responseData"]["language"] == 'ru':
          redirectUrl = "http://yandex.ru/yandsearch?text=" + text          
      self.redirect(redirectUrl)                                                           
    except urllib2.HTTPError, e:
      self.response.out.write( "HTTP error: %d" % e.code )
    except urllib2.URLError, e:
      self.response.out.write( "Network error: %s" % e.reason.args[1])
Martin v. Löwis