-
Notifications
You must be signed in to change notification settings - Fork 0
/
instaBot.py
347 lines (289 loc) · 12.3 KB
/
instaBot.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
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
import requests, urllib
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
#importing colors from colors.py for output/messages.
from colors import prCyan, prRed, prYellow, prGreen, prLightPurple, prPurple , R , B , Black
APP_ACCESS_TOKEN = '340910069.8ed58b0.caadd26c6dfe485286c031a4b6f458dc'
#Token Owner : iam_anasjaveed
#Sandbox Users : javeed_mohsin, vroom_geneva_dragmeet
BASE_URL = 'https://api.instagram.com/v1/'
'''
Function declaration to get your own info
'''
def self_info():
request_url = (BASE_URL + 'users/self/?access_token=%s') % (APP_ACCESS_TOKEN)
print 'GET request url : %s\n' % (request_url)
user_info = requests.get(request_url).json()
if user_info['meta']['code'] == 200:
if len(user_info['data']):
print 'Username: %s' % (user_info['data']['username'])
print 'No. of followers: %s' % (user_info['data']['counts']['followed_by'])
print 'No. of people you are following: %s' % (user_info['data']['counts']['follows'])
print 'No. of posts: %s' % (user_info['data']['counts']['media'])
else:
print 'User does not exist!'
else:
print 'Status code other than 200 received!'
'''
Function declaration to get the ID of a user by username
'''
def get_user_id(insta_username):
request_url = (BASE_URL + 'users/search?q=%s&access_token=%s') % (insta_username, APP_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']):
return user_info['data'][0]['id']
else:
return None
else:
print 'Status code other than 200 received!'
exit()
'''
Function declaration to get the info of a user by username
'''
def get_user_info(insta_username):
user_id = get_user_id(insta_username)
if user_id == None:
print 'User does not exist!'
exit()
request_url = (BASE_URL + 'users/%s?access_token=%s') % (user_id, APP_ACCESS_TOKEN)
print 'GET request url : %s\n' % (request_url)
user_info = requests.get(request_url).json()
if user_info['meta']['code'] == 200:
if len(user_info['data']):
print 'User ID: %s' % (user_info['data']['id'])
print 'Username: %s' % (user_info['data']['username'])
print 'No. of followers: %s' % (user_info['data']['counts']['followed_by'])
print 'No. of people you are following: %s' % (user_info['data']['counts']['follows'])
print 'No. of posts: %s' % (user_info['data']['counts']['media'])
else:
print 'There is no data for this user!'
else:
print 'Status code other than 200 received!'
'''
Function declaration to get your recent post
'''
def get_own_post():
request_url = (BASE_URL + 'users/self/media/recent/?access_token=%s') % (APP_ACCESS_TOKEN)
print 'GET request url : %s\n' % (request_url)
own_media = requests.get(request_url).json()
if own_media['meta']['code'] == 200:
if len(own_media['data']):
image_id = own_media['data'][0]['id']
image_name = own_media['data'][0]['id'] + '.jpeg'
image_url = own_media['data'][0]['images']['standard_resolution']['url']
urllib.urlretrieve(image_url, image_name)
print 'Your image has been downloaded!'
print 'Post/Image ID:'+ image_id
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
'''
Function declaration to get the recent post of a user by username
'''
def get_user_post(insta_username):
user_id = get_user_id(insta_username)
if user_id == None:
print 'User does not exist!'
exit()
request_url = (BASE_URL + 'users/%s/media/recent/?access_token=%s') % (user_id, APP_ACCESS_TOKEN)
print 'GET request url : %s\n' % (request_url)
user_media = requests.get(request_url).json()
if user_media['meta']['code'] == 200:
if len(user_media['data']):
image_id = user_media['data'][0]['id']
image_name = user_media['data'][0]['id'] + '.jpeg'
image_url = user_media['data'][0]['images']['standard_resolution']['url']
urllib.urlretrieve(image_url, image_name)
print 'Your image has been downloaded!'
print 'Post/Image ID:' + image_id
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
'''
Function declaration to get the ID of the recent post of a user by username
'''
def get_post_id(insta_username):
user_id = get_user_id(insta_username)
if user_id == None:
print 'User does not exist!'
exit()
request_url = (BASE_URL + 'users/%s/media/recent/?access_token=%s') % (user_id, APP_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']):
return user_media['data'][0]['id']
else:
print 'There is no recent post of the user!'
exit()
else:
print 'Status code other than 200 received!'
exit()
'''
Function declaration to like the recent post of a user
'''
def like_a_post(insta_username):
media_id = get_post_id(insta_username)
request_url = (BASE_URL + 'media/%s/likes') % (media_id)
payload = {"access_token": APP_ACCESS_TOKEN}
print 'POST request url : %s\n' % (request_url)
post_a_like = requests.post(request_url, payload).json()
if post_a_like['meta']['code'] == 200:
prGreen('Like was successful!')
else:
prRed('Your like was unsuccessful. Try again!')
'''
Function declaration to get recent post like of a user
'''
def get_recent_like(insta_username):
media_id = get_post_id(insta_username)
request_url = (BASE_URL + 'media/%s/likes') % (media_id)
payload = {"access_token": APP_ACCESS_TOKEN}
print 'POST request url : %s\n' % (request_url)
post_a_like = requests.post(request_url, payload).json()
if post_a_like['meta']['code'] == 200:
prGreen('Like was successful!')
else:
prRed('Your like was unsuccessful. Try again!')
'''
Function declaration to make a comment on the recent post of the user
'''
def post_a_comment(insta_username):
media_id = get_post_id(insta_username)
comment_text = raw_input("Your comment: ")
payload = {"access_token": APP_ACCESS_TOKEN, "text" : comment_text}
request_url = (BASE_URL + 'media/%s/comments') % (media_id)
print 'POST request url : %s' % (request_url)
make_comment = requests.post(request_url, payload).json()
if make_comment['meta']['code'] == 200:
print "Successfully added a new comment!"
else:
print "Unable to add comment. Try again!"
'''
Function declaration to make delete negative comments from the recent post
'''
def delete_negative_comment(insta_username):
media_id = get_post_id(insta_username)
request_url = (BASE_URL + 'media/%s/comments/?access_token=%s') % (media_id, APP_ACCESS_TOKEN)
print 'GET request url : %s' % (request_url)
comment_info = requests.get(request_url).json()
if comment_info['meta']['code'] == 200:
if len(comment_info['data']):
#Here's a naive implementation of how to delete the negative comments :)
for x in range(0, len(comment_info['data'])):
comment_id = comment_info['data'][x]['id']
comment_text = comment_info['data'][x]['text']
blob = TextBlob(comment_text, analyzer=NaiveBayesAnalyzer())
if (blob.sentiment.p_neg > blob.sentiment.p_pos):
print 'Negative comment : %s' % (comment_text)
delete_url = (BASE_URL + 'media/%s/comments/%s/?access_token=%s') % (media_id, comment_id, APP_ACCESS_TOKEN)
print 'DELETE request url : %s' % (delete_url)
delete_info = requests.delete(delete_url).json()
if delete_info['meta']['code'] == 200:
print 'Comment successfully deleted!\n'
else:
print 'Unable to delete comment!'
else:
print 'Positive comment : %s\n' % (comment_text)
else:
print 'There are no existing comments on the post!'
else:
print 'Status code other than 200 received!'
'''
Funtion to list comments
'''
def get_comment_list(insta_username):
media_id = get_post_id(insta_username)
request_url = (BASE_URL + 'media/%s/comments/?access_token=%s') % (media_id, APP_ACCESS_TOKEN)
print 'GET request url : %s \n' % (request_url)
comment_info = requests.get(request_url).json()
if comment_info['meta']['code'] == 200:
if len(comment_info['data']):
for x in range(0, len(comment_info['data'])):
comment_id = comment_info['data'][x]['id']
comment_text = comment_info['data'][x]['text']
print comment_text
prGreen('Successfully generated COMMENTS')
else:
prRed('No comments found')
'''
Function Decleration To Get list of Likes
'''
def get_like_list(insta_username):
media_id = get_post_id(insta_username)
request_url = (BASE_URL + "media/%s/likes?access_token=%s") % (media_id, APP_ACCESS_TOKEN)
print "GET request URL : %s" % (request_url)
like_list = requests.get(request_url).json()
users = []
for i in like_list["data"]:
users.append(i["username"])
print users
'''
Function Decleration to get the recent media liked by the user
'''
def post_liked():
url = (BASE_URL + "users/self/media/liked?access_token=%s") % (APP_ACCESS_TOKEN)
media_liked = requests.get(url).json()
if media_liked["meta"]["code"] == 200:
medias = [media_liked["data"][x]["id"] for x in range(0, len(media_liked["data"]))]
print ("**Here are the media id's of post liked by you**")
for i in range(0, len(medias)):
print medias[i]
def start_bot():
while True:
print '\n'
print 'Hey! Welcome to instaBot!\n'
print 'Here are your menu options:\n'
print "a.Get your own details\n"
print "b.Get details of a user by username\n"
print "c.Get your own recent post\n"
print "d.Get the recent post of a user by username\n"
print "e.Get a list of people who have liked the recent post of a user\n"
print "f.Like the recent post of a user\n"
print "g.Get a list of comments on the recent post of a user\n"
print "h.Make a comment on the recent post of a user\n"
print "i.Delete negative comments from the recent post of a user\n"
print "j. Get a List of People Who have Liked the Recent Post of a User\n"
print "k. Get recent media LIKED\n"
print "l.Exit\n"
choice = raw_input("Enter you choice: ")
if choice == "a":
self_info()
elif choice == "b":
insta_username = raw_input("Enter the username of the user: ")
get_user_info(insta_username)
elif choice == "c":
get_own_post()
elif choice == "d":
insta_username = raw_input("Enter the username of the user: ")
get_user_post(insta_username)
elif choice=="e":
insta_username = raw_input("Enter the username of the user: ")
get_like_list(insta_username)
elif choice=="f":
insta_username = raw_input("Enter the username of the user: ")
like_a_post(insta_username)
elif choice=="g":
insta_username = raw_input("Enter the username of the user: ")
get_comment_list(insta_username)
elif choice=="h":
insta_username = raw_input("Enter the username of the user: ")
post_a_comment(insta_username)
elif choice=="i":
insta_username = raw_input("Enter the username of the user: ")
delete_negative_comment(insta_username)
elif choice == "j":
insta_username = raw_input("Enter the username of the user: ")
get_like_list(insta_username)
elif choice == "k":
post_liked()
elif choice == "l":
prLightPurple('Thank you for using InstaBot, \n -Mohammed Anas')
exit()
else:
print "wrong choice"
start_bot()