foursquare

Get lat/long of Foursquare data


I'm looking for some help with the Foursquare API. I exported my checkin data and it doesn't have addresses or lat/lon data and that's what I'm looking for. I do have the venue_ID or fsq_id and was hoping to use the places API to search by ID and get the lat/lon, but I'm only seeing examples of how to feed the API an address (which I don't have). Any tips would be greatly appreciated.


Solution

  • This might not be the best formatted Python, but it works for what I needed and I wanted to share. I took my data export and grabbed all of the venue IDs and put them in a text file. I think read through the text file to get the lat/lon for each ID and put it in a CSV file.

    import requests
    import json
    import csv
    
    headers = {
        "accept": "application/json",
        "Authorization": "YOUR_API_KEY"
    }
    
    # define path for products adoc file
    path = r'foursquare.csv'
    
    # clear attributes file if exists
    c = open(path,'w')
    c.close()
    csv = open(path, 'a')
    
    with open('ids.txt', 'r') as f:
      for line in f:
        fsq_id = str(line).replace("\n","")
        url = "https://api.foursquare.com/v3/places/"+fsq_id+"?fields=geocodes"
        response = requests.get(url, headers=headers)
        if response.status_code != 404:
            locations = response.json()
            csv.write(fsq_id)
            csv.write(", ")
            csv.write(str(locations['geocodes']['main']['latitude']))
            csv.write(", ")
            csv.write(str(locations['geocodes']['main']['longitude']))
            csv.write("\n")
    
    print("done")