import pandas as pd
import requests
# intialize a function to get all the venue in the neighbourhood
def getNearbyVenues(names, latitudes, longitudes, radius=1000):
    
    venues_list=[]
    for name, lat, lng in zip(names, latitudes, longitudes):
                    
        # create the API request URL
        url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
            CLIENT_ID, 
            CLIENT_SECRET, 
            VERSION, 
            lat, 
            lng, 
            radius, 
            LIMIT)
        
        print(url)
            
        # make the GET request
        response = requests.get(url).json()["response"]
        results = response['groups'][0]['items']
        
        # return only relevant information for each nearby venue
        venues_list.append([(
            name, 
            lat, 
            lng, 
            v['venue']['name'], 
            v['venue']['location']['lat'], 
            v['venue']['location']['lng'],  
            v['venue']['categories'][0]['name']) for v in results])
    nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
    nearby_venues.columns = ['Neighborhood', 
                  'Neighborhood Latitude', 
                  'Neighborhood Longitude', 
                  'Venue', 
                  'Venue Latitude', 
                  'Venue Longitude', 
                  'Venue Category']
    
    return(nearby_venues)
# @hidden
# Client Key : Client Secret
FOURSQUARE_KEYS = [['0ANT5D4J32NF4ZRXBNJGOUE1GHSYM01E34BALDNLVJVAMZIG', 'SXIAJWKCXVAZ32UJYYSD4DOTHIOJRUPHXIUJFEBSJHRMJ454']
                  ,['JFIPVNQGSM3DJVNB4KPAFDWCS2AN5MLT0CUXMBDEEZ3TFTN1', 'O5MIDR0SDHPOMB4VRJRVJY2TV0YHYIYRKUUANCQKKN3UYOF4']
                  ,['ONXQAZDGDXM0J2NWDIO15QUNYVAYXIP2GEAKGFDMOY01BPZ5', '15QX0C5ZDA2WTU0QRKARHH4SUTMRMAJFISIZAWSPOE4I0ITX']
                  ,'0XBHK2D0ZYU2SGO2GFJHEEPBLBOI2N5HY33TFTHK5VYM1VPP', 'WOWHI5MO1M2FH2V3JTAHSAZRH4TE4YKA3HDOT5BUNHXFHL4U'
                  ,['KS1TFYVJUHPCPMZOCWEYNLSAJNU5JH5WXHXW3PXPGCI2GLGI', 'AOW13VCPF2STWG0EV12QCIXZL0FPPRFZOAAGPHYRHZU0VL0U']]
# list of all the cities in which we are interested in
cities = ['delhi'
          ,'mumbai'
          ,'kolkata'
          ,'chennai']
for city, key in zip(cities,FOURSQUARE_KEYS[:len(cities)]):
    
    # initializing foursquare API credentials
    CLIENT_ID = key[0] # your Foursquare ID
    CLIENT_SECRET = key[1] # your Foursquare Secret
    VERSION = '20180605' # Foursquare API version
    LIMIT = 100
    
    df = pd.read_csv(city + '_subdiv.csv', index_col = 0)
    data = df.copy()
    venues = getNearbyVenues(names=data['Neighborhood'],
                                   latitudes=data['Latitude'],
                                   longitudes=data['Longitude']
                                  )
    venues.to_csv(city + '_venues.csv')
    print(city,'completed!')
# Delhi
df = pd.read_csv('delhi_venues.csv', index_col = 0)
df.head()
df.info()
# Mumbai
df = pd.read_csv('mumbai_venues.csv', index_col = 0)
df.head()
df.info()
# Kolkata
df = pd.read_csv('kolkata_venues.csv', index_col = 0)
df.head()
df.info()
# Chennai
df = pd.read_csv('chennai_venues.csv', index_col = 0)
df.head()
df.info()