-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusy_GUI.py
328 lines (293 loc) · 12.8 KB
/
Busy_GUI.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
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from PIL import Image, ImageTk
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from os import listdir
from os.path import isfile, join
import requests, time, pathlib, re, os, sys
import subprocess
import tkinter as tk
import tkinter.ttk as ttk
from plyer import notification
import webbrowser
import threading
old = 0
window = tk.Tk(className='Shubham computers')
window.geometry("500x500")
# T = tk.Text(window, height = 6, width = 53)
window['background']='#00c4cc'
image1 = Image.open("C:\\Users\\Lenovo\\Pictures\\Saved Pictures\\sc.png")
test = ImageTk.PhotoImage(image1)
label1 = tk.Label(image=test)
label1.image = test
label1.place(x=0, y=0)
global progress
progress = ttk.Progressbar(window, orient = tk.HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(padx=5, pady=25, side=tk.BOTTOM)
class OnMyWatch:
def __init__(self, driver):
self.observer = Observer()
self.driver = driver
def run(self, driver):
event_handler = Handler(driver)
self.observer.schedule(event_handler, "C:\\Users\\Lenovo\\Desktop\\Busy_Automation", recursive = True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Observer Stopped")
self.observer.join()
class Handler(FileSystemEventHandler):
def __init__(self, driver):
self.driver = driver
def element_presence(self, by,xpath,time):
element_present = EC.presence_of_element_located((by, xpath))
WebDriverWait(self.driver, time).until(element_present)
def msgTxt(self):
global types, name, mobile_numbers, bill_no, amount, owner,message_text
with open("C://Users//Lenovo//Desktop//Busy_Automation//details.txt") as file:
types = file.readline()[:-1]
name = file.readline()[:-1]
mobile_numbers = [int("91"+mobile) for mobile in file.readline()[:-1].split(",")]
bill_no = file.readline()[:-1]
amount = file.readline()[:-1]
owner = "Ajay Mantri"
message_text='Dear '+name+' Please find the attached "'+types+'" for Rs. '+amount+'. Regards '+owner+'.'
def send_msg(self, text):
self.element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
txt_box=self.driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
self.msgTxt()
txt_box.send_keys(text)
txt_box.send_keys("\n")
attachment_box = self.driver.find_element(By.XPATH, '//div[@title="Attach"]')
attachment_box.click()
pdf_box = self.driver.find_element(By.XPATH, '//input[@accept="*"]')
mypath = "C://Users//Lenovo//Desktop//Busy_Automation"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in onlyfiles:
if file != 'details.txt':
bill = file
file_path = "C:\\Users\\Lenovo\\Desktop\\Busy_Automation\\" + bill;
pdf_box.send_keys(file_path)
self.element_presence(By.XPATH, '//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div', 30)
send_btn = self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div')
time.sleep(2)
send_btn.click()
time.sleep(2)
def search_contact(self, phone_no, text):
self.element_presence(By.XPATH,'//div[text()=\"Search or start new chat\"]/following-sibling::*/child::*/child::*/following-sibling::*', 30)
searchbar = self.driver.find_element(By.XPATH, '//div[text()=\"Search or start new chat\"]/following-sibling::*/child::*/child::*/following-sibling::*')
searchbar.send_keys(phone_no)
searchbar.send_keys("\n")
def on_any_event(self, event):
global old
if event.is_directory:
return None
elif event.event_type == 'modified':
global types, name, mobile_numbers, bill_no, amount, owner,message_text
self.msgTxt()
try:
mypath = "C://Users//Lenovo//Desktop//Busy_Automation"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in onlyfiles:
if file != 'details.txt':
bill = file
file_path = "C:\\Users\\Lenovo\\Desktop\\Busy_Automation\\" + bill
statbuf = os.stat(file_path)
new = statbuf.st_mtime
if new - old > 0.5:
try:
driver.find_element(By.XPATH, '//div[@class="_1wQdF"]')
notification.notify(
title = "Phone not Connected",
message = "Please check your phone's connection",
app_icon = "sc.ico",
timeout = 1
)
except:
for mobile in mobile_numbers:
self.search_contact(mobile, message_text)
self.send_msg(message_text)
notification.notify(
title = "Success",
message = "Your Message is delivered Successfully",
app_icon = "sc.ico",
timeout = 1
)
window.update()
old = new
except Exception as e:
print(e)
# elif event.event_type == 'modified':
# try:
# #set_up_whatsapp(driver)#,moblie_no,message_text)
# for mobile in mobile_numbers:
# self.search_contact(mobile, message_text)
# self.send_msg(message_text)
# notification.notify(
# title = "Success",
# message = "Your Message is delivered Successfully",
# app_icon = "sc.ico",
# timeout = 1
# )
# except Exception as e:
# print(e)
def element_presence(driver, by,xpath,time):
element_present = EC.presence_of_element_located((by, xpath))
WebDriverWait(driver, time).until(element_present)
def initializing_driver():
driveron = False;
options = FirefoxOptions()
profile = "C://Users//Lenovo//AppData//Roaming//Mozilla//Firefox//Profiles//j5hi6h30.smprofile"
options.add_argument("-profile")
options.add_argument(profile)
options.add_argument("--headless")
options.add_argument('--no-sandbox')
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(executable_path="C://Users//Lenovo//Desktop//geckodriver.exe",options=options, capabilities=firefox_capabilities, service_args=["--marionette-port", "2828"])
try:
driver.get("https://web.whatsapp.com/")
element_presence(driver,By.XPATH,'//div[text()=\"Search or start new chat\"]/following-sibling::*/child::*/child::*/following-sibling::*', 30)
notification.notify(
title = "Success",
message = "Connected to Whatsapp",
app_icon = "sc.ico",
timeout = 1
)
except Exception as e:
print(e)
notification.notify(
title = "Error",
message = "Please Check your internet connection.",
app_icon = "sc.ico",
timeout = 0.5
)
global progress
progress.stop()
global counter
counter = 1
driver.close()
sys.exit()
# window.quit()
return driver
def checkLoginStatus(driver):
try:
driver.find_element(By.XPATH, '//div[@class="O1rXL"]')
return False
except NoSuchElementException:
return True
def watchDir(driver):
watch = OnMyWatch(driver)
watch.run(driver)
global t
def check_thread():
global counter
if counter == 1:
b1["state"] = "normal"
b2["state"] = "disabled"
driver.close()
window.update()
counter = 0
else:
window.after(1000, check_thread)
def startProgress():
global progress
progress.start(10)
def startThread(cmd):
global counter
if cmd == 1:
counter = 0
b1["state"] = "disabled"
b2["state"] = "normal"
tp = threading.Thread(target=startProgress, args=(), daemon = True)
tp.start()
t = threading.Thread(target=startDriver, args=(cmd,), daemon = True)
t.start()
window.after(1000, check_thread)
else:
counter = 1
def startDriver(cmd):
if cmd == 1:
try:
os.system("taskkill /IM firefox.exe /F")
except:
print(e)
global driver
driver = initializing_driver()
if(not checkLoginStatus(driver)):
notification.notify(
title = "Please Login Whatsapp",
message = "Please Open Firefox and Scan QR code to login.",
app_icon = "sc.ico",
timeout = 0.5
)
webbrowser.get('firefox').open_new_tab("https://web.whatsapp.com/")
time.sleep(10)
else:
global progress
progress.stop()
watchDir(driver)
else:
driver.close()
global b2,b1
b1=tk.Button(window, text="Start", command=lambda: startThread(1), height=2, width=10, bg='black',fg='white')
b1.place(x=380, y=430)
b2=tk.Button(window, text="Stop", command=lambda: startThread(2), height=2, width=10, bg='black', fg='white')
b2.place(x=30, y=430)
window.mainloop()
############################################# Force #########################################################
def element_presence(driver,by,xpath,time):
element_present = EC.presence_of_element_located((by, xpath))
WebDriverWait(driver, time).until(element_present)
def send_msg(driver, text):
element_presence(driver,By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
txt_box.send_keys(text)
txt_box.send_keys("\n")
attachment_box = driver.find_element(By.XPATH, '//div[@title="Attach"]')
attachment_box.click()
pdf_box = driver.find_element(By.XPATH, '//input[@accept="*"]')
pdf_box.send_keys(file_path)
element_presence(driver,By.XPATH, '//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div', 30)
send_btn = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div')
time.sleep(2)
send_btn.click()
time.sleep(2)
def search_contact(driver, phone_no, text):
element_presence(driver, By.XPATH,'//div[text()=\"Search or start new chat\"]/following-sibling::*/child::*/child::*/following-sibling::*', 30)
searchbar = driver.find_element(By.XPATH, '//div[text()=\"Search or start new chat\"]/following-sibling::*/child::*/child::*/following-sibling::*')
searchbar.send_keys(phone_no)
searchbar.send_keys("\n")
def force():
driver = initializing_driver()
if(not checkLoginStatus(driver)):
notification.notify(
title = "Please Login Whatsapp",
message = "Please Open Firefox and Scan QR code to login.",
app_icon = "sc.ico",
# the notification stays for 50sec
timeout = 1
)
webbrowser.get('firefox').open_new_tab("https://web.whatsapp.com/")
time.sleep(5)
else:
try:
#set_up_whatsapp(driver)#,moblie_no,message_text)
for mobile in mobile_numbers:
search_contact(driver, mobile, message_text)
send_msg(driver, message_text)
except Exception as e:
print(e)
driver.close()
##################################################################################################