-
Notifications
You must be signed in to change notification settings - Fork 16
/
instagram_stuff.py
executable file
·285 lines (194 loc) · 9.28 KB
/
instagram_stuff.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
#!/usr/bin/python
from instagram.client import InstagramAPI
import json
import urllib2
import cv2
import imaging_stuff as image
import os
import time
import pdb
# class defined for the client stuff - standard code from instagram
class client_packet :
def __init__(self, client_id, client_secret, redirect_uri, begin_user_id):
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.begin_user_id = begin_user_id
def access_token(self,flag):
if flag:
scope = []
self.api = InstagramAPI(client_id = self.client_id, client_secret = self.client_secret, redirect_uri = self.redirect_uri)
redirect_uri = self.api.get_authorize_login_url(scope = scope)
print "Visit this page and authorize access in your browser:\n", redirect_uri
code = raw_input("Paste in code in query string after redirect: ").strip()
self.access_token = self.api.exchange_code_for_access_token(code)
print self.access_token
else:
self.access_token = 'Insert Your actual Client Access token to not repeath this again and again'
return self.access_token
# Visit the user node
def visit_user(user_id , stack, known, edges, user_details, api, max_followers = 20 , max_follows = 20, max_bfs_add = 20 , max_collect_media = 20, collect_media_flag = 2, verbose = False):
print "\n\n\n\n\n\n\n#########+++++++++=========+++++++++#########"
# Collect User Data
try :
user = api.user(user_id)
except:
if verbose:
print user_id + " is private"
print "#########+++++++++=========+++++++++#########\n\n\n\n\n\n\n"
print "Current Stack Size : " + str(len(stack))
return stack
print "Initialized user :" + user.username
print "User Id :" + user.id
print "User Full Name: " + user.full_name
print "Number of Images Posted : " + str(user.counts['media'])
print "Number of Followers : " + str(user.counts['followed_by'])
print "Number Follows: " +str(user.counts['follows'])
nodes = open("./userdata/nodes_known.csv",'a')
nodes.write(user.id + "\n")
nodes.close()
user_details.write(user.id + "," + user.username + "," + user.full_name.encode("utf-8","ignore") + "," + str(user.counts['media']) + "," + str(user.counts['followed_by']) + "," + str(user.counts['follows']) + ",")
if not os.path.exists('./profilepictures'):
os.makedirs('profilepictures')
# Collect the User Profile Picture and popularity of profile picture
if collect_media_flag > 0:
image.download_image(user.profile_picture,"./profilepictures/" + user.id + ".jpg")
mit_api = urllib2.urlopen('http://popularity.csail.mit.edu/cgi-bin/image.py?url=' + user.profile_picture) # collect mit popularity score.
mitoutput = json.load(mit_api)
user_details.write(str(mitoutput['popscore']) + "\n")
else:
user_details.write("\n")
# Show User Profile Picture
if verbose and collect_media_flag > 0:
profile_picture = cv2.imread("./profilepictures/" + user.id + ".jpg")
cv2.imshow(user.full_name,profile_picture)
cv2.waitKey(1)
# Collect user's graph neighborhood - follows
tryflag = True
while tryflag :
try:
follows, pagination = api.user_follows(user.id)
tryflag = False
except :
print "Sleeping ... "
print "Remaining API requests " + str(api.x_ratelimit_remaining) + " of " + str(api.x_ratelimit)
time.sleep(60)
print "Awake .. "
while pagination and len(follows) < max_follows:
tryflag = True
while tryflag :
try :
current_list, pagination = api.user_follows(with_next_url = pagination)
follows.extend(current_list)
tryflag = False
except :
print "Sleeping ..."
print "Remaining API requests " + str(api.x_ratelimit_remaining) + " of " + str(api.x_ratelimit)
time.sleep(60)
print "Awake .. "
# Collect user's graph neighborhood - followed by
tryflag = True
while tryflag:
try :
followed_by, pagination = api.user_followed_by(user.id)
tryflag = False
except :
print "Sleeping ..."
print "Remaining API requests " + str(api.x_ratelimit_remaining) + " of " + str(api.x_ratelimit)
time.sleep(60)
print "Awake .. "
while pagination and len(followed_by) < max_followers:
tryflag = True
while tryflag:
try:
current_list, pagination = api.user_follows(with_next_url = pagination)
followed_by.extend(current_list)
tryflag = False
except:
print "sleeping ..."
print "Remaining API requests " + str(api.x_ratelimit_remaining) + " of " + str(api.x_ratelimit)
time.sleep(60)
print "awake .. "
count = 0
nodes = open("./userdata/found_nodes.csv",'a')
for edge in followed_by:
edges.write(edge.id + "," + user.id + "\n")
if edge.id not in known:
if count < max_bfs_add:
stack.append(edge.id)
count = count + 1
known.append(edge.id)
nodes.write(edge.id + "\n")
count = 0
for edge in follows:
edges.write(user.id + "," + edge.id + "\n")
if edge.id not in known:
if count < max_bfs_add:
stack.append(edge.id)
count = count + 1
known.append(edge.id)
nodes.write(edge.id + "\n")
nodes.close()
if verbose:
print "Number of Followers of " + user.full_name + " crawled : " + str(len(followed_by))
# Collect the user Media
if collect_media_flag > 0:
collect_media(user, api, max_collect_media, collect_media_flag, verbose)
if verbose:
print "Finished Visiting user : " + user.full_name
print "#########+++++++++=========+++++++++#########\n\n\n\n\n\n\n"
print "Current Stack Size : " + str(len(stack))
cv2.destroyAllWindows()
# Collect and store iamges
def collect_media(user, api , max_collect_media, flag, verbose = False ):
print "Collecting Media Of : " + user.full_name
tryflag = True
while tryflag :
try :
media_list, pagination = api.user_recent_media(user_id = user.id, count = max_collect_media)
tryflag = False
except :
print "sleeping .."
print "Remaining API requests " + str(api.x_ratelimit_remaining) + " of " + str(api.x_ratelimit)
time.sleep(60)
print " awake..."
while pagination and len(media_list) < max_collect_media:
tryflag = True
while tryflag:
try:
current_list, pagination = api.user_recent_media(with_next_url = pagination)
media_list.extend(current_list)
tryflag = False
except:
print "sleeping .."
print "Remaining API requests " + str(api.x_ratelimit_remaining) + " of " + str(api.x_ratelimit)
time.sleep(60)
print " awake..."
print "Number of Photo Information Acquired From " + user.full_name + " is : " + str(len(media_list))
if not os.path.exists("./media/" + user.id):
os.makedirs("./media/" + user.id)
f = open("./media/" + user.id + "/data.csv",'w')
count = 0
for media in media_list:
if media.type == "image":
current_url = media.get_standard_resolution_url()
if flag == 2:
image.download_image(current_url, "./media/" + user.id + "/image_" + str(count) + ".jpg")
mit_api = urllib2.urlopen('http://popularity.csail.mit.edu/cgi-bin/image.py?url=' + current_url)
mitoutput = json.load(mit_api)
if media.caption:
f.write(str(count) + "," + str(media.like_count) + "," + str(media.comment_count) + "," + str(mitoutput['popscore']) + "," + media.filter + "," + media.id + "," + current_url + "," + media.caption.text.encode("utf-8","ignore").replace("/n"," ") + "\n")
else:
f.write(str(count) + "," + str(media.like_count) + "," + str(media.comment_count) + "," + str(mitoutput['popscore']) + "," + media.filter + "," + media.id + "," + current_url + "\n")
if verbose and flag == 2:
current_image = cv2.imread("./media/" + user.id + "/image_" + str(count) + ".jpg")
cv2.imshow(user.username, current_image)
print "For image " + str(count) + " Number of Likes = " + str(media.like_count) + " Number of Comments = " + str(media.comment_count) + " and MIT popularity score = " + str(mitoutput['popscore'])
if media.caption:
print "Caption of the Image : " + media.caption.text
cv2.waitKey(1)
count = count + 1
if verbose:
print "Number of Photos Downloaded from" + user.full_name + " is : " + str(count)
f.close()
cv2.destroyAllWindows()