Google App Engine UrlFetch - 网址中含有特殊字符的问题
我正在使用 Google 翻译通过以下网址将一段文本转换为语音:http://translate.google.com/translate_tts?tl=%s&q=%s
其中参数tl
包含要转换为语音的文本语言的语言代码,q
包含要转换的文本。
普通单词(没有特殊字符)返回正确的音频文件。
因此,在我的应用程序中,这就是我所做的(no
是挪威语的语言代码):
url = "http://translate.google.com/translate_tts?tl=%s&q=%s" % ('no', urllib.quote('kjendis'))
#url = http://translate.google.com/translate_tts?tl=no&q=kjendis
self.response.headers["Content-Type"] = "audio/mpeg"
self.response.out.write(urlfetch.fetch(url).content)
这会返回正确的声音。
顺便说一句,我正在使用普通的网络应用程序。
但是当我有一个带有特殊字符(vår)的单词时,有些事情就不对劲了。 生成的网址为 http://translate.google.com/translate_tts?tl=no&q=v%C3%A5r
。 ( å 已正确转换为百分比编码)
当使用浏览器打开该 url 时,我得到正确的声音,但是当使用 urlfetch.fetch 读取相同的 url 时,返回的声音不正确。
这里出了什么问题?我只能假设 fetch 正在以某种方式改变 url。
I'm using Google Translate to convert a piece of text to speech with this url:http://translate.google.com/translate_tts?tl=%s&q=%s
Where the parameter tl
contains the language code of the language of the text you want converted to speech, and q
contains the text you want converted.
Normal words (without special characters) return the correct audio file.
So in my application this is what I do (no
is the language code for Norwegian):
url = "http://translate.google.com/translate_tts?tl=%s&q=%s" % ('no', urllib.quote('kjendis'))
#url = http://translate.google.com/translate_tts?tl=no&q=kjendis
self.response.headers["Content-Type"] = "audio/mpeg"
self.response.out.write(urlfetch.fetch(url).content)
This returns the correct sound.
I'm using plain webapp btw.
But when I have a word with a special character in it (vår) something isn't right.
The url generated is http://translate.google.com/translate_tts?tl=no&q=v%C3%A5r
. (the å is correctly transformed to percent encoding)
When opening that url with my browser I get the correct sound, but when using urlfetch.fetch to read the same url the sound returned is not correct.
What is going wrong here? I can only assume that fetch is altering the url somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,问题不是 App Engine 问题,而是与 Google Translate url 处理不同用户代理的方式有关。
示例:
当使用 MyOpener 的 Firefox 用户代理字符串时,一切都按预期工作,但是当使用其他用户字符串时,返回的声音不正确。
Apparently the problem is not an App Engine problem, but it has to do with the way the Google Translate url handles different user agents.
An example:
When using the Firefox user agent string for MyOpener everything works as expected, but when using the other user string the sound returned is not correct.