-
Notifications
You must be signed in to change notification settings - Fork 1
/
instabot.py
227 lines (202 loc) · 8.91 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
import requests
import urllib
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
APP_ACCESS_TOKEN = '1138029990.33de34c.1d8c6ac982c441b99d30eda40468aa78'
BASE_URL = 'https://api.instagram.com/v1/'
#Function for Self Info
def self():
request_url = (BASE_URL + 'users/self/?access_token=%s') % (APP_ACCESS_TOKEN)
print 'Getting Info from the Url : %s' % (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 'Bio: %s' % (user_info['data']['bio'])
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' % (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 'Bio: %s' % (user_info['data']['bio'])
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!'
def get_own_post():
request_url = (BASE_URL + 'users/self/media/recent/?access_token=%s') % APP_ACCESS_TOKEN
print 'GET request url : %s' % (request_url)
my_media = requests.get(request_url).json()
if my_media['meta']['code'] == 200:
if len(my_media['data']):
image_name = my_media['data'][0]['id'] + '.jpeg'
image_url = my_media['data'][0]['images']['standard_resolution']['url']
urllib.urlretrieve(image_url, image_name)
print 'Your image has been downloaded!'
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
def get_users_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' % (request_url)
user_media = requests.get(request_url).json()
if user_media['meta']['code'] == 200:
if len(user_media['data']):
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!'
else:
print 'Post does not exist!'
else:
print 'Status code other than 200 received!'
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()
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' % (request_url)
post_a_like = requests.post(request_url, payload).json()
if post_a_like['meta']['code'] == 200:
print 'Like was successful!'
else:
print 'Your like was unsuccessful. Try again!'
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!"
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']):
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!'
def start_bot():
while True:
print '\n'
print 'Hey! Welcome to instaBot!'
print 'Here are your menu options:'
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.Exit"
choice=raw_input("Enter you choice: ")
if choice=="a":
self()
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_users_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: ")
make_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":
exit()
else:
print "wrong choice"
start_bot()