-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwholikes.py
306 lines (243 loc) · 8.73 KB
/
wholikes.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from gevent.pool import Pool
import gevent.monkey
gevent.monkey.patch_all()
import os
from pprint import pprint
import sys
import calendar
import time
import operator
import ujson as json
import requests
#requests.packages.urllib3.disable_warnings()
BASE_ENDPOINT = 'https://api.instagram.com/v1'
INSTAGRAM_CLIENT_ID = os.getenv('INSTAGRAM_CLIENT_ID_BKUP')
INSTAGRAM_CLIENT_SECRET = os.getenv('INSTAGRAM_CLIENT_SECRET_BKUP')
def get_base_args():
return {
'client_id': INSTAGRAM_CLIENT_ID,
'client_secret': INSTAGRAM_CLIENT_SECRET
}
def get_response(endpoint, params={}):
if params != {}:
r = requests.get(endpoint, params=params)
else:
r = requests.get(endpoint)
tries = 0
max_tries = 5
while tries < max_tries:
try:
if r.status_code == 400:
return json.loads(r.content)
if r.status_code == 404:
return HI
r.raise_for_status()
break
except:
print r
print endpoint
pprint(params)
print 'Received bad status_code. Sleeping \
for 5 seconds.'
time.sleep(5)
if params:
r = requests.get(endpoint, params=params)
else:
r = requests.get(endpoint)
tries += 1
return json.loads(r.content)
def get_next_page_data(response):
try:
return response['pagination']
except:
print 'No pagination data!'
return None
def get_user_id(username, access_token):
endpoint = BASE_ENDPOINT + '/users/search'
args = {'access_token': access_token}
args['q'] = username
data = get_response(endpoint, args)
data = data['data']
if len(data) > 0:
return data[0]['id']
def get_user_data(user_id, access_token):
endpoint = BASE_ENDPOINT + '/users/' + str(user_id)
args = {'access_token': access_token}
resp = get_response(endpoint, args)
if resp and resp.get('data', None):
return resp['data']
else:
return {}
def get_username(user_id, access_token):
data = get_user_data(user_id, access_token)
if data == {}:
return user_id
return data['username']
def get_num_follows(user_id, access_token):
data = get_user_data(user_id, access_token)
try:
return int(data['counts']['follows'])
except Exception as e:
print e
pprint(data)
return 0
def get_num_followers(user_id, access_token):
data = get_user_data(user_id, access_token)
pprint(data)
try:
return int(data['counts']['followed_by'])
except Exception as e:
print e
pprint(data)
return 0
def read_ids_from_file(fpath):
try:
with open(fpath) as f:
return json.loads(f.read())
except Exception as e:
print e
return None
def write_ids_to_file(fpath, ids):
if not os.path.exists(fpath):
os.system('touch {}'.format(fpath))
with open(fpath, 'w') as f:
f.write(json.dumps(ids))
#Returns a list of user_ids that a user follows.
def get_user_ids_followed(user_id, num_follows, access_token):
ids_from_file = read_ids_from_file('follows/' + str(user_id))
if ids_from_file != None:
return ids_from_file
endpoint = BASE_ENDPOINT + '/users/' + str(user_id) + '/follows'
args = {'access_token': access_token}
resp = get_response(endpoint, args)
next_page = get_next_page_data(resp)
user_ids = []
for item in resp.get('data', []):
user_ids.append(item['id'])
while (next_page and next_page.get('next_url')):
resp = get_response(next_page['next_url'], None)
next_page = get_next_page_data(resp)
for item in resp.get('data', []):
user_ids.append(item['id'])
write_ids_to_file('follows/' + str(user_id), list(set(user_ids)))
return list(set(user_ids))
#Returns a list of user_ids that follow a user.
def get_user_ids_followers(user_id, num_followers, access_token):
ids_from_file = read_ids_from_file('followed-by/' + str(user_id))
if ids_from_file != None:
return ids_from_file
endpoint = BASE_ENDPOINT + '/users/' + str(user_id) + '/followed-by'
args = {'access_token': access_token}
resp = get_response(endpoint, args)
next_page = get_next_page_data(resp)
user_ids = []
for item in resp.get('data', []):
user_ids.append(item['id'])
while (next_page and next_page.get('next_url')):
resp = get_response(next_page['next_url'], None)
next_page = get_next_page_data(resp)
for item in resp.get('data', []):
user_ids.append(item['id'])
write_ids_to_file('followed-by/' + str(user_id), list(set(user_ids)))
return list(set(user_ids))
#Returns JSON list of a user's most recent posts.
def get_latest_media_ids(user_id, num_images, access_token):
endpoint = BASE_ENDPOINT + '/users/' + str(user_id) + '/media/recent'
args = {'access_token': access_token}
args['count'] = num_images
resp = get_response(endpoint, args)
if resp == []:
return []
return [media['id'] for media in resp['data']]
#Get a list of users that liked a post.
def get_user_ids_that_like(item):
media_id, access_token = item
if media_id != -1:
endpoint = BASE_ENDPOINT + '/media/' + str(media_id) + '/likes'
args = {'access_token': access_token}
resp = get_response(endpoint, args)
if resp and resp.get('data', None):
return [user['id'] for user in resp['data']]
return [None]
#Returns a sorted list of users that provide the most likes.
def sort_likes(like_dict):
return sorted(like_dict.items(), key=operator.itemgetter(1), reverse=True)
def yield_latest_media_ids(user_id, num_images, access_token):
endpoint = BASE_ENDPOINT + '/users/' + str(user_id) + '/media/recent'
args = {'access_token': access_token}
args['count'] = num_images
resp = get_response(endpoint, args)
if resp.get('data', None) == None:
yield (-1, access_token)
else:
for media in resp['data']:
yield (media['id'], access_token)
def who_do_i_like(access_token):
like_dict = {}
endpoint = BASE_ENDPOINT + '/users/self/media/liked'
resp = get_response(endpoint, {'access_token': access_token})
next_url = resp['pagination']['next_url']
user_ids = []
for i in range(0, 3):
resp = get_response(next_url)
next_url = resp['pagination']['next_url']
user_ids += [u['user']['username'] for u in resp['data']]
return set(user_ids)
def who_does_user_like(username, access_token):
like_dict = {}
target_user_id = get_user_id(username, access_token)
num_follows = get_num_follows(target_user_id, access_token)
user_ids_followed = get_user_ids_followed(target_user_id, num_follows,
access_token)
fetch_pool = Pool(100)
for user_id in user_ids_followed:
for user_ids in fetch_pool.imap_unordered(
get_user_ids_that_like, yield_latest_media_ids(user_id, 10,
access_token)):
if user_ids and target_user_id in user_ids:
try:
like_dict[user_id] += 1
except:
like_dict[user_id] = 1
top_ten = sort_likes(like_dict)
if len(top_ten) > 10:
top_ten = sort_likes(like_dict)
user_names = []
for item in top_ten:
user_id, like_count = item
user_names.append(get_username(user_id, access_token))
return user_names
def who_likes_user(username, access_token):
like_dict = {}
target_user_id = get_user_id(username, access_token)
fetch_pool = Pool(100)
for user_ids in fetch_pool.imap_unordered(
get_user_ids_that_like, yield_latest_media_ids(target_user_id, 30,
access_token)):
for user_id in user_ids:
try:
like_dict[user_id] += 1
except:
like_dict[user_id] = 1
top_ten = sort_likes(like_dict)
if len(top_ten) > 10:
top_ten = sort_likes(like_dict)[:10]
user_ids = []
user_names = []
for item in top_ten:
user_id, like_count = item
user_ids.append(user_id)
user_names.append(get_username(user_id, access_token))
return user_names
if __name__ == "__main__":
usage = 'usage: python wholikes.py followers|followed user_name'
if len(sys.argv) == 3:
if sys.argv[1] in ['followers', 'followed']:
if sys.argv[1] == 'followers':
who_likes_user(sys.argv[2])
else:
who_does_user_like(sys.argv[2])
else:
print usage
else:
print usage