-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
320 lines (270 loc) · 12.2 KB
/
main.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
# InstaNinja
#InstaNinja is a versatile Instagram automation tool that allows you to manage multiple accounts, send messages, interact with stories, follow/unfollow users, comment on posts, and scrape followers' data with ease.
import tkinter as tk
from tkinter import messagebox
from instagrapi import Client
import json
import threading
import time
# Load config
with open('config.json', 'r') as file:
config = json.load(file)
accounts = config['accounts']
cl = Client()
current_account_index = 0
def switch_account():
global cl, current_account_index
if current_account_index < len(accounts):
account = accounts[current_account_index]
cl.logout()
if 'proxy' in account and account['proxy']:
cl.set_proxy(account['proxy'])
current_account_index += 1
return account
else:
return None
def perform_action(action_func, action_label):
account = switch_account()
if not account:
messagebox.showinfo("Info", "All actions completed for all accounts.")
return
username = account['username']
password = account['password']
label_current_account.config(text=f"Current Account: {username}")
# Show processing overlay
show_processing_overlay()
def wrapper():
try:
cl.login(username, password)
action_func()
except Exception as e:
messagebox.showerror("Error", f"Failed for account {username}: {e}")
finally:
hide_processing_overlay()
if current_account_index < len(accounts):
label_current_account.config(text=f"Current Account: {accounts[current_account_index]['username']}")
else:
label_current_account.config(text=f"All actions completed for all accounts")
messagebox.showinfo(action_label, f"{action_label} completed for {username}")
# Ask the user to select the next action for the new account
action_var.set(None)
threading.Thread(target=wrapper).start()
# Actions
def view_story():
user = entry_story_user.get()
def action():
try:
id_usr = cl.user_id_from_username(user)
time.sleep(5)
lst_stories = cl.user_stories(id_usr)
usr_str_pks = [lstus.pk for lstus in lst_stories]
time.sleep(3)
cl.story_seen(usr_str_pks)
except Exception as e:
messagebox.showerror("Story View", f"Failed to view {user}'s story: {e}")
perform_action(action, "Story View")
def send_message():
message = entry_message.get()
usernames = entry_usernames.get().split(',')
if not message:
messagebox.showerror("Error", "Please enter a message to send.")
return
if not usernames:
messagebox.showerror("Error", "Please enter usernames to send the message to.")
return
def action():
lst_usernames=[]
try:
for username in usernames:
username = username.strip()
if username:
user_id = cl.user_id_from_username(username)
time.sleep(3)
lst_usernames.append(user_id)
cl.direct_send(message,user_ids=lst_usernames)
except Exception as e:
messagebox.showerror("Send Message", f"Failed to send message to {username}: {e}")
perform_action(action, "Send Message")
def like_story():
user = entry_story_user.get()
def action():
try:
id_usr = cl.user_id_from_username(user)
time.sleep(5)
lst_stories = cl.user_stories(id_usr)
for story in lst_stories:
cl.story_like(story.pk)
except Exception as e:
messagebox.showerror("Like Story", f"Failed to like {user}'s story: {e}")
perform_action(action, "Like Story")
def follow():
user = entry_follow_user.get()
def action():
try:
cl.user_follow(cl.user_id_from_username(user))
except Exception as e:
messagebox.showerror("Follow", f"Failed to follow {user}: {e}")
perform_action(action, "Follow")
def unfollow():
user = entry_follow_user.get()
def action():
try:
cl.user_unfollow(cl.user_id_from_username(user))
except Exception as e:
messagebox.showerror("Unfollow", f"Failed to unfollow {user}: {e}")
perform_action(action, "Unfollow")
def comment():
post_url = entry_post_url.get()
comment_text = entry_comment.get()
def action():
try:
media_id = cl.media_id(cl.media_pk_from_url(post_url))
time.sleep(2) # Delay to prevent spam
cl.media_comment(media_id, comment_text)
except Exception as e:
messagebox.showerror("Comment", f"Failed to post comment: {e}")
perform_action(action, "Comment")
def scrape_followers():
usernames = entry_usernames.get().split(',')
if not usernames:
messagebox.showerror("Error", "Please enter usernames to scrape followers.")
return
def action():
all_followers_data = {}
for username in usernames:
username = username.strip()
if username:
try:
user_id = cl.user_id_from_username(username)
user_info=cl.user_info(user_id)
time.sleep(7)
followers = cl.user_followers(user_id)
time.sleep(6)
followers_data = []
for follower in followers.values():
followers_data.append(follower.username)
all_followers_data[username] = {"followers":followers_data, "email":user_info.public_email,"phone":user_info.contact_phone_number}
except Exception as e:
messagebox.showerror("Scrape Followers", f"Failed to scrape followers for {username}: {e}")
with open('followers_data.json', 'w') as file:
json.dump(all_followers_data, file, indent=4)
perform_action(action, "Scrape Followers")
def on_closing():
try:
cl.logout()
except Exception as e:
print(f"Error during logout: {e}")
root.destroy()
def show_processing_overlay():
processing_overlay.place(relx=0, rely=0, relwidth=1, relheight=1)
processing_label.config(text="Processing...")
def hide_processing_overlay():
processing_label.config(text="")
processing_overlay.place_forget()
def show_message_inputs():
entry_post_url.grid_forget()
entry_comment.grid_forget()
entry_story_user.grid_forget()
entry_follow_user.grid_forget()
entry_message.grid(row=8, column=1, padx=10, pady=5)
entry_usernames.grid(row=9, column=1, padx=10, pady=5)
def show_like_story_inputs():
entry_post_url.grid_forget()
entry_comment.grid_forget()
entry_story_user.grid(row=6, column=1, padx=10, pady=5)
entry_follow_user.grid_forget()
entry_message.grid_forget()
entry_usernames.grid_forget()
def show_follow_inputs():
entry_post_url.grid_forget()
entry_comment.grid_forget()
entry_story_user.grid_forget()
entry_follow_user.grid(row=7, column=1, padx=10, pady=5)
entry_message.grid_forget()
entry_usernames.grid_forget()
def show_unfollow_inputs():
entry_post_url.grid_forget()
entry_comment.grid_forget()
entry_story_user.grid_forget()
entry_follow_user.grid(row=7, column=1, padx=10, pady=5)
entry_message.grid_forget()
entry_usernames.grid_forget()
def show_comment_inputs():
entry_post_url.grid(row=4, column=1, padx=10, pady=5)
entry_comment.grid(row=5, column=1, padx=10, pady=5)
entry_story_user.grid_forget()
entry_follow_user.grid_forget()
entry_message.grid_forget()
entry_usernames.grid_forget()
def show_view_story_inputs():
entry_post_url.grid_forget()
entry_comment.grid_forget()
entry_story_user.grid(row=6, column=1, padx=10, pady=5)
entry_follow_user.grid_forget()
entry_message.grid_forget()
entry_usernames.grid_forget()
def show_scrape_followers_inputs():
entry_post_url.grid_forget()
entry_comment.grid_forget()
entry_story_user.grid_forget()
entry_follow_user.grid_forget()
entry_message.grid_forget()
entry_usernames.grid(row=9, column=1, padx=10, pady=5)
# GUI setup
root = tk.Tk()
root.title("Instagram Bot")
root.geometry("1400x600") # Increased window size
tk.Label(root, text="Action", font=('Arial', 14)).grid(row=0, column=0, padx=10, pady=5)
action_var = tk.StringVar()
action_var.set(None)
# Radio Buttons
tk.Radiobutton(root, text="Send Message", variable=action_var, value="Send Message", font=('Arial', 12),
command=show_message_inputs).grid(row=1, column=0, padx=10, pady=5)
tk.Radiobutton(root, text="Like Story", variable=action_var, value="Like Story", font=('Arial', 12),
command=show_like_story_inputs).grid(row=1, column=1, padx=10, pady=5)
tk.Radiobutton(root, text="Follow", variable=action_var, value="Follow", font=('Arial', 12),
command=show_follow_inputs).grid(row=1, column=2, padx=10, pady=5)
tk.Radiobutton(root, text="Unfollow", variable=action_var, value="Unfollow", font=('Arial', 12),
command=show_unfollow_inputs).grid(row=1, column=3, padx=10, pady=5)
tk.Radiobutton(root, text="Comment", variable=action_var, value="Comment", font=('Arial', 12),
command=show_comment_inputs).grid(row=1, column=4, padx=10, pady=5)
tk.Radiobutton(root, text="View Story", variable=action_var, value="View Story", font=('Arial', 12),
command=show_view_story_inputs).grid(row=1, column=5, padx=10, pady=5)
tk.Radiobutton(root, text="Scrape Followers", variable=action_var, value="Scrape Followers", font=('Arial', 12),
command=show_scrape_followers_inputs).grid(row=1, column=6, padx=10, pady=5)
# Action Labels
tk.Label(root, text="Message", font=('Arial', 14)).grid(row=8, column=0, padx=10, pady=5)
tk.Label(root, text="Story User", font=('Arial', 14)).grid(row=6, column=0, padx=10, pady=5)
tk.Label(root, text="User", font=('Arial', 14)).grid(row=7, column=0, padx=10, pady=5)
tk.Label(root, text="Post URL", font=('Arial', 14)).grid(row=4, column=0, padx=10, pady=5)
tk.Label(root, text="Comment", font=('Arial', 14)).grid(row=5, column=0, padx=10, pady=5)
tk.Label(root, text="Usernames (comma-separated)", font=('Arial', 14)).grid(row=9, column=0, padx=10, pady=5)
entry_message = tk.Entry(root, font=('Arial', 14), width=30)
entry_story_user = tk.Entry(root, font=('Arial', 14), width=30)
entry_follow_user = tk.Entry(root, font=('Arial', 14), width=30)
entry_post_url = tk.Entry(root, font=('Arial', 14), width=30)
entry_comment = tk.Entry(root, font=('Arial', 14), width=30)
entry_usernames = tk.Entry(root, font=('Arial', 14), width=30)
entry_message.grid(row=8, column=1, padx=10, pady=5)
entry_usernames.grid(row=9, column=1, padx=10, pady=5)
entry_story_user.grid_forget()
entry_follow_user.grid_forget()
entry_post_url.grid_forget()
entry_comment.grid_forget()
label_current_account = tk.Label(root, text=f"Current Account: {accounts[0]['username']}", font=('Arial', 14))
label_current_account.grid(row=10, column=0, columnspan=2, padx=10, pady=5)
# Action Buttons
tk.Button(root, text='Perform Action', command=lambda: action_var.get() == "Send Message" and send_message() or
action_var.get() == "Like Story" and like_story() or
action_var.get() == "Follow" and follow() or
action_var.get() == "Unfollow" and unfollow() or
action_var.get() == "Comment" and comment() or
action_var.get() == "View Story" and view_story() or
action_var.get() == "Scrape Followers" and scrape_followers(),
font=('Arial', 14), width=15).grid(row=11, column=1, pady=10)
# Processing overlay
processing_overlay = tk.Frame(root, bg='gray')
processing_label = tk.Label(processing_overlay, text="", font=('Arial', 20), bg='gray', fg='white')
processing_label.pack(expand=True)
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()