#!/usr/bin/env python import bottle from bottle import ( get, run, static_file, template ) import thesethreewords as these import sys from argparse import ArgumentParser parser = ArgumentParser( prog='these-3-words-api', description='Hosts an API for these-3-words API', epilog='Defaults to: 0.0.0.0:8080') parser.add_argument('--host', default='0.0.0.0', help='Host to bind to (default: 0.0.0.0)') parser.add_argument('--port', type=int, default=8080, help='Port to bind to (default: 8080)') args = parser.parse_args() example_locs = [("sydney", (-33.867480754852295, 151.20700120925903)), ("battery", (40.70329427719116, -74.0170168876648)), ("san_fran", (37.790114879608154, -122.4202036857605))] example_locs = dict((name,these.three_words(pos)) for name,pos in example_locs) @get('/static/') def serve_static(filename): return static_file(filename, root='static') @get('/') def index(): return template('index', err=None, **example_locs) @get('/') def showMap(threewords): try: lat, lng = these.decode(threewords) return template('map', lat=lat, lng=lng, threewords=threewords) except: return template('index', err="Could not find location {}".format(threewords), **example_locs) @get('/latlng/,') def showMapFromLatLng(lat, lng): try: threewords = these.three_words((lat, lng)) return template('map', lat=lat, lng=lng, threewords=threewords) except: return template('index', err="Could not find location {}".format(threewords), **example_locs) # API @get('/api/,') def latLngToHash(lat, lng): try: three = these.three_words((lat,lng,)) six = these.six_words((lat,lng,)) return {'three': three, 'six': six} except Exception as error: print(error, file=sys.stderr) return {} @get('/api/three/,') def latLngToThreeHash(lat, lng): try: three = these.three_words((lat,lng,)) return {'three': three} except Exception as error: print(error, file=sys.stderr) return {} @get('/api/six/,') def latLngToSixHash(lat, lng): try: six = these.six_words((lat,lng,)) return {'six': six} except Exception as error: print(error, file=sys.stderr) return {} @get('/api/') def hashToLatLng(threewords): try: lat,lng = these.decode(threewords) return {"lat": lat, "lng": lng} except: return {} if __name__ == '__main__': run(host=args.host, port=args.port) app = bottle.default_app()