import random, re, sets, sys, time, urllib, urllib2

try:

    if len(sys.argv) == 3:
        lang = sys.argv[1]
        encoding = sys.argv[2]
    elif len(sys.argv) == 1:
        lang = "ru"
        encoding = "cp866"
    else:
        print "Usage: popyfon lang encoding"
        sys.exit(1)

    pattern1 = r'<option value="(\w\w)_(\w\w)">'
    pattern2 = r'<div style=padding:10px;>([^<]*)</div>'

    url1 = "http://babelfish.altavista.com/"
    url2 = url1 + "tr"

    log = open("pofyfon.log", "a")

    history = sets.Set()
    timeout = 1

    link = urllib.urlopen(url1)
    page = link.read()

    languages = sets.Set(re.findall(pattern1, page))

    print len(languages), "directions"
    print

    image = raw_input(lang + " ")
    line = image.decode(encoding).encode("utf-8")
    line = " ".join(line.split())

    while(True):

        history.add((lang, line))
        print >> log, lang, line
        time.sleep(timeout)
        pair = random.choice([pair for pair in languages if pair[0] == lang])

        form = {}
        form["doit"] = "done"
        form["intl"] = "1"
        form["tt"] = "urltext"
        form["trtext"] = line
        form["lp"] = "%s_%s" % pair

        data = urllib.urlencode(form)
        req = urllib2.Request(url2, data)
        req.add_header("Accept-Charset", "utf-8")

        link = urllib2.urlopen(req)
        page = link.read()

        line = re.search(pattern2, page).group(1)
        line = " ".join(line.split())
        lang = pair[1]
        image = line.decode("utf-8").encode(encoding, "replace")

        if (lang, line) in history:
            print lang, "*", image
        else:
            print lang, image

except (AttributeError, KeyboardInterrupt), what:

    print >> log
    log.close()

    print
    print what.__class__.__name__

