forked from sbshah97/What-omation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wa.py
executable file
·150 lines (134 loc) · 3.55 KB
/
wa.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
#Import necessary libraries
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import threading
import os
import subprocess
d = webdriver.Chrome('/home/salman/All-Projects/WhatsApp-Web/chromedriver') # Optional argument, if not specified will search path.
d.get('https://web.whatsapp.com');
d.implicitly_wait(10)
d.get("https://web.whatsapp.com")
_B = '\033[1m'
B_ = '\033[0m'
ON = u"\u2022"
last_seen = 0
currentChat = ""
currentChatState = 0
def get_time():
return int(round(time.time()))
last_seen = get_time()
def select_chat(Chat):
global currentChat
global currentChatState
if Chat.lower() in currentChat.lower():
return
try:
inp = d.find_elements_by_class_name('input')
inp[0].send_keys(' ')
d.find_element_by_class_name('chat-body').click()
inp[0].send_keys(Chat)
time.sleep(1)
chat = d.find_elements_by_class_name('infinite-list-item')
chatName = chat[0].find_element_by_class_name('emojitext').get_attribute('title')
print(chatName)
if Chat.lower() in chatName.lower():
currentChat=chatName
chat[0].click()
chat_state = d.find_elements_by_class_name('pane-header')[1]
state = chat_state.find_elements_by_class_name('emojitext')[1].get_attribute('title')
currentChatState = (state=="online")
print("Chat "+currentChat+" selected")
else:
print("Failed to select Chat "+ Chat)
except:
print("Failed to select Chat "+ Chat)
def send_message(msg):
try:
inp = d.find_elements_by_class_name('input')
inp[1].send_keys(msg)
d.find_element_by_class_name('compose-btn-send').click()
except:
print("Failed to send message")
def send_message_to(chat, msg):
try:
select_chat(chat)
send_message(msg)
except:
print("Failed to send message")
def get_unread():
global last_seen
script = open("js/get_unread.js", "r").read()
script = "var last_seen = "+str(last_seen)+";\n"+script
script = "var currentChat = \""+currentChat+"\";\n"+script
last_seen = get_time()
chats = d.execute_script(script)
return chats
def get_read():
global last_seen
script = open("js/get_read.js", "r").read()
chats = d.execute_script(script)
return chats
def print_unread():
try:
chats = get_unread()
s = ""
i=0
for unread in chats:
chat = unread['name']
for msg in unread['messages']:
s += _B+chat.encode('utf-8')+" : "+B_
s += msg['msg'].encode('utf-8')+"\n"
i += 1
r = str(i)+" messages"
if i>0:
r += " from "+str(len(chats))+" chats"
print("\n" + r + "\n")
if len(s):
print(s)
except:
print("Failed to get unread messages")
def write2file():
try:
chats = get_read()
i=0
for unread in chats:
s = ""
chat = unread['name']
f = open("chats/"+ chat.encode('utf-8'), "w");
for msg in unread['messages']:
s += msg['msg'].encode('utf-8')+"\n"
i += 1
if len(s):
f.write(s)
f.close()
except:
print("Failed to get messages")
query = ""
while query!="quit":
if len(currentChat)>0:
if currentChatState:
query = raw_input(ON.encode('utf=8')+' '+currentChat.encode('utf-8')+" # ")
else:
query = raw_input(currentChat.encode('utf-8')+" # ")
else:
query = raw_input("# ")
query = query.strip()
q = query[0:3].strip()
s = query[3:].strip()
if q=="sc":
chat = s
select_chat(chat)
elif q=="sm":
msg = s
send_message(msg)
elif q=="um":
print_unread()
elif q=="gc":
write2file()
print("All chats are now accessible ...")
subprocess.Popen(["./read.sh"]);
elif query!="quit" and len(query)>0:
print("Invalid input")
d.quit()