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?
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