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

Commit 126065f

Browse files
authored
Merge pull request #14 from aahnik/dev
Dev
2 parents 4ce8b04 + a7ec5fe commit 126065f

File tree

10 files changed

+155
-50
lines changed

10 files changed

+155
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ These tests __could not be automatically run__ on server via Travis CI or GitHub
7272

7373
__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.
7474

75-
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]().
75+
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]().
7676

7777

7878

requirements.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
autopep8==1.5.4
2+
certifi==2020.6.20
3+
chardet==3.0.4
4+
idna==2.10
5+
pycodestyle==2.6.0
16
PyYAML==5.3.1
7+
requests==2.24.0
28
selenium==3.141.0
9+
toml==0.10.1
310
urllib3==1.25.10
4-
requests==2.24.0

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="wappdriver",
8-
version="0.2.6",
8+
version="0.2.7",
99
license='MIT',
1010
author="Aahnik Daw",
1111
author_email="meet.aahnik@gmail.com",

test_wappdriver/testConfig.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# PLease read the Documentation regarding testing, before attempting to test
2+
# this is a yaml file, so no need to enclose strings by double/single quotes
3+
4+
# please make sure to change the values of the parameters before testing
5+
6+
absolute_WhatsApp_Session_Folder_Path:
7+
# if not logged in, QR code will be displayed
8+
9+
10+
saved_contact: aahnik
11+
# case sensitive
12+
13+
14+
unsaved_contact: fakeNobody
15+
# name that is not saved
16+
# this is required for checking whether the program successfully raises
17+
# wappdriver error for UnknownContact
18+
19+
20+
# note:
21+
# the media at the paths you provide below, will be send to the saved_contact specified above
22+
23+
24+
absolute_ImagePath: path/to/
25+
# the absolute path to any image on your computer
26+
27+
28+
absolute_VideoPath: path/to/
29+
# the absolute path to any Video on your computer
30+
31+
32+
absolute_GIFPath: path/to/
33+
# the absolute path to any GIF on your computer
34+
35+
36+
absolute_PDFPath: path/to/
37+
# the absolute path to any PDF on your computer

wappdriver/data/chrome_driver_path.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ this file is used to store package data
55
Chrome Driver Path will be saved here after user species path for the first time
66

77
The value can be modified by making a function call
8-
TODO: fill instructions
8+
9+
Read Documentation for more details

wappdriver/data/var.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
TODO: fill instructions
1+
Don't tamper this file.
2+
Some dynamic variables fetched from the internet will be stored here.
3+
Read Documentation for more details

wappdriver/data/varVer.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
0
2-
TODO: fill instructions
2+
The first line must be zero initially
3+
Dont tamper.
4+
To be used by program only

wappdriver/driver.py

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1+
'''This module driver.py contains the WappDriver class, which is responsible for driving the core
2+
features of the application.
3+
You have to create an instance of the WappDriver class, to do any meaningful activity such as
4+
sending a text message or media(Image/GIF/Video) or PDF document
5+
'''
6+
17
from .util import first_time_set_up, convey
2-
from . import update as up
8+
from .update import chrome_driver_path, local_varVer_val, var, update_cdp
39

410
from selenium import webdriver
511
from selenium.webdriver.chrome.options import Options
612
from selenium.webdriver.support.ui import WebDriverWait
713
from selenium.webdriver.common.by import By
8-
from selenium.webdriver.support import expected_conditions as expCond
14+
from selenium.webdriver.support import expected_conditions
915
from selenium.webdriver.common.keys import Keys
1016

1117
import yaml
1218

1319

1420
class WappDriver():
21+
''' The WappDriver class serves as an unofficial API to WhatsApp.
22+
It interacts with the webdriver to send messages
23+
'''
1524

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

18-
if up.local_varVer_val == 0.0:
19-
first_time_set_up(up)
27+
if local_varVer_val == 0.0:
28+
first_time_set_up()
2029

21-
self.chrome_driver_path = open(up.chrome_driver_path).readline()
22-
with open(up.var) as file:
30+
self.chrome_driver_path = open(chrome_driver_path).readline()
31+
with open(var) as file:
2332
_var = yaml.full_load(file)
2433

2534
self.whatsapp_web_url = _var['whatsapp_web_url']
26-
self.mainScreenLOaded = _var['mainScreenLOaded']
35+
self.mainScreenLoaded = _var['mainScreenLoaded']
2736
self.searchSelector = _var['searchSelector']
2837
self.mBox = _var['mBox']
2938

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

4251
def load_chrome_driver(self, session, tried=0):
43-
while tried <= 3:
44-
try:
45-
chrome_options = Options()
46-
chrome_options.add_argument(f'--user-data-dir={session}')
47-
self.driver = webdriver.Chrome(
48-
options=chrome_options, executable_path=self.chrome_driver_path)
49-
return True
50-
except Exception as error:
51-
tried += 1
52-
message = f"""Chrome Driver could not be successfuly loaded
53-
Make sure that you have latest and matching versions of Chrome and Chrome Driver
52+
53+
try:
54+
chrome_options = Options()
55+
chrome_options.add_argument(f'--user-data-dir={session}')
56+
self.driver = webdriver.Chrome(
57+
options=chrome_options, executable_path=self.chrome_driver_path)
58+
return True
59+
60+
except Exception as error:
61+
message = f'''Chrome Driver could not be successfuly loaded
62+
Make sure that you have latest and matching versions of Chrome and Chrome Driver
5463
CHROME DRIVER INSTALLATION PATH IS INVALID !!
55-
"""
56-
convey(error, message)
57-
up.update_cdp()
64+
'''
65+
convey(error, message)
66+
update_cdp()
5867

5968
return False
6069

6170
def load_main_screen(self):
6271
try:
6372
self.driver.get(self.whatsapp_web_url)
6473
WebDriverWait(self.driver, self.timeout).until(
65-
expCond.presence_of_element_located((By.CSS_SELECTOR, self.mainScreenLOaded)))
74+
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, self.mainScreenLoaded)))
6675
return True
76+
6777
except Exception as error:
68-
message = "Could not load main screen of WhatsApp Web because of some errors, make sure to Scan QR"
78+
message = 'Could not load main screen of WhatsApp Web because of some errors, make sure to Scan QR'
6979
convey(error, message)
70-
7180
return False
7281

7382
# selecting a person after searching contacts
74-
def load_selected_person(self, name):
75-
83+
def load_person(self, name):
7684
search_box = self.driver.find_element_by_css_selector(
7785
self.searchSelector)
7886

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

8290
try:
83-
person = WebDriverWait(self.driver, self.timeout).until(expCond.presence_of_element_located(
91+
person = WebDriverWait(self.driver, self.timeout).until(expected_conditions.presence_of_element_located(
8492
(By.XPATH, f'//*[@title="{name}"]')))
8593
person.click()
8694
return True
8795

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

94102
convey(error, message)
95103

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

100-
def send_message(self, to, msg=''):
108+
def send_message(self, to, msg):
109+
'''Method to send a text message to a contact.
110+
Requires two arguments: to and msg
101111
102-
if self.load_selected_person(to):
112+
Example Use :
113+
bot.send_message(to='contact',msg='hi')
114+
or
115+
bot.send_message('contact','hi')
116+
where bot is an object of WappDriver class
117+
'''
103118

119+
if self.load_person(to):
104120
msg_box = WebDriverWait(self.driver, self.timeout).until(
105-
expCond.presence_of_element_located((By.XPATH, self.mBox)))
121+
expected_conditions.presence_of_element_located((By.XPATH, self.mBox)))
106122
lines = msg.split('\n')
107123

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

112128
msg_box.send_keys(Keys.ENTER) # send message
129+
130+
def send_media(self, to, path, caption=None):
131+
'''Method to send a media object to a contact.
132+
Supports: Image, GIF, Video
133+
Requires two arguments: to and path
134+
Optional argument: caption
135+
136+
Example Use :
137+
bot.send_media(to='contact',path='path/to/media',caption='wow')
138+
or
139+
bot.send_media('contact','path/to/media','wow')
140+
where bot is an object of WappDriver class
141+
142+
Not giving the caption is allowed
143+
'''
144+
145+
if self.load_person(to):
146+
pass
147+
148+
def send_file(self,to,path):
149+
'''Method to send any kind of file to a contact.
150+
Supports: ALl formats
151+
Requires two arguments: to and path
152+
153+
Example Use :
154+
bot.send_file(to='contact',path='path/to/file')
155+
or
156+
bot.send_file('contact','path/to/file')
157+
where bot is an object of WappDriver class
158+
159+
Not giving the caption is allowed
160+
'''
161+
162+
def send_contact(self,to,contact):
163+
pass
164+
165+
def send_url(self,to,url):
166+
pass
167+

wappdriver/update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
def update_cdp():
2424
# cdp stands for chrome_driver_path
25-
path = input("Paste the absoulte path of Chrome Driver: \n")
25+
path = input('Paste the absoulte path of Chrome Driver: \n')
2626
with open(chrome_driver_path, 'w') as f:
2727
f.write(path)
2828

@@ -32,6 +32,6 @@ def fetch_vars():
3232
if latest_varVer > local_varVer_val:
3333
open(var, 'w').write(requests.get(url=remote_var).text)
3434
open(varVer, 'w').write(str(latest_varVer))
35-
return("updated sucessfully")
35+
return('updated sucessfully')
3636
else:
37-
return("already the latest version")
37+
return('already the latest version')

wappdriver/util.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
def first_time_set_up(up):
2-
print("\n Thank You for using wapp-driver. Welcome !! ")
1+
from . import update
2+
from pyfiglet import Figlet
3+
4+
def first_time_set_up():
5+
f = Figlet(font='big')
6+
print(f.renderText('wapp'))
7+
print(f.renderText('driver'))
8+
39
print(
410
"\n You have to enter the Chrome Driver Path once: only for the first time")
5-
up.update_cdp()
11+
update.update_cdp()
612
print("Sucessfully Saved")
7-
up.fetch_vars()
8-
print("Latest values of Dynamic Variables fetched from internet !\n")
9-
print("If you want to change Chrome Driver Path or update Dynamic Variables")
10-
print(" then please visit aahnik.github.io/wappdriver ")
11-
13+
update.fetch_vars()
14+
1215

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

0 commit comments

Comments
 (0)