these-3-words/app.py

105 lines
2.8 KiB
Python
Raw Normal View History

2014-11-17 10:44:55 +00:00
#!/usr/bin/env python
2014-11-17 14:44:39 +00:00
import bottle
2014-11-17 10:44:55 +00:00
from bottle import (
get,
run,
static_file,
2014-11-17 10:44:55 +00:00
template
)
2024-06-28 13:31:01 +00:00
import thesethreewords as these
2024-06-28 18:52:12 +00:00
import sys
from argparse import ArgumentParser
2014-11-17 10:44:55 +00:00
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()
2014-11-17 10:44:55 +00:00
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/<filename:path>')
def serve_static(filename):
return static_file(filename, root='static')
2014-11-17 10:44:55 +00:00
@get('/')
def index():
return template('index', err=None, **example_locs)
2014-11-17 10:44:55 +00:00
2014-11-17 14:44:39 +00:00
2014-11-17 10:44:55 +00:00
@get('/<threewords>')
def showMap(threewords):
try:
lat, lng = these.decode(threewords)
2014-11-17 15:59:10 +00:00
return template('map', lat=lat, lng=lng, threewords=threewords)
2014-11-17 10:44:55 +00:00
except:
return template('index',
err="Could not find location {}".format(threewords),
**example_locs)
2014-11-17 10:44:55 +00:00
2014-11-18 14:40:05 +00:00
@get('/latlng/<lat:float>,<lng:float>')
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)
2014-11-17 10:44:55 +00:00
# API
@get('/api/<lat:float>,<lng:float>')
def latLngToHash(lat, lng):
try:
2024-06-28 18:54:00 +00:00
three = these.three_words((lat,lng,))
six = these.six_words((lat,lng,))
return {'three': three, 'six': six}
2024-06-28 18:45:49 +00:00
except Exception as error:
2024-06-28 18:52:12 +00:00
print(error, file=sys.stderr)
return {}
@get('/api/three/<lat:float>,<lng:float>')
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/<lat:float>,<lng:float>')
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/<threewords>')
def hashToLatLng(threewords):
try:
lat,lng = these.decode(threewords)
return {"lat": lat, "lng": lng}
except:
return {}
2014-11-17 10:44:55 +00:00
if __name__ == '__main__':
run(host=args.host, port=args.port)
app = bottle.default_app()