-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSpammer.py
200 lines (180 loc) · 7.62 KB
/
Spammer.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
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
import pickle , re , time
import json
import base64
import io
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
class Wspammer:
def __init__(self):
self.driver = webdriver.Chrome('chromedriver')
self.driver.get('https://web.whatsapp.com')
self.storage = LocalStorage(self.driver)
def store_to_file(self, filename="default.ws"):
if self.storage != None:
to_store = self.storage.getAll()
fileObject = open(filename, 'wb')
pickle.dump(to_store, fileObject)
def load_from_file(self, filename="default.ws"):
if self.storage != None:
fileObject = open(filename,'rb')
storedict = pickle.load(fileObject)
self.storage.setAll(storedict)
self.driver.refresh()
def spam_person(self, contact, message, times):
input_box = self.get_contact(contact)
if input_box == None:
print("Could Not Find The Contact")
elif input_box == False:
print("People With The Same Name Found")
else:
for i in range(int(times)):
input_box.send_keys(message + Keys.ENTER)
def get_contact(self,contact):
wait = WebDriverWait(self.driver,120)
searchbox = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="side"]/div[2]/div/label/input')))
searchbox.clear()
searchbox.send_keys(contact)
#Waiting for Spinner
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="side"]/div[2]/div/span/div')))
#Waiting for clearbutton
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="side"]/div[2]/div/span/button')))
#Finding Chats
people = self.driver.find_elements_by_class_name("chat")
contacts = []
for person in people:
if person.get_attribute("class") == "chat":
contacts.append(person)
if len(contacts)==1:
searchbox.send_keys(Keys.ENTER)
return self.driver.find_element_by_xpath(
'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
elif len(contacts)>1:
return False
else:
return None
def get_all_contacts(self):
wait = WebDriverWait(self.driver,60)
chatButton = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="side"]/header/div[2]/div/span/div[2]/button/span')))
chatButton.click()
contactBox=wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div/div/div[1]/span[1]/div/span/div/div[2]')))
reachedEnd = False
height = height = self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/span[1]/div/span/div/div[2]/div[2]').size['height']
counter = 0
#to Load All Contacts On to the page
while(not(reachedEnd)):
self.driver.execute_script('arguments[0].scrollTop = ' + str(counter),contactBox)
counter = counter + 100
if counter >= height:
reachedEnd=True
contactElements = self.driver.find_elements_by_class_name("chat-title")
contactNames = []
for contactElement in contactElements:
try:
contactNames.append(contactElement.find_element_by_class_name("emojitext").text)
except NoSuchElementException:
print("")
chatButton = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div/div/div[1]/span[1]/div/span/div/header/div/div/button')))
chatButton.click()
return contactNames
def is_logged_in(self, timegiven=60):
check = self.storage.get("WAToken1")
wait = WebDriverWait(self.driver, timegiven)
if check != None:
try:
y_arg = '//*[@id="side"]/div[2]/div/label/input'
wait.until(EC.presence_of_element_located((By.XPATH, y_arg)))
return True
except TimeoutException:
print("either not connected to the internet,or is logged out")
return False
else:
return False
def wait_till_login(self):
wait = WebDriverWait(self.driver,300)
try:
y_arg = '//*[@id="side"]/div[2]/div/label/input'
wait.until(EC.presence_of_element_located((By.XPATH, y_arg)))
return True
except TimeoutException:
print("either not connected to the internet,or is logged out")
return False
def show_current_qr(self,src,fig):
image64 = re.sub('^data:image/.+;base64,', '',src)
image64 = base64.b64decode(image64)
image64 = io.BytesIO(image64)
image64 = mpimg.imread(image64, format='JPG')
if fig==None:
plt.ion()
fig = plt.figure()
plt.imshow(image64)
plt.axis("off")
plt.show()
else:
plt.imshow(image64)
plt.pause(5.0)
return fig
def login_with_qr(self):
self.storage.clear()
self.driver.refresh()
currentsrc = None
currentfig = None
wait = WebDriverWait(self.driver,30)
while True:
if self.storage.get('WAToken1') == None:
reloadbutton = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="app"]/div/div/div/div[1]/div[1]/div')))
if(reloadbutton.get_attribute('class') == "idle"):
reloadbutton.click()
imagelement = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="app"]/div/div/div/div[1]/div[1]/div/img')))
tmp = imagelement.get_attribute('src')
if currentsrc == tmp:
time.sleep(1)
continue
else:
currentsrc = tmp
if currentfig!=None:
plt.clf()
currentfig = self.show_current_qr(currentsrc,currentfig)
continue
else:
if currentfig!=None:
plt.close(currentfig)
break
class LocalStorage:
def __init__(self, driver):
self.driver = driver
def set(self, key, value):
self.driver.execute_script(
"window.localStorage.setItem('{}',{})".format(key, json.dumps(value)))
def get(self, key=None):
if key:
return self.driver.execute_script(
"return window.localStorage.getItem('{}')".format(key))
else:
return self.driver.execute_script("""
var items = {}, ls = window.localStorage;
for (var i = 0, k; i < ls.length; i++)
items[k = ls.key(i)] = ls.getItem(k);
return items;
""")
def remove(self, key):
self.driver.execute_script(
"window.localStorage.removeItem('{}');".format(key))
def clear(self):
self.driver.execute_script(
"window.localStorage.clear();")
def getAll(self):
toReturn = {}
for key, value in list(self.get().items()):
toReturn[key] = value
return toReturn
def setAll(self, allKeys):
for key, value in allKeys.items():
self.set(key, value)