-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path03_Final_Script_Python2.py
59 lines (51 loc) · 1.71 KB
/
03_Final_Script_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
53
54
55
56
57
58
59
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
# Create output file
output = open('FoursquareVenues.csv', 'wb')
output.write('Name\tLatitude\tLongitude\tCategory\tCheckins\n')
# Building the request
for point in baseList[1:]:
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)
output.write(venueName + '\t' + str(venueLat) + '\t' + str(venueLon) + '\t' + venueCat + '\t' + str(venueCheckins) + '\n')
except HTTPError:
print 'There is a problem with the request...'
time.sleep(0.5)
output.close()
print 'Done with everything...'