Skip to content

Commit

Permalink
Ditched restrictive gmaps api. Now using OpenStreetMap #64 #135
Browse files Browse the repository at this point in the history
  • Loading branch information
jotyGill committed Apr 16, 2018
1 parent 3955e45 commit 162ba18
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions openpyn/locations.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,87 @@
import random
import sys
import time

import requests


user_agents = ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0']


# takes server list outputs locations (each only once) the servers are in.
def get_unique_locations(list_of_servers):
unique_locations = []
resolved_locations = []
locations_count = 0

for aServer in list_of_servers:
latLongDic = {"lat": aServer["location"]["lat"], "long": aServer["location"]["long"]}
if latLongDic not in unique_locations:
unique_locations.append(latLongDic)
# print(unique_locations)
for eachLocation in unique_locations:
geo_address_list = get_location_name(eachLocation)
user_agent = {'User-Agent': user_agents[locations_count % 6],
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}
geo_address_list = get_locations(eachLocation, user_agent)
time.sleep(random.randrange(1, 5, 1) * 0.1)
# geo_address_list = get_location_name(latitude=latitude, longitude=longitude)
resolved_locations.append(geo_address_list)
# print(resolved_locations)
locations_count += 1
print("resolved_locations", resolved_locations)
return resolved_locations


def get_locations(location_dic, req_headers):
latitude = location_dic["lat"]
longitude = location_dic["long"]

url = 'https://nominatim.openstreetmap.org/reverse?format=jsonv2'
params = "&lat={lat}&lon={lon}".format(
lat=latitude,
lon=longitude
)
final_url = url + params
print("req_headers", req_headers)
r = requests.get(final_url, headers=req_headers)
geo_address_list = []
name_list = []
try:
response = r.json()
results = response['address']
# print(results)
except IndexError:
print("IndexError: Looks like you have reached Google maps API's daily \
request limit. No location data for you :( you could restart your router to get a new IP.")
sys.exit()

geo_address_list.append(location_dic)

for key in results:
# print(results["city"])
if key == "country_code":
geo_address_list.insert(0, results["country"])
if key == "village":
name_list.append(results["village"])
if key == "city":
name_list.append(results["city"])
if key == "suburb":
name_list.append(results["suburb"])
if key == "region":
name_list.append(results["region"])
if key == "state":
name_list.append(results["state"])
if key == "state_district":
name_list.append(results["state_district"])
geo_address_list.insert(2, name_list)
print(geo_address_list)
return geo_address_list


def get_location_name(location_dic):
latitude = location_dic["lat"]
longitude = location_dic["long"]
Expand Down

0 comments on commit 162ba18

Please sign in to comment.