-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyelp_api.py
executable file
·75 lines (56 loc) · 1.9 KB
/
yelp_api.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/env python2.7
import os
import rauth
import time
import pprint
import imp
import json
import shapefile
# Parameters for Yelp api
def yelp_parameters(bbox,category,offset):
params = {}
params["term"] = category
params["bounds"] = "{},{}|{},{}".format(str(bbox[1]),str(bbox[0]),str(bbox[3]),str(bbox[2]))
params["limit"] = "20"
params["offset"] = offset
return params
# API response from paramaters and key
def yelp_results(params):
details = open ('config.json')
cred = json.load(details)
# Yelp's access details
session = rauth.OAuth1Session(
consumer_key = cred['consumer_key']
,consumer_secret = cred['consumer_secret']
,access_token = cred['token']
,access_token_secret = cred['token_secret'])
request = session.get("http://api.yelp.com/v2/search",params=params)
data = request.json()
session.close()
return data
def apiCount(shape,categories):
categoryCounts = []
for i in range(len(categories)):
responseCount = 0
offset = 0
while (responseCount == offset and offset <1000):
params = yelp_parameters(shape.bbox,categories[i],offset)
response = len(yelp_results(params)['businesses'])
responseCount += response
offset += 20
categoryCounts += [responseCount]
return categoryCounts
def getData(shapes,refs):
categories = ["active","art","beautysvc","education",
"food","health","localservices","nightlife",
"religiousorgs","restaurants","shopping"]
results = []
for i in range(len(shapes)):
counts = apiCount(shapes[i],categories)
results.append([refs[i][1]]+counts)
print results
return results
scc_shapefile = "data/shapefile/Manhattan-SubwayComplaintCrime"
data = shapefile.Reader(scc_shapefile)
result = getData(data.shapes(),data.records())
print result