Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code for translation module #476

Merged
merged 1 commit into from
Mar 7, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions willie/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

http://willie.dftba.net
"""

from __future__ import unicode_literals
from willie import web
from willie.module import rule, commands, priority, example
import json
Expand All @@ -34,10 +34,10 @@ def configure(config):
config.translate.collect_mangle_lines = True


def translate(text, input='auto', output='en'):
def translate(text, in_lang='auto', out_lang='en'):
raw = False
if unicode(output).endswith('-raw'):
output = output[:-4]
if unicode(out_lang).endswith('-raw'):
out_lang = out_lang[:-4]
raw = True

headers = {
Expand All @@ -46,9 +46,18 @@ def translate(text, input='auto', output='en'):
'Gecko/20071127 Firefox/2.0.0.11'
}

result = web.get('http://translate.google.com/translate_a/t?' +
('client=t&sl=%s&tl=%s' % (input, output)) +
('&q=%s' % text), 40, headers=headers)
url_query = {
"client": "t",
"sl": in_lang,
"tl": out_lang,
"q": text,
}
query_string = "&".join(
"{key}={value}".format(key=key, value=value)
for key, value in url_query.items()
)
url = "http://translate.google.com/translate_a/t?{query}".format(query=query_string)
result = web.get(url, timeout=40, headers=headers)
if sys.version_info.major>=3:
result = result.decode()

Expand All @@ -74,19 +83,19 @@ def translate(text, input='auto', output='en'):
@priority('low')
def tr(bot, trigger):
"""Translates a phrase, with an optional language hint."""
input, output, phrase = trigger.groups()
in_lang, out_lang, phrase = trigger.groups()

phrase = phrase.encode('utf-8')

if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('Phrase must be under 350 characters.')

input = input or 'auto'
input = input.encode('utf-8')
output = output or 'en'
in_lang = in_lang or 'auto'
in_lang = in_lang.encode('utf-8')
out_lang = out_lang or 'en'

if input != output:
msg, input = translate(phrase, input, output)
if in_lang != out_lang:
msg, in_lang = translate(phrase, in_lang, out_lang)
if sys.version_info.major < 3 and isinstance(msg, str):
msg = msg.decode('utf-8')
if msg:
Expand Down