lingvo.ru module

#$ neutron_plugin 01

import urllib, htmllib, formatter, htmlentitydefs

lingvo_encoding = 'cp1251'

class MyWriter(formatter.NullWriter):
    body = ''
    def send_paragraph(self,lines):
        self.body += "\n"
    def send_flowing_data(self,data):
        self.body += data
    def send_literal_data(self,data):
        self.body += data
    def send_label_data(self,data):
        self.body += "\n_" + data + "_\n"

def desc_ent(m,defs=htmlentitydefs.entitydefs):
    try:
        return defs[m.group(1)]
    except KeyError:
        return m.group(0)
def desc_amp(m):
    s = "\\u%04X" % int(m.group(1))
    return unichr(int(m.group(1))).encode(lingvo_encoding)
def descape(txt):
    ent = re.compile("&(\w+?);")
    amp = re.compile("&#([0-9]+);")
    ret = ent.sub(desc_ent, txt)
    return amp.sub(desc_amp, ret)
def eat(m):
    return ''

def detag(text):
    img = re.compile(r'<IMG.+?>')
    nob = text.replace('<B>','*').replace('</B>','*')
    nobr = nob.replace('<BR>', '').replace('<BR/>','')
    noimg = img.sub(eat,nobr)
    return noimg

def doLingvo(phrase):
    phrase = ' '.join(string.replace(phrase.strip(), "\n", ' ').split())
    params = urllib.urlencode( { 'words' : phrase.encode(lingvo_encoding,'replace'),
                         'Ln' : '1',
                         'lingvoaction' : 'translate',
                         'VTI-GROUP' : '0'
                     } )
    try:
        response = urllib.urlopen('http://www.lingvo.ru/lingvo/Translate.asp',params);
    except IOError, what:
        print "Unexpected error:", sys.exc_info()[0]

    html = response.read()
    response.close()
    regs =  [ re.compile(r'<DIV LANG=[rR][uU]>(.+?)<\/DIV>',re.M|re.S) ,
              re.compile(r'<DIV LANG=[eE][nN]-[uU][sS]>(.+?)<\/DIV>',re.M|re.S)]
    for reg in regs:
        match = reg.search(html)
        if match: break
    if not match: return 'Cant recognize result string.'
    tr = match.group(1)
    txt = descape(detag(tr))
    w = MyWriter()
    f = formatter.AbstractFormatter(w)
    p = htmllib.HTMLParser(f)
    p.feed(txt)
    p.close()
    out = w.body
    ret = unicode(out,lingvo_encoding)
    return ret

def handler_lingvo_lingvo(type, source, parameters):
        results = doLingvo(parameters)
        smsg(type, source, results)

register_command_handler(handler_lingvo_lingvo, 'lingvo', 0, 'Translate a word on Lingvo.ru.', 'lingvo ', ['lingvo neutron'])

as you can see it in russian, but may be helpful for someone

Just for reference, there is

Just for reference, there is a related plugin in gh0st-dev branch: lingvo_plugin.py

Syndicate content