sweb

Some web helper scripts.
git clone git://r-36.net/sweb
Log | Files | Refs | LICENSE

translate (2792B)


      1 #!/usr/bin/env python2
      2 # See the LICENSE file for copyright and license details.
      3 
      4 import urllib2
      5 import urllib
      6 import os
      7 import sys
      8 import json
      9 import getopt
     10 import requests
     11 from lxml import etree
     12 
     13 def prlangs(name, l, short=False):
     14 	keys = l.keys()
     15 	keys.sort()
     16 
     17 	if short == True:
     18 		sys.stdout.write("%s %s\n" % (name,
     19 			", ".join(keys)))
     20 	else:
     21 		print name
     22 		for k in keys:
     23 			print "%s: %s" % (k.rjust(10), l[k])
     24 
     25 def getoptions(sel):
     26 	langs = {}
     27 
     28 	opts = sel.xpath("//option")
     29 	for i in opts:
     30 		val = i.attrib["value"]
     31 		if val != "separator":
     32 			langs[val] = i.text
     33 	
     34 	return langs
     35 
     36 def getlanglist():
     37 	headers = {"User-Agent":  "Mozilla/5.0 (X11; U; Linux x86_64; "
     38 		"en-US; "
     39 		"rv:1.9.2.7) Gecko/20100721 Firefox/3.6.7"}
     40 	req = urllib2.Request("http://translate.google.com", 
     41 			headers=headers)
     42 	opener = urllib2.build_opener()
     43 	parser = etree.HTMLParser()
     44 
     45 	fd = opener.open(req)
     46 	xml = etree.parse(fd, parser)
     47 
     48 	sels = xml.xpath("//select")
     49 	flangs = getoptions(sels[0])
     50 	tlangs = getoptions(sels[1])
     51 
     52 	return (flangs, tlangs)
     53 
     54 def dotranslate(tfrom, tto, text):
     55 	data = urllib.urlencode({ 'sl': tfrom, 'tl': tto, 'text': text })
     56 	headers = {"User-Agent":  "Mozilla/5.0 (X11; U; Linux x86_64; "
     57 		"en-US; "
     58 		"rv:1.9.2.7) Gecko/20100721 Firefox/3.6.7"}
     59 	req = urllib2.Request("http://translate.google.com", data,
     60 			headers)
     61 	opener = urllib2.build_opener()
     62 	parser = etree.HTMLParser()
     63 
     64 	fd = opener.open(req)
     65 	xml = etree.parse(fd, parser)
     66 
     67 	sels = xml.xpath("//span[@id=\"result_box\"]/span")
     68 	return "\n".join([" ".join(i.itertext()) for i in sels])
     69 
     70 def usage(app):
     71 	app = os.path.basename(app)
     72 	sys.stderr.write("usage: %s [-h] [-f from] [-t to] [[-s] -l|[from to] text]\n" % (app))
     73 	sys.exit(1)
     74 
     75 def main(args):
     76 	try:
     77 		opts, largs = getopt.getopt(args[1:], "hslf:t:")
     78 	except getopt.GetoptError, err:
     79 		print str(err)
     80 		usage(args[0])
     81 	
     82 	dolist = False
     83 	doshort = True
     84 	tfrom = "auto" 
     85 	tto = None
     86 	for o, a in opts:
     87 		if o == "-h":
     88 			usage(args[0])
     89 		elif o == "-l":
     90 			dolist = True
     91 		elif o == "-s":
     92 			doshort = False
     93 		elif o == "-f":
     94 			tfrom = a
     95 		elif o == "-t":
     96 			tto = a
     97 		else:
     98 			assert False, "unhandled option"
     99 
    100 	if dolist == True:
    101 		(flang, tlang) = getlanglist()
    102 
    103 		prlangs("FROM:", flang, doshort)
    104 		print
    105 		prlangs("TO:", tlang, doshort)
    106 
    107 		return 0
    108 
    109 	if tto == None:
    110 		if len(largs) < 1:
    111 			usage(args[0])
    112 		elif len(largs) < 2:
    113 			tto = largs[0]
    114 			text = sys.stdin.read()
    115 		elif len(largs) < 3:
    116 			tfrom = largs[0]
    117 			tto = largs[1]
    118 			text = sys.stdin.read()
    119 		else:
    120 			tfrom = largs[0]
    121 			tto = largs[1]
    122 			text = ' '.join(largs[2:])
    123 	else:
    124 		if len(largs) < 1:
    125 			text = sys.stdin.read()
    126 		else:
    127 			text = ' '.join(largs)
    128 
    129 	result = dotranslate(tfrom, tto, text)
    130 	print result
    131 
    132 	return 0
    133 
    134 if __name__ == "__main__":
    135 	sys.exit(main(sys.argv))
    136