-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_app.py
46 lines (32 loc) · 1.42 KB
/
google_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import imghdr
from flask import Flask, render_template, jsonify
import requests
from google_api_key import key
app = Flask(__name__)
search_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json"
details_url = "https://maps.googleapis.com/maps/api/place/details/json"
photos_url = "https://maps.googleapis.com/maps/api/place/photo"
@app.route("/", methods=["GET"])
def retreive():
return render_template('layout.html')
@app.route("/sendRequest/<string:query>")
def results(query):
search_payload = {"key": key, 'input': query, 'inputtype': 'textquery'}
search_req = requests.get(search_url, params=search_payload)
search_json = search_req.json()
place_id = search_json['candidates'][0]['place_id']
# details_payload = {"key": key, "placeid": place_id}
# details_resp = requests.get(details_url, params=details_payload)
# details_json = details_resp.json()
#
# url = details_json["result"]["url"]
# return jsonify({'result': url})
photo_payload = {"key": key, "maxwidth": 500, "maxheight ": 500, "photoreference": place_id}
photo_request = requests.get(photos_url, params=photo_payload)
photo_type = imghdr.what("", photo_request.content)
photo_name = "static/" + query + "." + photo_type
with open(photo_name, "wb") as photo:
photo.write(photo_request.content)
return '<img src=' + photo_name + '>'
if __name__ == "__main__":
app.run(debug=True)