-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotos_pub.py
48 lines (41 loc) · 2.07 KB
/
photos_pub.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
47
48
from flask import Flask, jsonify
from flask_cors import CORS
from mongoengine import connect
from models import HighSchool
import urllib.request
import json
app = Flask(__name__)
CORS(app)
connect(host='mongodb://admin:[email protected]:29450/sharkweb')
@app.route("/photos/<id>", methods=['GET'])
def get_photos(id):
response = []
connect('high_school')
if not HighSchool.objects(id=id):
response = "invalid id in request"
return jsonify(response), 400
else:
for school in HighSchool.objects(id=id):
name = school.name.replace(' ', '%20')
url_textsearch = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + \
name + '&key=AIzaSyBqleXsttoPMyDVWDMQgcYwutB7ENx4icQ'
with urllib.request.urlopen(url_textsearch) as textsearch_json:
textsearch_data = json.load(textsearch_json)
if textsearch_data["status"] != "OK":
response = []
return jsonify(response), 200
place_id = textsearch_data["results"][0]["place_id"]
url_details = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' + \
place_id + '&key=AIzaSyBqleXsttoPMyDVWDMQgcYwutB7ENx4icQ'
with urllib.request.urlopen(url_details) as details_json:
details_data = json.load(details_json)
if 'photos' in details_data["result"]:
for photo in details_data["result"]["photos"]:
photo_reference = photo["photo_reference"]
url_photo = 'https://maps.googleapis.com/maps/api/place/photo?photoreference=' + \
photo_reference + '&sensor=false&maxheight=1600&maxwidth=1600' \
'&key=AIzaSyBqleXsttoPMyDVWDMQgcYwutB7ENx4icQ'
response.append(url_photo)
return jsonify(response), 200
if __name__ == '__main__':
app.run(port=5002)