Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 881296c

Browse files
authored
Merge pull request #92 from esirK/driver_install
Automatically download chrome driver.
2 parents b278d45 + d08d886 commit 881296c

6 files changed

Lines changed: 135 additions & 17 deletions

File tree

deletefb/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class UnknownOSException(Exception):
2+
pass

deletefb/tools/chrome_driver.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import re
2+
import zipfile
3+
import os, sys, stat, platform
4+
from urllib.request import urlretrieve
5+
from collections import namedtuple
6+
7+
from clint.textui import puts, colored
8+
import progressbar
9+
10+
from selenium import webdriver
11+
12+
from .common import NO_CHROME_DRIVER
13+
from ..exceptions import UnknownOSException
14+
15+
16+
_ = namedtuple('WebDrivers', 'mac linux windows')
17+
drivers = ['https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_mac64.zip',
18+
'https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip',
19+
'https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_win32.zip'
20+
]
21+
WebDriver = _(drivers[0], drivers[1], drivers[2])
22+
23+
24+
def extract_zip(filename):
25+
"""
26+
Uses zipfile package to extract a single zipfile
27+
:param filename:
28+
:return: new filename
29+
"""
30+
try:
31+
_file = zipfile.ZipFile(filename, 'r')
32+
except FileNotFoundError:
33+
puts(colored.red(f"{filename} Does not exist"))
34+
sys.exit(1)
35+
36+
# Save the name of the new file
37+
new_file_name = _file.namelist()[0]
38+
39+
# Extract the file and make it executable
40+
_file.extractall()
41+
42+
driver_stat = os.stat(new_file_name)
43+
os.chmod(new_file_name, driver_stat.st_mode | stat.S_IEXEC)
44+
45+
_file.close()
46+
os.remove(filename)
47+
return new_file_name
48+
49+
50+
def setup_selenium(driver_path, options):
51+
# Configures selenium to use a custom path
52+
return webdriver.Chrome(executable_path=driver_path, options=options)
53+
54+
55+
def get_webdriver():
56+
"""
57+
Ensure a webdriver is available
58+
If Not, Download it.
59+
"""
60+
cwd = os.listdir(os.getcwd())
61+
webdriver_regex = re.compile('chromedriver')
62+
web_driver = list(filter(webdriver_regex.match, cwd))
63+
64+
if web_driver:
65+
# check if a extracted copy already exists
66+
if not os.path.isfile('chromedriver'):
67+
# Extract file
68+
extract_zip(web_driver[0])
69+
70+
return "{0}/chromedriver".format(os.getcwd())
71+
72+
else:
73+
# Download it according to the current machine
74+
75+
os_platform = platform.system()
76+
if os_platform == 'Darwin':
77+
chrome_webdriver = WebDriver.mac
78+
elif os_platform == 'Linux':
79+
chrome_webdriver = WebDriver.linux
80+
elif os_platform == 'Windows':
81+
chrome_webdriver = WebDriver.windows
82+
else:
83+
raise UnknownOSException("Unknown Operating system platform")
84+
85+
global total_size
86+
87+
def show_progress(*res):
88+
global total_size
89+
pbar = None
90+
downloaded = 0
91+
block_num, block_size, total_size = res
92+
93+
if not pbar:
94+
pbar = progressbar.ProgressBar(maxval=total_size)
95+
pbar.start()
96+
downloaded += block_num * block_size
97+
98+
if downloaded < total_size:
99+
pbar.update(downloaded)
100+
else:
101+
pbar.finish()
102+
103+
puts(colored.yellow("Downloading Chrome Webdriver"))
104+
file_name = chrome_webdriver.split('/')[-1]
105+
response = urlretrieve(chrome_webdriver, file_name, show_progress)
106+
107+
if int(response[1].get('Content-Length')) == total_size:
108+
puts(colored.green(f"DONE!"))
109+
110+
return "{0}/{1}".format(os.getcwd(), extract_zip(file_name))
111+
112+
else:
113+
puts(colored.red("An error Occurred While trying to download the driver."))
114+
# remove the downloaded file and exit
115+
os.remove(file_name)
116+
sys.stderr.write(NO_CHROME_DRIVER)
117+
sys.exit(1)

deletefb/tools/common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
from selenium.common.exceptions import (
66
NoSuchElementException,
77
StaleElementReferenceException,
8-
TimeoutException,
9-
JavascriptException
8+
TimeoutException
109
)
1110

1211
import json
1312
import logging
1413
import logging.config
1514
import os
16-
import pendulum
1715

1816
SELENIUM_EXCEPTIONS = (
1917
NoSuchElementException,
2018
StaleElementReferenceException,
2119
TimeoutException
2220
)
2321

22+
2423
def click_button(driver, el):
2524
"""
2625
Click a button using Javascript
2726
"""
2827
driver.execute_script("arguments[0].click();", el)
2928

29+
3030
def scroll_to(driver, el):
3131
"""
3232
Scroll an element into view, using JS
@@ -36,6 +36,7 @@ def scroll_to(driver, el):
3636
except SELENIUM_EXCEPTIONS:
3737
return
3838

39+
3940
def logger(name):
4041
"""
4142
Args:
@@ -66,7 +67,8 @@ def wait_xpath(driver, expr):
6667
except SELENIUM_EXCEPTIONS:
6768
return
6869

70+
6971
NO_CHROME_DRIVER = """
70-
You need to install the chromedriver for Selenium\n
72+
You need to manually install the chromedriver for Selenium\n
7173
Please see this link https://github.com/weskerfoot/DeleteFB#how-to-use-it\n
7274
"""

deletefb/tools/login.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from .common import NO_CHROME_DRIVER
21
from selenium.common.exceptions import NoSuchElementException
32
from selenium.webdriver.chrome.options import Options
4-
from seleniumrequests import Chrome
53

6-
import sys
74
import time
85

6+
from .chrome_driver import get_webdriver, setup_selenium
7+
8+
99
def login(user_email_address,
1010
user_password,
1111
is_headless,
@@ -37,15 +37,8 @@ def login(user_email_address,
3737
chrome_options.add_argument('--no-sandbox')
3838
chrome_options.add_argument('log-level=2')
3939

40-
try:
41-
driver = Chrome(options=chrome_options)
42-
except Exception as e:
43-
# The user does not have chromedriver installed
44-
# Tell them to install it
45-
sys.stderr.write(str(e))
46-
sys.stderr.write(NO_CHROME_DRIVER)
47-
sys.exit(1)
48-
40+
driver_path = get_webdriver()
41+
driver = setup_selenium(driver_path, chrome_options)
4942
driver.implicitly_wait(10)
5043

5144
driver.get("https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ bleach==3.1.0
44
cattrs==0.9.0
55
certifi==2018.11.29
66
chardet==3.0.4
7+
clint==0.5.1
78
docutils==0.14
89
idna==2.8
910
lxml==4.4.0
1011
pendulum==2.0.5
1112
pkginfo==1.5.0.1
13+
progressbar==2.5
1214
pybloom-live==3.0.0
1315
Pygments==2.4.2
1416
python-dateutil==2.8.0

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"attrs",
2828
"cattrs",
2929
"lxml",
30-
"pendulum"
30+
"pendulum",
31+
"clint",
32+
"progressbar"
3133
],
3234
classifiers= [
3335
"Programming Language :: Python :: 3",

0 commit comments

Comments
 (0)