-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
114 lines (97 loc) · 3.81 KB
/
api.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#Address
#Picture Url
from flask import Flask
from flask_restful import Resource, Api, request
import json
import os
import types
import requests
import sys
app = Flask(__name__)
api = Api(app)
def api_route(self, *args, **kwargs):
def wrapper(cls):
self.add_resource(cls, *args, **kwargs)
return cls
return wrapper
api.route = types.MethodType(api_route, api)
@api.route('/matches')
class Server(Resource):
def get(self):
if request.args.get('number'):
number = int(request.args.get('number'))
else:
number = 5
output_dict = generate_matches(number)
return output_dict
#print(data,file=sys.stderr)
#api.route('/homes')
class homes(Resource):
def get(self):
iter =0
count = 0
number = request.args.get('number')
zipCode = request.args.get('zipcode')
#print(zipCode)
#print(number)
output_dict = {}
with open('bigdump2.json') as data_file:
data = json.load(data_file)
while count <= int(number):
#print("Iter: "+str(iter))
#print(data[iter][0]["address"]["postal_code"]["value"])
if data[iter][0]["address"]["postal_code"]["value"] == zipCode:
output_dict[count] = data[iter][0]
output_dict[count]["food_score"] = getYelpRestaurants(data[iter][0]["address"]["postal_code"]["value"])
count+=1
iter+=1
return output_dict
#print(data,file=sys.stderr)
api.add_resource(homes, '/homes')
def generate_matches(number):
output_dict = {}
with open('bigdump2.json') as data_file:
data = json.load(data_file)
# for iter in range(0,int(number)):
# print(data[0])
for iter in range(0, number):
output_dict[iter] = getMatchFields(data[iter][0])
return output_dict
def getMatchFields(listing):
"""
:param listing: Accepts the a listing and returns a list with only the fields needed to display a match
:return:
"""
#print(listing)
clean_record = {}
clean_record["street_address"] = listing["address"]["street_address"]["label"]
clean_record["postal_code"] = listing["address"]["postal_code"]["value"]
clean_record["city"] = listing["address"]["locality"]["label"]
clean_record["state"] = listing["address"]["region"]["label"]
clean_record["main_uri"] = "http://homes.com" + listing["main_uri"]
clean_record["price"] = listing["price"]["value"]
clean_record["primary_image "] = listing["primary_image"]["src"]
clean_record["crime_url"] = getCrimeUrl(listing["address"]["postal_code"]["value"])
clean_record["food_score"] = getYelpRestaurants(listing["address"]["postal_code"]["value"])
clean_record["restaurant_search_url"] = "https://www.yelp.com/search?find_desc=Restaurants&find_loc="+listing["address"]["postal_code"]["value"]
return clean_record
#api.add_resource(Server, '/')
def getCrimeUrl(zipcode):
return ("http://www.mylocalcrime.com/#"+zipcode)
def getYelpRestaurants(zipcode):
url = "https://api.yelp.com/v3/businesses/search"
querystring = {"term": "restaurants", "location": zipcode, "sort_by": "rating"}
headers = {
'authorization': "Bearer kqvOVDOQeslDhIResy4ITpm1koTAGKnzQTOgQE80hYk-cIxKc9_g4sZSAY4e5VhUnVZlg9UZU8p7nTw3fKNpsW49cucrvvki-M8VEg1Uz_J-Fjg-bda49n8HOKumWHYx",
'cache-control': "no-cache",
'postman-token': "08286f44-b75a-b265-e966-40e016de51bd"
}
response = requests.request("GET", url, headers=headers, params=querystring).json()
#print(response)
sum=0
for business in response["businesses"]:
sum += business['rating']
return (sum/len(response["businesses"]))
if __name__ == '__main__':
app.run(debug=True, host = os.getenv("IP","0.0.0.0"),port = int (os.getenv('PORT', 33507)))
#app.run()