Skip to content

Instantly share code, notes, and snippets.

@VISWESWARAN1998
Created August 30, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VISWESWARAN1998/5417b8a3b524fe9ce6dd4785d6ae0e9f to your computer and use it in GitHub Desktop.
Save VISWESWARAN1998/5417b8a3b524fe9ce6dd4785d6ae0e9f to your computer and use it in GitHub Desktop.
There is no official WhatsApp API. Here is a simple python class which satisfies the need.
{
":laughing:" : ":-d",
":neutral_face:" : ":-|",
"stuck_out_tongue:" : ":-p",
":heart:" : "<3",
":simple_smile:" : ":-)",
":worried:" : ":-(",
":-1:" : "(n)",
":thumbsup:" : "(y)",
":wink:" : ";-)"
}
# SWAMI KARUPPASWAMI THUNNAI
#============================================================
# Simple yet Hackable! WhatsApp API [UNOFFICIAL] for Python3
# Note: The author gives permission to use it under Apache2.0
#============================================================
import time
import datetime as dt
import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
class WhatsApp:
"""
This class is used to interact with your whatsapp [UNOFFICIAL API]
"""
emoji = {} # This dict will contain all emojies needed for chatting
browser = webdriver.Chrome()
# This constructor will load all the emojies present in the json file and it will initialize the webdriver
def __init__(self,wait,screenshot=None):
self.browser.get("https://web.whatsapp.com/")
# emoji.json is a json file which contains all the emojis
with open ("emoji.json") as emojies:
self.emoji = json.load(emojies) # This will load the emojies present in the json file into the dict
time.sleep(wait)
if screenshot != None:
self.browser.save_screenshot(screenshot) # This will save the screenshot to the specified file location
# This method is used to send the message to the individual person or a group
# will return true if the message has been sent, false else
def send_message(self,name,message):
message = self.emojify(message) # this will emojify all the emoji which is present as the text in string
search = self.browser.find_element_by_class_name("input-search")
search.send_keys(name+Keys.ENTER) # we will send the name to the input key box
current_time = time.time()
while True:
try:
send_msg = self.browser.find_element_by_class_name("input")
send_msg.send_keys(message+Keys.ENTER) # send the message
return True
except Exception as e:
print(e)
new_time = time.time()
elapsed_time = new_time - current_time
if elapsed_time > 30:
return False
# This method will count the no of participants for the group name provided
def participants_for_group(self,group_name):
search = self.browser.find_element_by_class_name("input-search")
search.send_keys(group_name+Keys.ENTER) # we will send the name to the input key box
current_time = time.time()
while True:
try:
click_menu = self.browser.find_element_by_css_selector("header.pane-header:nth-child(2) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)")
click_menu.click()
participants_css_selector = "div.animate-enter2:nth-child(4) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2)"
participants_count = self.browser.find_element_by_css_selector(participants_css_selector).text
if "256" in participants_count:
return participants_count
else:
time.sleep(1)
except Exception as e:
print(e)
new_time = time.time()
elapsed_time = new_time - current_time
if elapsed_time > 100:
return "NONE"
# get the status message of a person
# TimeOut is approximately set to 10 seconds
def get_status(self,name,timeout=10):
search = self.browser.find_element_by_class_name("input-search")
search.send_keys(name+Keys.ENTER) # we will send the name to the input key box
start_time = dt.datetime.now()
while True:
try:
click_menu = self.browser.find_element_by_css_selector("header.pane-header:nth-child(2) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)")
click_menu.click()
# This is the css selector for status
status_selector = "div.animate-enter2:nth-child(4) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(1)"
status = self.browser.find_element_by_css_selector(status_selector).text
# if the status if obtained the length must be greater than 0
if len(status) > 0:
return status
except Exception as e:
pass
end_time = dt.datetime.now()
elapsed_time = (end_time - start_time).seconds
if elapsed_time > timeout:
raise TimeoutError("The response has been TimedOut. You may change the time-out specifying in your args.")
# to get the last seen of the person
def get_last_seen(self,name,timeout=10):
search = self.browser.find_element_by_class_name("input-search")
search.send_keys(name+Keys.ENTER) # we will send the name to the input key box
start_time = dt.datetime.now()
last_seen_css_selector = ".chat-subtitle-text"
while True:
try:
last_seen = self.browser.find_element_by_css_selector(last_seen_css_selector).text
if len(last_seen) > 0:
if "click here" in last_seen:
pass
else:
return last_seen
except Exception as e:
pass
end_time = dt.datetime.now()
elapsed_time = (end_time - start_time).seconds
if elapsed_time > timeout:
raise TimeoutError("The response has been TimedOut. You may change the time-out specifying in your args.")
# This method is used to emojify all the text emoji's present in the message
def emojify(self,message):
for emoji in self.emoji:
message = message.replace(emoji,self.emoji[emoji])
return message
# This method is used to quit the browser
def quit(self):
self.browser.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment