Browse Source

cleanly shut down selenium when an exception happens

pull/120/head
Wesley Kerfoot 4 years ago
parent
commit
b564aa564b
  1. 37
      deletefb/deletefb.py
  2. 10
      deletefb/quit_driver.py
  3. 114
      deletefb/tools/login.py

37
deletefb/deletefb.py

@ -6,6 +6,7 @@ from .tools.login import login
from .tools.wall import delete_posts
from .tools.conversations import traverse_conversations
from .tools.comments import delete_comments
from .quit_driver import quit_driver_and_reap_children
import argparse
import getpass
@ -116,22 +117,26 @@ def run_delete():
chrome_binary_path=args.chromebin
)
if args.mode == "wall":
delete_posts(
driver,
args.profile_url,
year=args.year
)
elif args.mode == "unlike_pages":
unlike_pages(driver, args.profile_url)
elif args.mode == "conversations":
traverse_conversations(driver, year=args.year)
else:
print("Please enter a valid mode")
sys.exit(1)
try:
if args.mode == "wall":
delete_posts(
driver,
args.profile_url,
year=args.year
)
elif args.mode == "unlike_pages":
unlike_pages(driver, args.profile_url)
elif args.mode == "conversations":
traverse_conversations(driver, year=args.year)
else:
print("Please enter a valid mode")
sys.exit(1)
except:
if driver:
quit_driver_and_reap_children(driver)
if __name__ == "__main__":
run_delete()

10
deletefb/quit_driver.py

@ -0,0 +1,10 @@
import os
def quit_driver_and_reap_children(driver):
driver.quit()
try:
pid = True
while pid:
pid = os.waitpid(-1, os.WNOHANG)
except ChildProcessError:
pass

114
deletefb/tools/login.py

@ -1,6 +1,7 @@
from .chrome_driver import get_webdriver, setup_selenium
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from ..quit_driver import quit_driver_and_reap_children
import time
@ -42,59 +43,62 @@ def login(user_email_address,
driver_path = get_webdriver(chrome_binary_path)
driver = setup_selenium(driver_path, chrome_options)
driver.implicitly_wait(10)
driver.get("https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110")
email = "email"
password = "pass"
login_button = "loginbutton"
approvals_code = "approvals_code"
driver.find_element_by_name(email).send_keys(user_email_address)
driver.find_element_by_name(password).send_keys(user_password)
driver.find_element_by_id(login_button).click()
# Defaults to no 2fa
has_2fa = False
try:
# If this element exists, we've reached a 2FA page
driver.find_element_by_xpath("//form[@class=\"checkpoint\"]")
driver.find_element_by_xpath("//input[@name=\"approvals_code\"]")
has_2fa = True
except NoSuchElementException:
has_2fa = "two-factor authentication" in driver.page_source.lower() or has_2fa
if has_2fa:
print("""
Two-Factor Auth is enabled.
Please file an issue at https://github.com/weskerfoot/DeleteFB/issues if you run into any problems
""")
if two_factor_token and has_2fa:
twofactorelement = driver.find_element_by_name(approvals_code)
twofactorelement.send_keys(two_factor_token)
# Submits after the code is passed into the form, does not validate 2FA code.
contelement = driver.find_element_by_id("checkpointSubmitButton")
contelement.click()
# Defaults to saving this new browser, this occurs on each new automated login.
save_browser = driver.find_element_by_id("checkpointSubmitButton")
save_browser.click()
elif has_2fa:
# Allow time to enter 2FA code
print("Pausing to enter 2FA code")
time.sleep(35)
print("Continuing execution")
else:
pass
# block until we have reached the main page
# print a message warning the user
while driver.current_url != "https://www.facebook.com/":
print("Execution blocked: Please navigate to https://www.facebook.com to continue")
time.sleep(5)
return driver
driver.implicitly_wait(10)
driver.get("https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110")
email = "email"
password = "pass"
login_button = "loginbutton"
approvals_code = "approvals_code"
driver.find_element_by_name(email).send_keys(user_email_address)
driver.find_element_by_name(password).send_keys(user_password)
driver.find_element_by_id(login_button).click()
# Defaults to no 2fa
has_2fa = False
try:
# If this element exists, we've reached a 2FA page
driver.find_element_by_xpath("//form[@class=\"checkpoint\"]")
driver.find_element_by_xpath("//input[@name=\"approvals_code\"]")
has_2fa = True
except NoSuchElementException:
has_2fa = "two-factor authentication" in driver.page_source.lower() or has_2fa
if has_2fa:
print("""
Two-Factor Auth is enabled.
Please file an issue at https://github.com/weskerfoot/DeleteFB/issues if you run into any problems
""")
if two_factor_token and has_2fa:
twofactorelement = driver.find_element_by_name(approvals_code)
twofactorelement.send_keys(two_factor_token)
# Submits after the code is passed into the form, does not validate 2FA code.
contelement = driver.find_element_by_id("checkpointSubmitButton")
contelement.click()
# Defaults to saving this new browser, this occurs on each new automated login.
save_browser = driver.find_element_by_id("checkpointSubmitButton")
save_browser.click()
elif has_2fa:
# Allow time to enter 2FA code
print("Pausing to enter 2FA code")
time.sleep(35)
print("Continuing execution")
else:
pass
# block until we have reached the main page
# print a message warning the user
while driver.current_url != "https://www.facebook.com/":
print("Execution blocked: Please navigate to https://www.facebook.com to continue")
time.sleep(5)
return driver
except:
quit_driver_and_reap_children(driver)

Loading…
Cancel
Save