limestone

A KDE marble commandline interface.
git clone git://r-36.net/limestone
Log | Files | Refs | LICENSE

geopos (1603B)


      1 #!/usr/bin/env python
      2 # coding=utf-8
      3 #
      4 # Copy me if you can.
      5 # by 20h
      6 #
      7 # usage: geopos [-gjri] position description
      8 #   -g - produce geo: URI
      9 #   -j - print JSON output
     10 #   -i - get position of some IP
     11 #   -r - do reverse search of a position to some description
     12 #
     13 
     14 import sys
     15 import os
     16 import geocoder
     17 import getopt
     18 
     19 def usage(app):
     20 	app = os.path.basename(app)
     21 	sys.stderr.write("usage: %s [-gjri] loc\n" % (app))
     22 	sys.exit(1)
     23 
     24 def main(args):
     25 	try:
     26 		opts, largs = getopt.getopt(args[1:], "hgjri")
     27 	except getopt.GetoptError as err:
     28 		print(str(err))
     29 		usage(args[0])
     30 	
     31 	printjson = False
     32 	isreverse = False
     33 	isip = False
     34 	dogeouri = False
     35 	for o, a in opts:
     36 		if o == "-h":
     37 			usage(args[0])
     38 		elif o == "-g":
     39 			dogeouri = True
     40 		elif o == "-j":
     41 			printjson = True
     42 		elif o == "-i":
     43 			isip = True
     44 		elif o == "-r":
     45 			isreverse = True
     46 		else:
     47 			assert False, "unhandled option"
     48 
     49 	if len(largs) < 1:
     50 		usage(args[0])
     51 
     52 	g = None
     53 	sarg = " ".join(largs)
     54 	if isreverse == True:
     55 		g = geocoder.reverse(sarg)
     56 	elif isip == True:
     57 		g = geocoder.ip(sarg)
     58 	else:
     59 		g = geocoder.google(sarg)
     60 
     61 	retval = 1
     62 	if printjson == True:
     63 		if "ERROR" not in b.status:
     64 			print(g.json)
     65 	else:
     66 		if dogeouri == True:
     67 			if "lat" in g.json and "lng" in g.json:
     68 				print("geo:%s,%s" % (g.json["lat"], g.json["lng"]))
     69 				retval = 0
     70 		else:
     71 			for k in ("lat", "lng", "address"):
     72 				if k in g.json:
     73 					if type(g.json[k]) == bytes:
     74 						s = g.json[k].decode("utf-8")
     75 					else:
     76 						s = g.json[k]
     77 					print("%s: %s" % (k, s))
     78 					retval = 0
     79 
     80 	return retval
     81 
     82 if __name__ == "__main__":
     83 	sys.exit(main(sys.argv))
     84