-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (53 loc) · 2.49 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import urllib2
import json
from flask import Flask, render_template, redirect, url_for, request, flash
from shared.forms import SearchForm
from geopy.geocoders import Nominatim
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
geolocator = Nominatim()
def convert_miles_to_meters(miles):
conversion = int(miles)
conversion = conversion * 1609.33999997549
return conversion
@app.route('/', methods=['GET','POST'])
def route_home():
form = SearchForm()
if form.validate_on_submit():
return redirect(url_for('route_results'), code=307)
return render_template('search.html', title='Search Here', form=form)
@app.route('/results', methods=['GET', 'POST'])
def route_results():
keyword = request.form['keyword']
zipcode = str(request.form['zipcode'])
radius = request.form['radius']
location = geolocator.geocode(zipcode)
lat = str(location.latitude)
lon = str(location.longitude)
# Find lat / long
data = '{"api_key" : "c9caffc16e51883cfec43b1febf65cb898926037", "fields" : [ "name", "description", "website_url", "open_hours", "categories", "locu_id", "location", "contact" ], "venue_queries" : [ { "categories" : "' + keyword + '", "locu": { "is_publishable": true }, "location" : { "geo" : { "$in_lat_lng_radius" : [' + str(location.latitude) + ', ' + str(location.longitude) +', ' + str(convert_miles_to_meters(radius)) + '] } } } ] }'
url = 'https://api.locu.com/v2/venue/search'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
for x in f:
data = x
f.close()
parsed_json = json.loads(data)
return render_template('results.html', json=parsed_json)
@app.route('/profile', methods=['GET', 'POST'])
def route_profile():
data = '{"api_key": "c9caffc16e51883cfec43b1febf65cb898926037", "fields" : [ "name", "description", "website_url", "open_hours", "categories", "locu_id", "location", "contact" ], "venue_queries": [{"locu_id": "' + request.args.get('id') + '","locu": {"is_publishable": true}}]}'
url = 'https://api.locu.com/v2/venue/search'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
for x in f:
data = x
f.close()
parsed_json = json.loads(data)
slim_json = parsed_json['venues'][0]
address = slim_json["location"]["address1"]
return render_template('profile.html', json=slim_json)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')