-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
413 lines (333 loc) · 12.4 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# -*- coding:utf-8 -*-
"""
Author: Joel Ermantraut
File: main.py
Desc: Main file of project.
"""
"""
Site Structure:
_3Pwfx: Chat selector class
_22mTQ: Image
_1C6Zl: Name and last message
_1c_mC:
_3Tw1q:
_3es8f:
span: name
_2AuNk:
_3SvgF:
DuUXI:
_1awRl copyable-text selectable-text: Input
_1yHR2:
Barcode
"""
from automation_scripts.web_scrapping import WebScrapper
from automation_scripts.mouse_control import MouseControl
from automation_scripts.files_use import FileUse
from time import sleep
import argparse
from os import remove, getcwd
from os.path import exists, expanduser
import sys
class WhatsMatic(object):
"""App to control WhatsApp and automate it."""
def __init__(self, HOME):
self.side_element = "._2ruyW"
self.list_of_chats_element = "._3soxC"
self.header_element = "._2O84H"
self.search_bar_element = "._1Ra05"
self.chat_button_element = "._3Pwfx"
self.chat_element = "._3Pwfx ._1C6Zl ._3es8f span"
self.input_element = "._2AuNk ._1awRl"
self.barcode_element = "._1yHR2"
self.notification_message_element = "._3Aa1y"
self.chat_per_page = 5
self.files_path = getcwd()
if exists(self.files_path + "/chromedriver.txt"):
with open(self.files_path + "/chromedriver.txt", "r") as file:
self.chromedriver = file.readline()
else:
self.chromedriver = HOME + "/chromedriver"
def init(self):
"""
Inits objects.
"""
self.driver = WebScrapper(
self.chromedriver,
"https://web.whatsapp.com"
)
self.mouse = MouseControl()
self.files = FileUse()
self.driver.maximize()
self.wait_for_scan()
self.wait_for_notification_message()
def wait_for_scan(self):
"""
When WhatsApp is open, the barcode is shown.
This functions waits after it is scanned.
"""
while self.driver.get_elements(self.barcode_element):
sleep(1)
def wait_for_notification_message(self):
"""
After a while, WhastApp generates a notification message
over the chat box that produce problem with elements
attachment.
"""
while not self.driver.get_elements(self.notification_message_element):
sleep(1)
self.driver.scripting("return document.\
getElementsByClassName('{}')[0].remove();".format(self.notification_message_element[1:]))
# And removes it
def is_element(self, attribute, value, elements):
"""
Searches an element with the given properties.
"""
for element in elements:
chat_properties = self.driver.get_all_properties(element)[0]
if value in chat_properties.values():
return element
return None
def get_number_of_chats(self):
"""
Gets the number of times that it must do scroll to
see all contacts of chat list.
"""
side = self.driver.get_elements(self.side_element)[0]
header = self.driver.get_elements(self.header_element)[0]
search_bar = self.driver.get_elements(self.search_bar_element)[0]
list_of_chats = self.driver.get_elements(self.list_of_chats_element)[0]
side_size = side.size['height']
header_size = header.size['height']
search_bar_size = search_bar.size['height']
list_of_chats_size = list_of_chats.size['height']
list_of_chat_show_height = side_size - header_size - search_bar_size
iters = list_of_chats_size // list_of_chat_show_height + 1
# Plus one because the division will trunk the value
return list_of_chat_show_height, iters
def get_contacts(self):
"""
Gets the list of chat contacts.
Take care that this function don't take care of contacts
registered that where not writted.
"""
contacts = []
self.mouse.mouse_move((30, 450))
# Moves mouse to list of chats to scroll
side_height, iters = self.get_number_of_chats()
for i in range(iters):
elements = self.driver.get_elements(self.chat_element)
for element in elements:
title = self.driver.get_all_properties(element)[0]['title']
if title not in contacts:
contacts.append(title)
self.mouse.scroll(side_height) # 1% of height
sleep(1)
# Scrolling in Selenium is not working as it must.
# So, it preferable to use another tool.
return contacts
def get_groups(self):
"""
Returns a list of created groups.
"""
self.files.list_all(self.files_path)
groups = self.files.list_files(r'.*.wmg')
if groups == 0:
print("There are no groups.")
exit()
return groups
def group_exists(self, group_name):
"""
Checks if a group with that name was already created.
"""
if group_name in self.get_groups():
return True
else:
return False
def search_chat(self, name):
"""
Search for a chat with the name given and returns the element.
Elements cannot be accessed if they are not loaded. Though, the
script will scroll up to last element be the same as the last scrolled
last element.
"""
self.mouse.mouse_move((30, 400))
# Moves mouse to list of chats to scroll
iters = self.get_number_of_chats()
for i in range(iters):
elements = self.driver.get_elements(self.chat_element)
element = self.is_element('title', name, elements)
if element:
return element
self.mouse.scroll(1, True) # 1% of height
sleep(1)
# Scrolling in Selenium is not working as it must.
# So, it preferable to use another tool.
return None
def select_chat(self, chat_element):
"""
Clicks on the chat element to select it.
"""
self.driver.click_elements(chat_element)
def write_on_chat(self, content):
"""
Writes on a selected chat.
"""
footer = self.driver.get_elements("footer")
text_input = self.driver.get_elements("._1awRl", footer)
self.driver.send_keys(text_input, [content])
self.driver.send_keys(text_input, ["enter"])
def send_to_group(self, group, message):
"""
Receives a list of contacts, named as groups, and a message.
Sends it to each member of the group.
"""
for member in group:
self.send_message(member, message)
def send_message(self, contact, message):
"""
Sends a message to a particular contact.
"""
chat = self.search_chat(contact)
self.select_chat(chat)
self.write_on_chat(message)
def create_a_group(self, name, contacts_list, contacts_index):
"""
Receives a name, a contact list, and another list that indicates
which of the contacts of contact list will be included in the group.
Then creates a file, where stores the members of the group.
"""
new_contact = list()
for index in range(len(contacts_list)):
if str(index) in contacts_index:
new_contact.append(contacts_list[index])
with open("{}/{}".format(self.files_path, name), "w") as file:
file.write(",".join(new_contact))
return "{}/{}".format(self.files_path, name)
def create_group_routine(self):
"""
Routine to create a new group.
"""
print("Insert group name: ")
group_name = input()
group_name += ".wmg"
# Adds extension to filename
if self.group_exists(group_name):
print("Group already exists. Do you want to overwrite it: ")
response = input()
if response.lower() in ['y', 'yes']:
print("Overwrote.")
else:
return False
print("Contacts will be numerically list.")
print("Write comma separated indexes of contacts to include.")
contacts = self.get_contacts()
for contact_index in range(len(contacts)):
print("{}. {}".format(contact_index, contacts[contact_index]))
print("Contact list: ")
contact_list = input()
self.create_a_group(group_name, contacts, contact_list.split(","))
return True
def remove_group_routine(self):
"""
Shows routine to remove a group.
"""
groups = self.get_groups()
for group_index in range(len(groups)):
print("{}. {}".format(group_index, groups[group_index]))
print("Select a group: ")
group_number = input()
print("Remove group {}?".format(groups[int(group_number)]))
response = input()
if response.lower() in ['y', 'yes']:
remove(groups[int(group_number)])
print("Group removed.")
def edit_group_routine(self):
"""
Shows routine to edit a group.
"""
groups = self.get_groups()
for group_index in range(len(groups)):
print("{}. {}".format(group_index, groups[group_index]))
print("Select a group: ")
group_number = input()
print("Contacts will be numerically list.")
print("Write comma separated indexes of contacts to include.")
contacts = self.get_contacts()
for contact_index in range(len(contacts)):
print("{}. {}".format(contact_index, contacts[contact_index]))
print("Contact list: ")
contact_list = input()
remove(groups[int(group_number)])
group_name = groups[int(group_number)].split('/')[-1]
self.create_a_group(group_name, contacts, contact_list.split(","))
def manage_groups(self, option):
"""
Interface to add/edit/remove groups.
"""
if option == "1":
print("Create a group.")
self.create_group_routine()
elif option == "2":
print("Remove a group.")
self.remove_group_routine()
elif option == "3":
print("Edit a group")
self.edit_group_routine()
else:
print("Incorrect option.")
exit()
def close(self):
"""
Closes driver.
"""
self.driver.quit()
def arg_parsing(parser):
"""
Function to parse arguments from CLI.
"""
parser.add_argument("-v", "--version", help="show program version", action="store_true")
parser.add_argument("-l", "--list", help="list_contacts", action="store_true")
parser.add_argument("-s", "--send", help="send a message to a contact")
parser.add_argument("-m", "--message", help="takes the message")
parser.add_argument("-g", "--group", help="manage groups")
parser.add_argument("-c", "--chromedriver", help="sets default location of Chromedriver")
def take_action(parser, whatsmatic):
"""
Decides what to do with the args given.
"""
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
# No argument given
elif args.version:
print("WhatsMatic version 1.0.0")
elif args.list and args.group:
print("\n".join(whatsmatic.get_groups()))
elif args.send and not args.message:
print("ERROR: Missing message parameter.")
elif args.message and not args.send and not args.group:
print("ERROR: Missing send parameter.")
elif args.chromedriver:
with open(whatsmatic.files_path + "/chromedriver.txt", "w") as file:
file.write(args.chromedriver)
else:
# All functions that need WhatsMatic to open WhatsApp
whatsmatic.init()
if args.list:
print("\n".join(whatsmatic.get_contacts()))
elif args.send and args.message:
whatsmatic.send_message(args.send, args.message)
elif args.group:
if args.message:
whatsmatic.send_to_group(args.group, args.message)
else:
whatsmatic.manage_groups(args.group)
whatsmatic.close()
def main():
HOME = expanduser('~')
whatsmatic = WhatsMatic(HOME)
parser = argparse.ArgumentParser()
arg_parsing(parser)
take_action(parser, whatsmatic)
if __name__ == "__main__":
main()