# -*- coding: utf-8 -*-
# XChat translato spanish to english plugin
# Copyright (C) 2009 Jorge J. Lopez C. <lowlifebob[at]gmail[point]com>
# weblog :
http://www.lopz.org
import xchat
import httplib
__module_name__ = "pyTransXchat"
__module_version__ = "1.0"
__module_description__ = "pyTrans lang"
# Lang. support
"""
sq : albanés
de : alemán
ar : árabe
bg : búlgaro
ca : catalán
cs : checo
zh-CN : chino
ko : coreano
hr : croata
da : danés
sk : eslovaco
sl : esloveno
es : español
et : estonio
fi : finlandés
fr : francés
gl : gallego
el : griego
iw : hebreo
hi : hindi
nl : holandés
hu : húngaro
id : indonesio
en : inglés
it : italiano
ja : japonés
lv : letón
lt : lituano
mt : maltés
no : noruego
pl : polaco
pt : portugués
ro : rumano
ru : ruso
sr : serbio
sv : sueco
tl : tagalo
th : tailandés
tr : turco
uk : ucraniano
vi : vietnamita
"""
a = "es"
b = "en"
const_dom_trans = "translate.google.es"
const_url_trans = "/translate_a/t?client=t&text=%s&sl=" + a + "&tl=" + b
def quote(s, safe = '/'):
""" quote('abc def') -> 'abc%20def' """
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789' '_.-')
_safemaps = {}
cachekey = (safe, always_safe)
try:
safe_map = _safemaps[cachekey]
except KeyError:
safe += always_safe
safe_map = {}
for i in range(256):
c = chr(i)
safe_map[c] = (c in safe) and c or ('%%%02X' % i)
_safemaps[cachekey] = safe_map
res = map(safe_map.__getitem__, s)
return ''.join(res)
def download(dom, url, word=None):
if word:
headers = {
"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; es-ES; \
rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3",
"Accept": "text/html,application/xhtml+xml,application/xml; \
q=0.9,*/*;q=0.8",
"Accept-Language": "es-es,es;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Charset": "UTF-8,*",
"Keep-Alive": "300",
"Connection": "keep-alive"
}
# conectamos con el servidor
conn = httplib.HTTPConnection(dom, 80)
# hacemos la peticion a la imagen
conn.request ("GET", (url % word), None, headers)
resp = conn.getresponse()
return resp
else:
pass
def main(words, word_eol, userdata):
try:
words = " ".join(words[1:])
trans = download(const_dom_trans, const_url_trans, quote(words)).read()
resp = trans[1:-1]
xchat.command("msg %s %s" % (xchat.get_info("channel"), resp))
except:
xchat.prnt("use: /t words")
return xchat.EAT_ALL
xchat.hook_command("t", main, help="/t words")
xchat.prnt(__module_description__ + ", loaded!!")