Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #14 from aahnik/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
aahnik authored Sep 9, 2020
2 parents 4ce8b04 + a7ec5fe commit 126065f
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ These tests __could not be automatically run__ on server via Travis CI or GitHub

__I run the tests on a regular basis and manually update the feature badges__ at the top of this README, to indicate whether that particular feature is successfully working or not.

If you find that any of the features is not working as expected, _feel free to create an issue_. Nonetheless, you could easily *clone this repo* and **run the tests locally** after configuring the variables in `testConfig.yml` file inside `tests` directory. To learn how to run tests [click here]().
If you find that any of the features is not working as expected, _feel free to create an issue_. Nonetheless, you could easily *clone this repo* and **run the tests locally** after configuring the variables in `testConfig.yml` file inside `test_wappdriver` directory. To learn how to run tests [click here]().



Expand Down
8 changes: 7 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
autopep8==1.5.4
certifi==2020.6.20
chardet==3.0.4
idna==2.10
pycodestyle==2.6.0
PyYAML==5.3.1
requests==2.24.0
selenium==3.141.0
toml==0.10.1
urllib3==1.25.10
requests==2.24.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="wappdriver",
version="0.2.6",
version="0.2.7",
license='MIT',
author="Aahnik Daw",
author_email="[email protected]",
Expand Down
37 changes: 37 additions & 0 deletions test_wappdriver/testConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PLease read the Documentation regarding testing, before attempting to test
# this is a yaml file, so no need to enclose strings by double/single quotes

# please make sure to change the values of the parameters before testing

absolute_WhatsApp_Session_Folder_Path:
# if not logged in, QR code will be displayed


saved_contact: aahnik
# case sensitive


unsaved_contact: fakeNobody
# name that is not saved
# this is required for checking whether the program successfully raises
# wappdriver error for UnknownContact


# note:
# the media at the paths you provide below, will be send to the saved_contact specified above


absolute_ImagePath: path/to/
# the absolute path to any image on your computer


absolute_VideoPath: path/to/
# the absolute path to any Video on your computer


absolute_GIFPath: path/to/
# the absolute path to any GIF on your computer


absolute_PDFPath: path/to/
# the absolute path to any PDF on your computer
3 changes: 2 additions & 1 deletion wappdriver/data/chrome_driver_path.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ this file is used to store package data
Chrome Driver Path will be saved here after user species path for the first time

The value can be modified by making a function call
TODO: fill instructions

Read Documentation for more details
4 changes: 3 additions & 1 deletion wappdriver/data/var.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
TODO: fill instructions
Don't tamper this file.
Some dynamic variables fetched from the internet will be stored here.
Read Documentation for more details
4 changes: 3 additions & 1 deletion wappdriver/data/varVer.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
0
TODO: fill instructions
The first line must be zero initially
Dont tamper.
To be used by program only
119 changes: 87 additions & 32 deletions wappdriver/driver.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
'''This module driver.py contains the WappDriver class, which is responsible for driving the core
features of the application.
You have to create an instance of the WappDriver class, to do any meaningful activity such as
sending a text message or media(Image/GIF/Video) or PDF document
'''

from .util import first_time_set_up, convey
from . import update as up
from .update import chrome_driver_path, local_varVer_val, var, update_cdp

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as expCond
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys

import yaml


class WappDriver():
''' The WappDriver class serves as an unofficial API to WhatsApp.
It interacts with the webdriver to send messages
'''

def __init__(self, session='wappDefaultSession', timeout=100):

if up.local_varVer_val == 0.0:
first_time_set_up(up)
if local_varVer_val == 0.0:
first_time_set_up()

self.chrome_driver_path = open(up.chrome_driver_path).readline()
with open(up.var) as file:
self.chrome_driver_path = open(chrome_driver_path).readline()
with open(var) as file:
_var = yaml.full_load(file)

self.whatsapp_web_url = _var['whatsapp_web_url']
self.mainScreenLOaded = _var['mainScreenLOaded']
self.mainScreenLoaded = _var['mainScreenLoaded']
self.searchSelector = _var['searchSelector']
self.mBox = _var['mBox']

Expand All @@ -40,73 +49,119 @@ def __init__(self, session='wappDefaultSession', timeout=100):
self.driver.quit()

def load_chrome_driver(self, session, tried=0):
while tried <= 3:
try:
chrome_options = Options()
chrome_options.add_argument(f'--user-data-dir={session}')
self.driver = webdriver.Chrome(
options=chrome_options, executable_path=self.chrome_driver_path)
return True
except Exception as error:
tried += 1
message = f"""Chrome Driver could not be successfuly loaded
Make sure that you have latest and matching versions of Chrome and Chrome Driver

try:
chrome_options = Options()
chrome_options.add_argument(f'--user-data-dir={session}')
self.driver = webdriver.Chrome(
options=chrome_options, executable_path=self.chrome_driver_path)
return True

except Exception as error:
message = f'''Chrome Driver could not be successfuly loaded
Make sure that you have latest and matching versions of Chrome and Chrome Driver
CHROME DRIVER INSTALLATION PATH IS INVALID !!
"""
convey(error, message)
up.update_cdp()
'''
convey(error, message)
update_cdp()

return False

def load_main_screen(self):
try:
self.driver.get(self.whatsapp_web_url)
WebDriverWait(self.driver, self.timeout).until(
expCond.presence_of_element_located((By.CSS_SELECTOR, self.mainScreenLOaded)))
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, self.mainScreenLoaded)))
return True

except Exception as error:
message = "Could not load main screen of WhatsApp Web because of some errors, make sure to Scan QR"
message = 'Could not load main screen of WhatsApp Web because of some errors, make sure to Scan QR'
convey(error, message)

return False

# selecting a person after searching contacts
def load_selected_person(self, name):

def load_person(self, name):
search_box = self.driver.find_element_by_css_selector(
self.searchSelector)

# we will send the name to the input key box
search_box.send_keys(name)

try:
person = WebDriverWait(self.driver, self.timeout).until(expCond.presence_of_element_located(
person = WebDriverWait(self.driver, self.timeout).until(expected_conditions.presence_of_element_located(
(By.XPATH, f'//*[@title="{name}"]')))
person.click()
return True

except Exception as error:
message = f"""{name} not loaded, MAY BE NOT IN YOUR CONTACTS ,
message = f'''{name} not loaded, MAY BE NOT IN YOUR CONTACTS ,
If you are sure {name} is in your contacts, Try checking internet connection
OR May be some other problem ... """
OR May be some other problem ... '''

convey(error, message)

search_box.send_keys((Keys.BACKSPACE)*len(name))
# clearing the search bar by backspace, so that searching the next person does'nt have any issue
return False

def send_message(self, to, msg=''):
def send_message(self, to, msg):
'''Method to send a text message to a contact.
Requires two arguments: to and msg
if self.load_selected_person(to):
Example Use :
bot.send_message(to='contact',msg='hi')
or
bot.send_message('contact','hi')
where bot is an object of WappDriver class
'''

if self.load_person(to):
msg_box = WebDriverWait(self.driver, self.timeout).until(
expCond.presence_of_element_located((By.XPATH, self.mBox)))
expected_conditions.presence_of_element_located((By.XPATH, self.mBox)))
lines = msg.split('\n')

for line in lines:
msg_box.send_keys(line) # write a line
msg_box.send_keys(Keys.SHIFT + Keys.ENTER) # go to next line

msg_box.send_keys(Keys.ENTER) # send message

def send_media(self, to, path, caption=None):
'''Method to send a media object to a contact.
Supports: Image, GIF, Video
Requires two arguments: to and path
Optional argument: caption
Example Use :
bot.send_media(to='contact',path='path/to/media',caption='wow')
or
bot.send_media('contact','path/to/media','wow')
where bot is an object of WappDriver class
Not giving the caption is allowed
'''

if self.load_person(to):
pass

def send_file(self,to,path):
'''Method to send any kind of file to a contact.
Supports: ALl formats
Requires two arguments: to and path
Example Use :
bot.send_file(to='contact',path='path/to/file')
or
bot.send_file('contact','path/to/file')
where bot is an object of WappDriver class
Not giving the caption is allowed
'''

def send_contact(self,to,contact):
pass

def send_url(self,to,url):
pass

6 changes: 3 additions & 3 deletions wappdriver/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def update_cdp():
# cdp stands for chrome_driver_path
path = input("Paste the absoulte path of Chrome Driver: \n")
path = input('Paste the absoulte path of Chrome Driver: \n')
with open(chrome_driver_path, 'w') as f:
f.write(path)

Expand All @@ -32,6 +32,6 @@ def fetch_vars():
if latest_varVer > local_varVer_val:
open(var, 'w').write(requests.get(url=remote_var).text)
open(varVer, 'w').write(str(latest_varVer))
return("updated sucessfully")
return('updated sucessfully')
else:
return("already the latest version")
return('already the latest version')
20 changes: 11 additions & 9 deletions wappdriver/util.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
def first_time_set_up(up):
print("\n Thank You for using wapp-driver. Welcome !! ")
from . import update
from pyfiglet import Figlet

def first_time_set_up():
f = Figlet(font='big')
print(f.renderText('wapp'))
print(f.renderText('driver'))

print(
"\n You have to enter the Chrome Driver Path once: only for the first time")
up.update_cdp()
update.update_cdp()
print("Sucessfully Saved")
up.fetch_vars()
print("Latest values of Dynamic Variables fetched from internet !\n")
print("If you want to change Chrome Driver Path or update Dynamic Variables")
print(" then please visit aahnik.github.io/wappdriver ")

update.fetch_vars()


def convey(error, message):
print(f"\n {message} \n")
print(f'\n{error}\n')
print("\n For help visit aahnik.github.io/wappdriver ")

0 comments on commit 126065f

Please sign in to comment.