-
Notifications
You must be signed in to change notification settings - Fork 0
/
arun.py
127 lines (110 loc) · 4.59 KB
/
arun.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
import art, time, os, platform
from colorama import init, Fore
from termcolor import colored
import pandas as pd
import sys
import datetime
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
FILE_LOC = "C:\\Users\HP\Desktop\data.xlsx"
SRNO = 'SRNO'
NAME = 'Name'
PHONENUMBER = 'Phone Number'
MESSAGE = 'Message'
IMAGE = 'Image'
REPLACENAME = '{{name}}'
URL = 'https://web.whatsapp.com/'
WAITER_ELEMENT = "landing-title _3-XoE"
PHONE_NUMER_INPUT = "//*[@id='side']/div[1]/div/label/div/div[2]"
PERSON_DIV = "//*[@id='pane-side']/div[1]/div/div/div[1]/div/div/div[1]/div/div/img"
MESSAGE_INPUT = "//*[@id='main']/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]"
ATTACMENT_INPUT = "webdriver.find_element('//div[@title='Attach']')"
IMAGE_INPUT = "webdriver.find_element('//input[@accept='image/*,video/mp4,video/3gpp,video/quicktime']')"
SEND_BUTTON = "//*[@id='main']/footer/div[1]/div/span[2]/div/div[2]/div[2]/button"
driver = webdriver.Chrome(executable_path='E:\chromedriver_win32 (1)\chromedriver.exe')
waiter = WebDriverWait(driver, 10)
data = []
Date = datetime.datetime(int(2022), int(1), 1)
def printData(message, type):
def printData(image, type):
if type == 'INFO':
print('[' + colored(datetime.datetime.now().strftime('%H:%M:%S'), 'cyan') + '][' + colored('INFO', 'green') + '] ' + message,image)
elif type == 'WARNING':
print('[' + colored(datetime.datetime.now().strftime('%H:%M:%S'), 'cyan') + '][' + colored('WARNING', 'yellow') + '] ' + message,image)
elif type == 'ERROR':
print('[' + colored(datetime.datetime.now().strftime('%H:%M:%S'), 'cyan') + '][' + colored('ERROR', 'red') + '] ' + message,image)
def read_data_from_excel():
try:
df = pd.DataFrame()
df = pd.read_excel(FILE_LOC)
printData("Retrieving data from excel", 'INFO')
except:
printData("Excel 'data.xlsx' not found", 'ERROR')
printData("Found {0} messages to be send".format(len(df.index)), 'INFO')
for i in df.index:
if '+' not in str(df[PHONENUMBER][i]):
number = '+91' + str(df[PHONENUMBER][i])
else:
number = str(df[PHONENUMBER][i])
output = {
'SrNo': df[SRNO][i],
'Name': df[NAME][i],
'PhoneNumber': number,
'Message': df[MESSAGE][i],
'Image': df[IMAGE][i],
}
data.append(output)
def send_whatsapp_message():
global driver
driver.get(URL)
printData("Loading site...", 'INFO')
waiter.until(EC.title_is("WhatsApp"))
printData("Site loaded successfully...", 'INFO')
printData("Waiting for user to log in using WhatsApp Web", 'INFO')
waitCounter = 0
while 1:
try:
waiter.until(EC.presence_of_element_located((By.XPATH, "//canvas[@aria-label='Scan me!']")))
waitCounter+=1
if waitCounter%1000 == 0:
printData("Waiting for user to log in...", 'WARNING')
except:
printData("Logged in to WhatsApp", 'INFO')
break
for entry in data:
driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(str(entry['PhoneNumber']))
time.sleep(5)
driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(Keys.ENTER)
time.sleep(5)
driver.find_element_by_xpath(MESSAGE_INPUT).send_keys(str(entry['Message']))
time.sleep(5)
driver.find_element_by_xpath(IMAGE_INPUT).send_keys(str['Image'])
time.sleep(5)
driver.find_element_by_xpath(IMAGE_INPUT).send_keys(Keys.ENTER)
time.sleep(5)
driver.find_element('//span[@title = "{}"]'.format(name))
time.sleep(5)
driver.find_element_by_xpath(SEND_BUTTON).click()
time.sleep(5)
printData("Successfully send message to {0}, name: {1}".format(str(entry['PhoneNumber']), str(entry['Name'])), 'INFO')
if __name__ == '__main__':
# Initialize colorama
init()
# Clear the screen
if platform.system() == 'Windows':
os.system('cls')
else:
os.system('clear')
# Display ASCII art
print(art.text2art("Boom Messenger"))
print(Fore.CYAN + "\nCreated By:" + Fore.RESET + " Arunkumar[All About Python]\n")
print(Fore.YELLOW + "GitHub: " + Fore.RESET + " Arunkumar")
# Read data from 'data.xlsx' file
read_data_from_excel()
# Send whatsapp message
send_whatsapp_message()
# Close chromedriver
driver.close()