-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path02_Adding_Points_Python2.py
52 lines (46 loc) · 1.44 KB
/
02_Adding_Points_Python2.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
print 'Importing libraries...'
from urllib2 import urlopen, HTTPError
from json import load
import csv
import time
# Setting up global variables
baseURL = 'https://api.foursquare.com/v2/venues/search?'
lat = 40.7
lon = -74
token = 'your token here'
version = 20160404
limit = 50
# Open base file
with open('Base_Points.csv', 'rb') as basePoints:
reader = csv.reader(basePoints, delimiter = ',')
baseList = list(reader)
# for point in baseList:
# print point
# Building the request
for point in baseList[1:10]:
longitude = point[0]
latitude = point[1]
request = baseURL + 'll=' + str(latitude) + ',' + str(longitude) + '&oauth_token=' + token + '&v=' + str(version) + '&limit=' + str(limit)
# Querying API
try:
response = urlopen(request)
baseData = load(response)
# Parsing the API
response = baseData['response']
venues = response['venues']
for venue in venues:
venueName = venue['name'].encode('utf-8')
venueLat = venue['location']['lat']
venueLon = venue['location']['lng']
venueCategories = venue['categories']
if len(venueCategories) > 0:
venueCat = venueCategories[0]['name'].encode('utf-8')
else:
venueCat = 'None'
venueStats = venue['stats']
venueCheckins = venueStats['checkinsCount']
print venueName + ', ' + str(venueLat) + ', ' + str(venueLon) + ', ' + venueCat + ', ' + str(venueCheckins)
except HTTPError:
print 'There is a problem with the request...'
time.sleep(1)
print 'Done with everything...'