-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstabot.py
More file actions
410 lines (368 loc) · 16.1 KB
/
instabot.py
File metadata and controls
410 lines (368 loc) · 16.1 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import requests, urllib
from termcolor import colored
from access_token import ACCESS_TOKEN
from user_detail import User, user_list,Recent_Media, media_list
from clarifai.rest import ClarifaiApp
import csv
BASE_URL = 'https://api.instagram.com/v1/'
# Function retrieve owner's information
def self_info():
print "Displaying Own Information"
request_url = (BASE_URL + 'users/self/?access_token=%s') % (ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
try:
owner_info = requests.get(request_url).json()
if owner_info['meta']['code'] == 200: # HTTP 200 means transmission is OK
if len(owner_info['data']):
print colored("Username: ", "red"), '%s' % colored(owner_info['data']['username'], "blue")
print colored('Number of followers: ', "red"), '%s' % colored(owner_info['data']['counts']['followed_by'], "blue")
print colored('Number of people you are following: ', "red"), '%s' % colored(owner_info['data']['counts']['follows'],
"blue")
print colored('Number of posts: ', "red"), '%s' % colored(owner_info['data']['counts']['media'], "blue")
print owner_info
else:
print 'User does not exist!'
else:
print 'Status code other than 200 received!'
except:
print "Something Wrong with the url"
# Function to add user using username and generate corresponding user id
def add_user():
username = raw_input("Enter Username: ")
try:
request_url = (BASE_URL + 'users/search?q=%s&access_token=%s') % (username, ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
user_info = requests.get(request_url).json()
if user_info['meta']['code'] == 200:
if len(user_info['data']):
id = user_info['data'][0]['id']
# print id
# print user_info
print "User has been added successfully!!"
user_list.append(User(username, id))
else:
print "Invalid username!!"
else:
print "Status code not 200!"
except:
print "Url request exception occurred!"
# Function to select user for further operations
def select_user():
if not user_list:
return None
else:
index = 1
for i in range(0, len(user_list)):
print '%d. %s' % (index, user_list[i].name)
index = index + 1
while True:
choice = raw_input("Enter Your Choice: ")
try:
choice = int(choice)
break
except:
print colored("Enter Valid Choice", "red")
choice = int(choice)
return choice - 1
# Function to fetch user details by selecting a user
def fetch_user_details():
index = select_user()
if index is not None:
id1 = user_list[index].id
request_url = (BASE_URL + 'users/%s?access_token=%s') % (id1, ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
try:
user_info = requests.get(request_url).json()
if user_info['meta']['code'] == 200: # HTTP 200 means transmission is OK
if len(user_info['data']):
print colored("Username: ", "red"), '%s' % colored(user_info['data']['username'], "blue")
if len(user_info['data']['bio']):
print colored("Bio: ", "red"), '%s' % colored(user_info['data']['bio'], "blue")
else:
print colored("Bio: ", "red"), '%s' % colored("No Bio in his profile", "blue")
print colored('Number of followers: ', "red"), '%s' % colored(user_info['data']['counts']['followed_by'], "blue")
print colored('Number of people you are following: ', "red"), '%s' % colored(
user_info['data']['counts']['follows'],
"blue")
print colored('Number of posts: ', "red"), '%s' % colored(user_info['data']['counts']['media'], "blue")
# print user_info
else:
print 'User does not exist!'
else:
print 'Status code other than 200 received!'
except:
print "Something Wrong with the url"
else:
print 'Add Users Please'
# Function to fetch self recent posts.
def fetch_own_recent_posts():
count = int(raw_input("Enter number of posts you want to retrieve: "))
try:
request_url = (BASE_URL + 'users/self/media/recent/?access_token=%s&count=%d') % (ACCESS_TOKEN, count)
print 'GET request url : %s' % (request_url)
own_media = requests.get(request_url).json()
if own_media['meta']['code'] == 200:
if len(own_media['data']):
if len(own_media['data']) >= count:
for i in range(0, count):
image_name = own_media['data'][i]['id'] + '.jpeg'
image_url = own_media['data'][i]['images']['standard_resolution']['url']
urllib.urlretrieve(image_url, image_name)
print 'Your image has been downloaded!'
else:
print "Your count limit exceeds bye user total posts"
print own_media
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
except:
print "Something wrong with url"
# Function to fetch recent posts of user by selecting it from list.
def fetch_user_recent_posts():
index = select_user()
if index is not None:
id1 = user_list[index].id
name = user_list[index].name
if not media_list:
count = int(raw_input("Enter number of posts you want to retrieve: "))
try:
request_url = (BASE_URL + 'users/%s/media/recent/?access_token=%s&count=%d') % (id1, ACCESS_TOKEN, count)
print 'GET request url : %s' % (request_url)
user_media = requests.get(request_url).json()
if user_media['meta']['code'] == 200:
if len(user_media['data']):
if len(user_media['data']) >= count:
for i in range(0, count):
media_id = user_media['data'][i]['id']
media_type = user_media['data'][i]['type']
media_url = user_media['data'][i]['images']['standard_resolution']['url']
media_likes = user_media['data'][i]['likes']['count']
media_list.append(Recent_Media(name, media_id, media_type, media_url, media_likes))
ch = raw_input("Do you want to download image (y/n): ")
if ch.upper() == 'Y':
image_name = user_media['data'][i]['id'] + '.jpeg'
urllib.urlretrieve(media_url, image_name)
print 'Your image has been downloaded!'
else:
print "Your count limit exceeds bye user total posts"
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
except:
print "Something wrong with url"
else:
print "Please Add Users"
# Function to like recent post of user
def like_recent_post():
index = select_user()
name = user_list[index].name
if index is not None:
for i in range(0, len(media_list)):
if media_list[i].name == name:
media_id = media_list[i].media_id
try:
request_url = (BASE_URL + 'media/%s/likes') % (media_id)
print 'GET request url : %s' % (request_url)
payload = {'access_token': ACCESS_TOKEN}
post_like = requests.post(request_url, payload).json()
if post_like['meta']['code'] == 200:
print "You have liked the pic successfully!!"
break
else:
print "Status code not 200!"
break
except:
print "Url request exception occurred!!!"
break
else:
print "Please add users!!"
# Function delete a like from recent post of user
def delete_like():
index = select_user()
name = user_list[index].name
if index is not None:
for i in range(0, len(media_list)):
if media_list[i].name == name:
media_id = media_list[i].media_id
try:
request_url = (BASE_URL + 'media/%s/likes?access_token=%s') % (media_id, ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
delete_like = requests.delete(request_url).json()
if delete_like['meta']['code'] == 200:
print "You have unliked the pic successfully!!"
break
else:
print "Status code not 200!"
break
except:
print "Url request exception occurred!!!"
break
else:
print "Please add users!!"
# Function to post a comment on recent post of user
def post_comment():
index = select_user()
name = user_list[index].name
if index is not None:
for i in range(0, len(media_list)):
if media_list[i].name == name:
media_id = media_list[i].media_id
try:
request_url = (BASE_URL + 'media/%s/comments?access_token=%s') % (media_id, ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
list_comment = requests.get(request_url).json()
if list_comment['meta']['code'] == 200:
print "Your comment has been successfully posted!!!!"
break
else:
print "Status code not 200!"
break
except:
print "Url request exception occurred!!!"
break
else:
print "Please add users!!"
# Function to list comments on recent post of a user
def list_comment():
index = select_user()
name = user_list[index].name
if index is not None:
for i in range(0, len(media_list)):
if media_list[i].name == name:
media_id = media_list[i].media_id
try:
request_url = (BASE_URL + 'media/%s/comments?access_token=%s') % (media_id, ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
post_like = requests.get(request_url).json()
if post_like['meta']['code'] == 200:
for i in range(0, len(post_like['data'])):
print "%s commented by: %s" % (post_like['data'][i]['text'], post_like['data'][i]['from']['username'])
break
else:
print "Status code not 200!"
break
except:
print "Url request exception occurred!!!"
break
else:
print "Please add users!!"
# Function to det images of natural calamity using Clarifai API
def get_images():
app = ClarifaiApp(api_key='c421a2e2717246b09706b18c36039e79')
app.inputs.delete_all()
# For Better results have maximum users in sandbox
# mriutestbot id
id = 5716892371
image_list = []
url_list = []
request_url = (BASE_URL + 'users/%s/media/recent/?access_token=%s') % (id, ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
user_media = requests.get(request_url).json()
if user_media['meta']['code'] == 200:
if len(user_media['data']):
for i in range(0, len(user_media['data'])):
media = {}
print user_media['data'][i]['images']['low_resolution']['url']
media['media_url'] = user_media['data'][i]['images']['low_resolution']['url']
media['latitude'] = user_media['data'][i]['location']['latitude']
media['longitude'] = user_media['data'][i]['location']['longitude']
media['location'] = user_media['data'][i]['location']['name']
app.inputs.create_image_from_url(media['media_url'])
image_list.append(media)
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
data1 = app.inputs.search_by_predicted_concepts(concept='cyclone')
data2 = app.inputs.search_by_predicted_concepts(concept='flood')
data3 = app.inputs.search_by_predicted_concepts(concept='drought')
data4 = app.inputs.search_by_predicted_concepts(concept='earthquake')
print len(data1)
for i in range(0, len(data1)):
print data1[i].url
url = {}
url['url'] = data1[i].url
url['name'] = 'Cyclone'
url_list.append(url)
for i in range(0, len(data2)):
url = {}
print data2[i].url
url['url'] = data2[i].url
url['name'] = 'flood'
url_list.append(url)
for i in range(0 , len(data3)):
url = {}
print data3[i].url
url['url'] = data3[i].url
url['name'] = 'drought'
url_list.append(url)
for i in range(0 ,len(data4)):
url = {}
print data4[i].url
url['url'] = data4[i].url
url['name'] = 'earthquake'
url_list.append(url)
print url_list
print image_list
with open('image.csv', 'wb') as out:
writer = csv.DictWriter(out, fieldnames=['Url', 'Longitude', 'Latitude', 'Name', 'Location'], delimiter=';')
writer.writeheader()
for i in range(0, len(url_list)):
for j in range(0 ,len(image_list)):
if url_list[i]['url'] == image_list[j]['media_url']:
writer.writerow({'Url': image_list[j]['media_url'], 'Latitude': image_list[j]['latitude'],
'Longitude': image_list[j]['longitude'], 'Name': url_list[i]['name'],
'Location': image_list[j]['location']})
def start():
while True:
print 'Hey! Welcome to instaBot!'
print 'Here are your menu options:'
print "1.Get your own details"
print "2.Add User"
print "3.Fetch User Details"
print "4.Fetch your own recent post"
print "5.Fetch the recent post of a user"
print "6.Like the most recent post of a user"
print "7.Unlike the most recent post of a user"
print "8.Post a comment on user's recent post"
print "9.Get a list of comments on user's recent post"
print "10.Getting natural Calamities Images"
print "11.Exit"
while True:
choice = raw_input("Enter Your Choice: ")
try:
choice = int(choice)
break
except:
print 'Please Enter Valid Option'
choice = int(choice)
if choice == 1:
self_info()
elif choice == 2:
add_user()
elif choice == 3:
fetch_user_details()
elif choice == 4:
fetch_own_recent_posts()
elif choice == 5:
fetch_user_recent_posts()
elif choice == 6:
like_recent_post()
elif choice == 7:
delete_like()
elif choice == 8:
post_comment()
elif choice == 9:
list_comment()
elif choice == 10:
get_images()
elif choice == 11:
exit()
else:
print "Enter valid option"
ch = raw_input("Do you wish to continue: y/n ")
if ch.upper() == "N":
break
start()