-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
91 lines (75 loc) · 3.21 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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from os import path
from os import getcwd
from getpass import getuser
from group import group
import time
import sys
import argparse
import signal
stop = False
def signal_handler(sig, frame):
print('Exiting nicely!')
global stop
stop = True
chrome.quit()
exit()
#Uses the search bar to find and open chat
def open_chat(name):
search_field = chrome.find_element_by_xpath('//*[@id="side"]/div[1]/div/label/div/div[2]')
search_field.send_keys()
chat = chrome.find_element_by_xpath('//span[@title="{}"]'.format(name))
chat.click()
def read_latest_msg(group_name):
msg_in = chrome.find_elements(By.CLASS_NAME,"message-in")[-1]
msg_span = msg_in.find_element(By.CLASS_NAME,"eRacY")
print(msg_span.text)
def read_msgs(group_name):
open_chat(group_name)
msgs_in = chrome.find_elements(By.CLASS_NAME,"message-in")
for msg in msgs_in:
msg_span = msg.find_element(By.CLASS_NAME,"eRacY")
print(msg_span.text)
# opens chat and sends message
def send_msg(message,group_name):
open_chat(group_name)
type_field = chrome.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
type_field.send_keys(message)
send_button = chrome.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button')
send_button.click()
def setup_args_parser(parser):
parser.add_argument("name",type=str,help="The name of the group chat the bot will run on")
parser.add_argument("-c","--cache",action="store_true",help = "Use browser cache to stop having to scan QR code more than once")
parser.add_argument("-s","--system",type=str,choices=["windows","mac","linux"],default="linux",help = "What OS is being used")
return parser
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
arg_parser = argparse.ArgumentParser()
arg_parser = setup_args_parser(arg_parser)
args = arg_parser.parse_args()
# construct paths
user = getuser()
local_path = path.abspath(getcwd())
driver_path = local_path+"/chromedriver" if args.system!="windows" else local_path+"\chromedriver.exe"
chrome_profiles = {"linux":'/home/{}/.config/google-chrome/default'.format(user),
"mac":'/Users/{}/Library/Application Support/Google/Chrome/Default'.format(user),
"windows":'C:\\Users\\{}\\AppData\\Local\\Google\\Chrome\\User Data\\Default'.format(user)}
# Set options and use cache of the chrome browser
options = webdriver.ChromeOptions()
if args.cache:
options.add_argument('--user-data-dir='+chrome_profiles[args.system])
options.add_argument('--profile-directory=Default')
# open chrome and whatsapp
chrome = webdriver.Chrome(driver_path,options=options)
chrome.get("https://web.whatsapp.com/")
print("press ENTER once whatsapp has loaded.")
input("")
group_name = args.name
open_chat(group_name)
group = group(group_name,chrome)
print(group.name,group.birth,group.desc,group.size)
while not stop:
group.read_latest_msg(chrome)