-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
360 lines (305 loc) · 15.7 KB
/
function.py
File metadata and controls
360 lines (305 loc) · 15.7 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
#making the necessary imports
import requests
import urllib
import Tkinter
import tkMessageBox
from PIL import Image
#This file has the necessary functions being used in other file
#global variable Access token and base url
Access_token='5697615882.a918ceb.8a12a664f5d84dff9d0b4802205e1d78'
base='https://api.instagram.com/v1/'
#sel method to display the details of own profile
def self():
url=base+'users/self/?access_token='+Access_token
print url
self_info=requests.get(url).json()
branch=Tkinter.Tk()
branch.title('SELF DETAILS')
branch.configure(background='red')
if self_info['meta']['code'] == 200:
if len(self_info['data']):
Tkinter.Label(branch, font=('Helvetica', 20), text="The username : "+self_info['data']['username'],fg='blue').grid(row=0, column=0,pady=(10),padx=(10))
#print 'Username: %s' % (self_info['data']['username'])
Tkinter.Label(branch, font=(None, 20), text="No. of followers : " + str(self_info['data']['counts']['followed_by']),fg='blue').grid(row=1,column=0,pady=(10),padx=(10))
#print 'No. of followers: %s' % (self_info['data']['counts']['followed_by'])
Tkinter.Label(branch, font=(None, 20), text="Following : " + str(self_info['data']['counts']['follows']),fg='blue').grid(row=2,column=0,pady=(10),padx=(10))
#print 'No. of people you are following: %s' % (self_info['data']['counts']['follows'])
Tkinter.Label(branch, font=(None, 20), text="No. of posts : " + str(self_info['data']['counts']['media']),fg='blue').grid(row=3,column=0,pady=(10),padx=(10))
#print 'No. of posts: %s' % (self_info['data']['counts']['media'])
else:
Tkinter.label(branch,text= 'User does not exist!',fg='blue').gird(row=3,column=0,pady=(10),padx=(10))
else:
Tkinter.label(branch, text='ERROR!!!',fg='blue').gird(row=3, column=0,pady=(10),padx=(10))
print 'Status code other than 200 received!'
branch.mainloop()
#method to display user's own media from the downloaded path using canvas and Tkinter
def display_self_media(i):
branch = Tkinter.Toplevel()
val = int(i) - 1
print val
branch.configure(background='black')
canvas = Tkinter.Canvas(branch, width=1000, height=1000)
path = 'selfmedia_%s_.jpg' % (str(i))
canvas.pack()
im = Image.open(path)
im = im.convert('RGB')
im.save(path + '.pgm')
img = Tkinter.PhotoImage(file=path + ".pgm")
canvas.create_image(40, 40, image=img, anchor='nw')
branch.mainloop()
# method to download the user's self media and save it to the desired path
def get_self_media(i):
url = (base + 'users/self/media/recent/?access_token=' + Access_token)
val = int(i) - 1
print val
path = 'selfmedia_%s_.jpg' % (str(i))
self_media = requests.get(url).json()
if val >= len (self_media['data'] ):
print "out of bound"
tkMessageBox.showwarning("Error",'This media is not available')
else:
if self_media['data'][val]['images']:
print 'url is ', self_media['data'][val]['images']['standard_resolution']['url']
print 'media id is', self_media['data'][val]['id']
#using urllib library to download the images
urllib.urlretrieve(self_media['data'][val]['images']['standard_resolution']['url'], path)
tkMessageBox.showwarning('succesfull', 'the image has been succesfully downloaded')
else:
print 'ERROR'
#method to get user's id bases on the username of the user being passed as the parameter to the method
#this method performs function via instagram's Api endpoints
def get_user_id(username):
if len(username)==0:
tkMessageBox.showwarning('ERROR','Name can not be empty')
url=(base+'users/search?q=%s&access_token=%s') %(username,Access_token)
print url
user_id=requests.get(url).json()
if len(user_id['data'] ):
user_info(user_id['data'][0]['id'])
return user_id['data'][0]['id']
else :
tkMessageBox.showwarning("Error",'user does not exist')
#this method has user id as its parameter and on the basis of the user id being passed it uses the perticular endpoint to fetch user details
def user_info(user_id):
url = (base+'users/%s/?access_token='+Access_token )%(user_id)
print url
user=requests.get(url).json()
branch=Tkinter.Tk()
branch.title('User details')
branch.configure(background='black')
if user['meta']['code']==200:
if len(user['data'])>1:
Tkinter.Label(branch, font=('Helvetica', 20), text="The username : " + user['data']['username'],fg='blue').grid(row=0, column=0, pady=(10), padx=(10))
# print 'Username: %s' % (self_info['data']['username'])
Tkinter.Label(branch, font=(None, 20),text="No. of followers : " + str(user['data']['counts']['followed_by']),fg='blue').grid(row=1, column=0, pady=(10), padx=(10))
# print 'No. of followers: %s' % (self_info['data']['counts']['followed_by'])
Tkinter.Label(branch, font=(None, 20), text="Following : " + str(user['data']['counts']['follows']),fg='blue').grid(row=2, column=0, pady=(10), padx=(10))
# print 'No. of people you are following: %s' % (self_info['data']['counts']['follows'])
Tkinter.Label(branch, font=(None, 20), text="No. of posts : " + str(user['data']['counts']['media']),fg='blue').grid(row=3, column=0, pady=(10), padx=(10))
# print 'No. of posts: %s' % (self_info['data']['counts']['media'])
else:
Tkinter.label(branch, text='User does not exist!', fg='blue').gird(row=3, column=0, pady=(10), padx=(10))
else :
Tkinter.label(branch, text='ERROR!!!', fg='blue').gird(row=3, column=0, pady=(10), padx=(10))
print 'Status code other than 200 received!'
branch.mainloop()
#this method download the users media using instagram api endpoints and the urllib library
def get_user_media(username,i):
if len(username)==0:
tkMessageBox.showwarning('ERROR','Name can not be empty')
url=(base+'users/search?q=%s&access_token=%s') %(username,Access_token)
print url
user_id=requests.get(url).json()
val = int(i) - 1
print val
path = 'usermedia_%s_.jpg' % (str(i))
if len(user_id['data'] ):
use_id=user_id['data'][0]['id']
url=(base+'users/%s/media/recent/?access_token=%s')%(use_id,Access_token)
user_media=requests.get(url).json()
if user_media['data'][0]['images']:
print 'url is ', user_media['data'][val]['images']['standard_resolution']['url']
urllib.urlretrieve(user_media['data'][val]['images']['standard_resolution']['url'], path)
print 'the image has been succesfully downloaded'
print 'user media id =',user_media['data'][0]['id']
#this method is used to display the media downloaded by the urllib library by the help of canvas
def display_umedia(i):
branch = Tkinter.Toplevel()
val = int(i) - 1
print val
branch.configure(background='black')
canvas = Tkinter.Canvas(branch, width=1000, height=1000)
path = 'usermedia_%s_.jpg' % (str(i))
canvas.pack()
im = Image.open(path)
im = im.convert('RGB')
im.save(path + '.pgm')
img = Tkinter.PhotoImage(file=path + ".pgm")
canvas.create_image(40, 40, image=img, anchor='nw')
branch.mainloop()
# this methos puts a like on the user's post
#it has two parameters one for the username and other is the variable which helps in finding the media id of a particula media out of the whole list
def put_like(username,i):
if len(username)==0:
tkMessageBox.showwarning('ERROR','Name can not be empty')
url=(base+'users/search?q=%s&access_token=%s') %(username,Access_token)
print url
user_id=requests.get(url).json()
val = int(i) - 1
print val
if len(user_id['data']):
use_id = user_id['data'][0]['id']
url = (base + 'users/%s/media/recent/?access_token=%s') % (use_id, Access_token)
user_media = requests.get(url).json()
if user_media['data'][0]['images']:
print 'url is ', user_media['data'][val]['images']['standard_resolution']['url']
media_id=user_media['data'][val]['id']
print 'user media id =', user_media['data'][0]['id']
#here the media id is saved to the variable so that it can be used further for like endpoint
url=(base+'media/%s/likes') %(media_id)
#payloads are used to send additional data during post requests
payload={"access_token":Access_token}
print url
post_like=requests.post(url,payload).json()
print post_like
if post_like['meta']['code']==200:
tkMessageBox.showinfo('Succesfull','Succesfully liked the post')
else :
tkMessageBox.showinfo('Failed!!','Unsuccesfull in liking the post')
# this method works in similar way as the put like method
#only difference is that it uses a different enpoint
def post_comment(username,i,comment):
if len(username)==0:
tkMessageBox.showwarning('ERROR','Name can not be empty')
if len(comment)==0:
tkMessageBox.showwarning('ERROR', 'Comment can not be empty')
url=(base+'users/search?q=%s&access_token=%s') %(username,Access_token)
print url
user_id=requests.get(url).json()
val = int(i) - 1
print val
path = 'usermedia_%s_.jpg' % (str(i))
if len(user_id['data']):
use_id = user_id['data'][0]['id']
url = (base + 'users/%s/media/recent/?access_token=%s') % (use_id, Access_token)
user_media = requests.get(url).json()
if user_media['data'][0]['images']:
print 'url is ', user_media['data'][val]['images']['standard_resolution']['url']
media_id=user_media['data'][val]['id']
print 'user media id =', user_media['data'][0]['id']
url=(base+'media/%s/comments') %(media_id)
payloads={'access_token':Access_token,'text':comment}
post_Cmmnt=requests.post(url,payloads).json()
if post_Cmmnt['meta']['code']==200:
tkMessageBox.showinfo('Succesfull', "Comment Posted!!!")
else:
tkMessageBox.showinfo('Unsuccesfull', "Comment Not Posted!!!")
#this method is used to get the list of comments on the user's post by using the appropriate endpoint
#it works in the same way as the like and comment method
def fetch_comments(username,i):
if len(username) == 0:
tkMessageBox.showwarning('ERROR', 'Name can not be empty')
branch=Tkinter.Tk()
branch.configure(background='black')
url = (base + 'users/search?q=%s&access_token=%s') % (username, Access_token)
print url
user_id = requests.get(url).json()
val = int(i) - 1
print val
if len(user_id['data']):
use_id = user_id['data'][0]['id']
url = (base + 'users/%s/media/recent/?access_token=%s') % (use_id, Access_token)
user_media = requests.get(url).json()
if user_media['data'][0]['images']:
print 'url is ', user_media['data'][val]['images']['standard_resolution']['url']
media_id = user_media['data'][val]['id']
print 'user media id =', user_media['data'][0]['id']
url=base+'media/%s/comments?access_token=%s' %(media_id,Access_token)
comment_list=requests.get(url).json()
print comment_list
if comment_list['meta']['code']==200:
if len(comment_list['data'])>0:
for x in range(len(comment_list['data'])):
Tkinter.Label(branch,text=str(x+1)+' . ' + comment_list['data'][x]['from']['full_name']+' : '+comment_list['data'][x]['text'],fg='blue',font=(None,20)).grid(row=x,column=1)
else :
tkMessageBox.showerror('Alert','No comment Found')
else :
tkMessageBox.showerror('Error','Code other than 200 recieved')
branch.mainloop()
#method to find the media with the minimun likes
#it downloads the image and then displays it
def min_likes(username):
if len(username)==0:
tkMessageBox.showwarning('ERROR','Name can not be empty')
url=(base+'users/search?q=%s&access_token=%s') %(username,Access_token)
print url
user_id=requests.get(url).json()
if len(user_id['data'] ):
use_id=user_id['data'][0]['id']
url=(base+'users/%s/media/recent/?access_token=%s')%(use_id,Access_token)
user_media=requests.get(url).json()
if user_media['meta']['code']:
list=[]
count=0
for x in range(len(user_media['data'])):
count=user_media['data'][x]['likes']['count']
list.append(count)
else:
tkMessageBox.showerror('Error','Code other than 200')
list=sorted(list)
print list
for x in range(len(user_media['data'])):
path='min_like%s.jpg' %(str(x))
if list[0]==user_media['data'][x]['likes']['count']:
urllib.urlretrieve(user_media['data'][x]['images']['standard_resolution']['url'], path)
print 'the image has been succesfully downloaded'
branch = Tkinter.Toplevel()
branch.title('Media')
branch.configure(background='black')
canvas = Tkinter.Canvas(branch, width=500, height=500)
canvas.pack()
im = Image.open(path)
im = im.convert('RGB')
im.save(path + '.pgm')
img = Tkinter.PhotoImage(file=path + ".pgm")
canvas.create_image(40, 40, image=img, anchor='nw')
branch.mainloop()
#this methos is similar to the min likes methos but with a slight change
#it find the media with the maximum likes
def max_likes(username):
if len(username) == 0:
tkMessageBox.showwarning('ERROR', 'Name can not be empty')
url = (base + 'users/search?q=%s&access_token=%s') % (username, Access_token)
print url
user_id = requests.get(url).json()
if len(user_id['data']):
use_id = user_id['data'][0]['id']
url = (base + 'users/%s/media/recent/?access_token=%s') % (use_id, Access_token)
user_media = requests.get(url).json()
if user_media['meta']['code']:
list = []
count = 0
for x in range(len(user_media['data'])):
count = user_media['data'][x]['likes']['count']
list.append(count)
else:
tkMessageBox.showerror('Error', 'Code other than 200')
list = sorted(list)
print list
for x in range(len(user_media['data'])):
path = 'min_like%s.jpg' % (str(x))
if max(list) == user_media['data'][x]['likes']['count']:
urllib.urlretrieve(user_media['data'][x]['images']['standard_resolution']['url'], path)
print 'the image has been succesfully downloaded'
branch = Tkinter.Toplevel()
branch.title('Media')
branch.configure(background='black')
canvas = Tkinter.Canvas(branch, width=500, height=500)
canvas.pack()
im = Image.open(path)
im = im.convert('RGB')
im.save(path + '.pgm')
img = Tkinter.PhotoImage(file=path + ".pgm")
canvas.create_image(40, 40, image=img, anchor='nw')
branch.mainloop()
#these are the functions that have been used