m-wrzr / populartimes

MIT License
824 stars 167 forks source link

Can't seem to accurately pass in P1/P2 params. #63

Closed dillonfromdallas closed 5 years ago

dillonfromdallas commented 5 years ago

Hi!

Currently trying to create a Flask route which will take parameters, perform a populartimes.get() call, and return the JSONified data.

This is the route as-is:

@app.route("/get")
def get():
    types = request.args.get("types")
    p1 = request.args.get("p1")
    p2 = request.args.get("p2")
    n_threads = request.args.get("n_threads", type = int, default = 1)
    radius = request.args.get("radius", type = int, default = 1000)
    all_places = request.args.get("all_places", type = bool, default = True)

    return jsonify(populartimes.get(api_key, types=types, p1=p1, p2=p2, n_threads=n_threads, radius=radius, all_places=all_places))

And the current URL: http://localhost:5000/get?types=bar&p1=(48.132986,%2011.566126)&p2=(48.142199,%2011.580047)

However, this runs into an issue when parsing p1/p2

ValueError: could not convert string to float: '('

How can I make sure I'm properly passing the right values and types into the function, and properly make the call?


requirements.txt

certifi==2018.11.29
chardet==3.0.4
Click==7.0
Flask==1.0.2
geographiclib==1.49
geopy==1.18.1
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
populartimes==2.0
requests==2.21.0
urllib3==1.24.1
Werkzeug==0.14.1
Porter97 commented 5 years ago

The issue is your use of lists for the points parameter. The .get() is trying to evaluate them as floats, but misses on the conversion because they are wrapped in parentheses. If you are accepting the points through flask you can either a) switch to a post form request, and pass through a list through the form for the two points, or b) you can wrap them in brackets (ie, ['xx.xx', 'yy.yy'], for a python list), and convert to a standard list using either json.loads, or ast.literal_eval(). Then you just slice the received list (list[0], list[1]) for your points to put them into the function. Make sure to use some form of error handling in case your program can't read the inputs.