示例#1
0
文件: misc.py 项目: MTK358/TuxBot-Old
def translate(from_lang, to_lang, text):
    params = urllib.urlencode({"babelfishfrontpage": "yes",
                               "doit": "done",
                               "urltext": text,
                               "lp": from_lang + "_" + to_lang})
    try:
        response = urllib.urlopen2("http://babelfish.yahoo.com/translate_txt", params)
    except ioerror, e:
        return "(error: %s)" % (e)
示例#2
0
文件: misc.py 项目: MTK358/TuxBot-Old
def get_man_page_synopsis(section, name):
    stream = None
    try:
        stream = urllib.urlopen2(get_man_page(section, name))
        page = stream.read()
        match = re.match(r'.*<h2>Synopsis</h2>(.*)<h2>Description</h2>.*', page, flags = re.IGNORECASE | re.DOTALL)
        if not match:
            return None
        synopsis = match.group(1)
        synopsis = re.sub(r'<br\s*/?>', "\n", synopsis)
        synopsis = re.sub(r'<[^>]*>', "", synopsis)
        synopsis = synopsis.replace("&lt;", "<")
        synopsis = synopsis.replace("&gt;", ">")
        synopsis = synopsis.replace("&amp;", "&")
        return synopsis
    except IOError:
        return None
    finally:
        if stream:
            stream.close()