In [136]:
import requests
import pandas as pd
import json
import numpy as np
import random

In [137]:
lat = '51.511732'
lon = '-0.123270'

In [138]:
query = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='+lat+','+lon+'&radius=300&type=cafe&key=AIzaSyBgo0C-bUIQ1276K8gt-DaikKwoYd_Ay_M'
request = requests.get(query).json()

In [139]:
queries = []
for i in range(len(request["results"])):
    placeid=request["results"][i]["place_id"]
    queries.append('https://maps.googleapis.com/maps/api/place/details/json?placeid='+placeid+'&key=AIzaSyBgo0C-bUIQ1276K8gt-DaikKwoYd_Ay_M')

In [140]:
r = []
for i in range(len(queries)):
    r.append(requests.get(queries[i]).json())

In [141]:
queries2 =[]
for i in range(len(request["results"])):
    latitude = str(r[i]["result"]["geometry"]["location"]["lat"])
    longitude = str(r[i]["result"]["geometry"]["location"]["lng"])
    queries2.append('https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.511732,-0.123270&destinations='+latitude+','+longitude+'&mode=walking&key=AIzaSyBgo0C-bUIQ1276K8gt-DaikKwoYd_Ay_M')

In [142]:
r2 = []
for i in range(len(queries2)):
    r2.append(requests.get(queries2[i]).json())

In [143]:
distances=[]
names1=[]
for i in range(len(r2)):
    distances.append(str(r2[i]["rows"][0]["elements"][0]["duration"]["text"])[0])
    names1.append(r[i]["result"]["name"])
    r[i]["result"]["distance"]=distances[i]
    if ('price_level' in request["results"][i]):
        r[i]["result"]["price"]=request["results"][i]["price_level"]
    else:
        r[i]["result"]["price"]=random.randint(1,4)

In [144]:
import oauth2
class Oauth1Authenticator(object):

    def __init__(
        self,
        consumer_key,
        consumer_secret,
        token,
        token_secret
    ):
        self.consumer = oauth2.Consumer(consumer_key, consumer_secret)
        self.token = oauth2.Token(token, token_secret)

    def sign_request(self, url, url_params={}):
        oauth_request = oauth2.Request(
            method="GET",
            url=url,
            parameters=url_params
        )
        oauth_request.update(
            {
                'oauth_nonce': oauth2.generate_nonce(),
                'oauth_timestamp': oauth2.generate_timestamp(),
                'oauth_token': self.token.key,
                'oauth_consumer_key': self.consumer.key
            }
        )
        oauth_request.sign_request(
            oauth2.SignatureMethod_HMAC_SHA1(),
            self.consumer,
            self.token
        )
        return oauth_request.to_url()

In [145]:
from yelp.client import Client
from yelp.oauth1_authenticator import Oauth1Authenticator

auth = Oauth1Authenticator(
    consumer_key="fW0PMrTIaH_gQ1RbgZVk-g",
    consumer_secret="uPtL5lPYvkbg2hL_DXugUen6mbU",
    token="OYLNFrR_Kw4F43Qau34IyQdCTmCmvwlb",
    token_secret="5nN_zzdNxuOgC4Nu_3eVXVrCeL8"
)

client = Client(auth)

In [146]:
params = {
    'term':'food',
    'radius_filter':'300',  
}

response = client.search_by_coordinates(51.511732,-0.123270, **params)

In [147]:
names2=[]
latitudes=[]
longitudes=[]
ratings=[]
phone=[]
address=[]
for i in range(len(response.businesses)):
    names2.append(response.businesses[i].name)
    latitudes.append(response.businesses[i].location.coordinate.latitude)
    longitudes.append(response.businesses[i].location.coordinate.longitude)
    ratings.append(response.businesses[i].rating)
    phone.append(response.businesses[i].display_phone)
    address.append(response.businesses[i].location.address)

In [148]:
distances2=[]
queries3 =[]
r3=[]
for i in range(len(names2)):
    latitude = str(latitudes[i])
    longitude = str(longitudes[i])
    queries3.append('https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.511732,-0.123270&destinations='+latitude+','+longitude+'&mode=walking&key=AIzaSyBgo0C-bUIQ1276K8gt-DaikKwoYd_Ay_M')
    r3.append(requests.get(queries2[i]).json())
    distances2.append(str(r3[i]["rows"][0]["elements"][0]["duration"]["text"])[0])

In [149]:
for i in range(len(names1)):
    if ('rating' not in request["results"][i]):
        if (names2[i] in names1):
            r[i]["result"]["rating"]=ratings[i]
        else:
            r[i]["result"]["rating"]=random.randint(1,5)

In [ ]:
geojson = []

for id, place in table.iterrows():
    feature = {
        "type":"Feature",
        "geometry":{
            "type": "Point",
            "coordinates": [float(place["geometry.location.lng"]), float(place["geometry.location.lat"])]
        },
        "properties":{
            "price_level":place.price_level,
            
        }
    }
    geojson.append(feature)
import json
with open("table.geojson","w") as out_file:
    out_file.write(json.dumps(geojson))

In [ ]: