-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoursquare-api.py
executable file
·218 lines (197 loc) · 7.18 KB
/
foursquare-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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
import sys
from database import Database
import foursquare
import argparse
import json
# duplicated from database
import configparser
db = Database()
config = configparser.ConfigParser()
config.read('config.ini')
# Construct the client object
client = foursquare.Foursquare(client_id=config['FOURSQUARE']['CLIENT_ID'],
client_secret=config['FOURSQUARE']['CLIENT_SECRET'],
redirect_uri=config['FOURSQUARE']['CLIENT_CALLBACK'])
def auth():
# Build the authorization url for your app
auth_uri = client.oauth.auth_url()
print(auth_uri);
def auth_redirect(code):
# Interrogate foursquare's servers to get the user's access_token
access_token = client.oauth.get_token(code)
# Apply the returned access token to the client
client.set_access_token(access_token)
# Get the user's data
user = client.users()
#print(user)
sql = "REPLACE INTO users (id, name, auth_token) VALUES (%(id)s, %(name)s, %(auth_token)s);"
# join strings with spaces if not null
name = ' '.join(filter(None, [ user['user'].get('firstName', None), user['user'].get('lastName', None) ]))
fid = user['user']['id']
print(fid + ': ' + name)
cur = db.execute(sql, {'id': fid, 'name': name, 'auth_token': access_token})
db.commit()
def insert_checkin(cx, user_id):
cf = [
'id',
'user_id',
'type',
'timeZoneOffset',
'createdAt',
'private',
'shout',
'venue_id',
'venue_name',
'location',
'event',
'photos',
'like',
'post',
]
sqlCheckin = "INSERT INTO checkins (" + \
','.join('`' + i + '`' for i in cf) + \
") VALUES (" + \
','.join('%(' + i + ')s' for i in cf) + \
");"
cv = [
'id',
'name',
'location_address',
'location_crossStreet',
'location_lat',
'location_lng',
'location_postalCode',
'location_cc',
'location_city',
'location_state',
'location_country',
'location_formattedAddress',
'categories',
'closed',
]
sqlVenue = "REPLACE INTO venues (" + \
','.join('`' + i + '`' for i in cv) + \
") VALUES (" + \
','.join('%(' + i + ')s' for i in cv) + \
");"
# TODO it appears cur.rowcount does not reflect skipped rows from INSERT IGNORE
# without this we cannot tell if we have reached the end of our polling
cur = db.execute("SELECT id FROM checkins WHERE id = %(id)s;", {'id': cx['id']})
if (cur.fetchone()):
return 0
cur = db.execute(sqlCheckin, {
'id': cx['id'],
'user_id': user_id,
'type': cx['type'],
'timeZoneOffset': cx['timeZoneOffset'],
'createdAt': cx['createdAt'],
'private': cx.get('private', 0),
'shout': cx['shout'] if cx.get('shout') else None,
'venue_id': cx['venue']['id'] if cx.get('venue') else None,
'venue_name': cx['venue']['name'] if cx.get('venue') else None,
'location': json.dumps(cx['location']) if cx.get('location') else None,
'event': cx['event']['name'] if cx.get('event') else None,
'photos': json.dumps(cx['photos']['items']) if cx['photos']['count'] > 0 else None,
'like': 1 if cx['like'] else 0,
'post': "\n".join(x['text'] for x in cx['posts']['items']) if cx['posts']['count'] > 0 else None,
})
if cx.get('venue'):
vx = cx['venue'];
lx = vx['location'];
cur2 = db.execute(sqlVenue, {
'id': vx['id'],
'name': vx['name'],
'location_address': lx['address'] if lx.get('address') else None,
'location_crossStreet': lx['crossStreet'] if lx.get('crossStreet') else None,
'location_lat': lx['lat'],
'location_lng': lx['lng'],
'location_postalCode': lx['postalCode'] if lx.get('postalCode') else None,
'location_cc': lx['cc'] if lx.get('cc') else None,
'location_city': lx['city'] if lx.get('city') else None,
'location_state': lx['state'] if lx.get('state') else None,
'location_country': lx['country'] if lx.get('country') else None,
'location_formattedAddress': "\n".join(lx['formattedAddress']) if lx.get('formattedAddress') else None,
'categories': ','.join(x['name'] for x in vx['categories']) if vx.get('categories') else None,
'closed': 1 if vx.get('closed') else 0,
})
return cur.rowcount
def checkins(user_id, *opts):
#print(user_id)
sql = "SELECT auth_token FROM users WHERE id = %(id)s;"
cur = db.execute(sql, {'id': user_id})
row = cur.fetchone()
access_token = row[0]
#print(access_token)
#print(access_token)
#client = foursquare.Foursquare(access_token)
client.set_access_token(access_token)
#sql = "SELECT MAX(createdAt) FROM checkins WHERE user_id = %(user_id)s;"
#cur = db.execute(sql, {'user_id': user_id})
#row = cur.fetchone()
#after = None
#if row[0]:
# after = row[0] # - 360 # subtract 5mins
# https://developer.foursquare.com/docs/api/users/checkins
# afterTimestamp param appears to be ignored!
limit = 100
params={'limit': limit, 'sort': 'newestfirst'}
if len(opts) == 1:
params['beforeTimestamp'] = opts[0] + 60 # 60s buffer
recs = client.users.checkins('self', params) #={'limit': limit, 'sort': 'newestfirst'})
count = 0
for cx in recs['checkins']['items']:
#print(cx)
count += insert_checkin(cx, user_id)
db.commit()
print("Inserted "+str(count)) # todo text format
if count == limit:
# do another round using beforeTimestamp
# TODO: sleep, or prevent deep recursion?
checkins(user_id, cx['createdAt'])
elif count == 0:
sys.exit(2)
def all_checkins(user_id):
sql = "SELECT auth_token FROM users WHERE id = %(id)s;"
cur = db.execute(sql, {'id': user_id})
row = cur.fetchone()
access_token = row[0]
#print(access_token)
#client = foursquare.Foursquare(access_token)
client.set_access_token(access_token)
recs = client.users.all_checkins() # iteratorable
#print(recs)
count = 0
for cx in recs:
#print(cx);
count += insert_checkin(cx, user_id)
db.commit()
print("Inserted "+str(count)) # todo text format
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
parser = argparse.ArgumentParser(description="Download Foursquare Checkins")
parser.add_argument('cmds', metavar="CMD", type=str, nargs='+',
help="""auth, auth_redirect, checkins, all_checkins""")
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
if args.cmds[0] == 'auth':
auth();
elif args.cmds[0] == 'auth_redirect':
if len(args.cmds) < 2:
print("Auth CODE required")
sys.exit(1)
auth_redirect(args.cmds[1]);
elif args.cmds[0] == 'checkins':
if len(args.cmds) < 2:
print("User ID required")
sys.exit(1)
checkins(args.cmds[1])
elif args.cmds[0] == 'all_checkins':
if len(args.cmds) < 2:
print("User ID required")
sys.exit(1)
all_checkins(args.cmds[1])
else:
print("instructions...")