-
Notifications
You must be signed in to change notification settings - Fork 3
/
Locator.py
185 lines (154 loc) · 5.38 KB
/
Locator.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import requests
import json
import time
from pyzillow.pyzillow import ZillowWrapper, GetDeepSearchResults
from geopy.geocoders import GoogleV3
import pandas as pd
import re
# Put your api keys here
zillowKey = input("Enter your zillow API key")
googleKey = input("Enter your google API key")
class GooglePlaces(object):
"""
A class to simplify API queries to the Google Maps
API.
"""
def __init__(self):
self.loc_x = None
self.loc_y = None
self.location = None
def get_address(self, loc_x, loc_y):
"""
Takes in the given lattitude and longitude (loc_x and
loc_y respectively) and returns a formatted address list.
loc_x: float
The latitude
loc_y: float
The longitude
"""
geolocator = GoogleV3(api_key=googleKey)
location = str(loc_x)+","+str(loc_y)
locations = geolocator.reverse(location)
address = locations[0].address.split(",")
print("Address: ")
print(address)
print()
return address
def get_coordinates(self, bssid):
"""
Alternative method to get coordinates from BSSID
that uses the Google Maps API instead of WiGLE.net.
bssid: str
The BSSID
"""
endpoint_url = "https://www.googleapis.com/geolocation/v1/geolocate" + "?key=" + googleKey
formatted = "".join([bssid[i:i+2] + ":" for i in range(0, len(bssid), 2)])[:-1]
print(formatted)
headers = {"Content-Type": "application/json"}
params = {
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": formatted[:-1] + "d"
},
{
"macAddress": formatted
}
]
}
results = requests.post(endpoint_url, data = json.dumps(params), headers = headers)
results = json.loads(results.content)
print(results)
return results["location"]["lat"], results["location"]["lng"]
def search_places_by_coordinate(self, loc_x,loc_y, radius=100):
"""
Method that returns all points of interest near the given coordinates.
loc_x: float
The latitude
loc_y: float
The longitude
radius: int
The signal radius
"""
location = str(loc_x)+","+str(loc_y)
endpoint_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
places = []
params = {
'location': location,
'radius': radius,
'key': googleKey
}
results = requests.get(endpoint_url, params = params)
results = json.loads(results.content)
places.extend(results['results'])
time.sleep(2)
while "next_page_token" in results:
params['pagetoken'] = results['next_page_token'],
res = requests.get(endpoint_url, params = params)
results = json.loads(res.content)
places.extend(results['results'])
time.sleep(5)
return places
def get_points_of_interest(self,loc_x, loc_y):
"""
Method that returns the top three points of interest from search_places_by_coordinate()
in a pandas dataframe.
loc_x: float
The latitude
loc_y: float
The longitude
"""
places = self.search_places_by_coordinate(loc_x,loc_y)
d = {'types': []}
df = pd.DataFrame(data=d)
setter =3
if(len(places)<3):
setter = len(places)
for z in range(0,setter):
x = places[z]
if "price_level" in x:
f = {'name': [x["name"]],'types': [x["types"]],'price_level': [x["price_level"]]}
else:
f = {'name': [x["name"]],'types': [x["types"]],'price_level': ["null"]}
lf = pd.DataFrame(data=f)
df = df.append(lf, ignore_index=True)
return df
class ZillowAPI(object):
"""
A class to handle the ZillowAPI queries.
"""
def __init__(self, loc_x, loc_y):
"""
Constructor method.
loc_x: float
The latitude
loc_y: float
The longitude
"""
# Used to get the address, could be replaced with GoogleAPI class
geolocator = GoogleV3(api_key=googleKey)
locations = geolocator.reverse(str(loc_x) + ", " + str(loc_y))
# Format address for query to Zillow
f = locations[0].address.split(",")
n = re.sub("[^0-9]", "", f[2])
self.address = f[0] +"," + f[1]
self.zipcode = n
self.zillow_data = ZillowWrapper(zillowKey)
def isHouse(self):
"""
Method that uses a try/catch block to determine if the coordinates (loc_x, loc_y)
passed into the class is a household.
Returns a boolean value.
"""
deep_search_response = self.zillow_data.get_deep_search_results(self.address, self.zipcode)
try:
return True
except:
return False
def getResults(self):
"""
Method that returns a dict() of data about the house at the coordinates of loc_x, loc_y.
"""
deep_search_response = self.zillow_data.get_deep_search_results(self.address, self.zipcode)
result = GetDeepSearchResults(deep_search_response)
return result