diff --git a/deletefb/tools/archive.py b/deletefb/tools/archive.py index 052a05a..1fca7c3 100644 --- a/deletefb/tools/archive.py +++ b/deletefb/tools/archive.py @@ -37,7 +37,9 @@ class Archive: """ Archive an object """ - print("Archiving content") + + if hasattr(content, 'name'): + print("Archiving {0}".format(content.name)) if content.name not in self._bloom_filter: self.archive_file.write(json.dumps(cattr.unstructure(content), diff --git a/deletefb/tools/common.py b/deletefb/tools/common.py index 18c74cd..de0d679 100644 --- a/deletefb/tools/common.py +++ b/deletefb/tools/common.py @@ -1,4 +1,7 @@ from os.path import isfile +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.common.by import By from selenium.common.exceptions import ( NoSuchElementException, StaleElementReferenceException, @@ -52,6 +55,17 @@ def logger(name): logging.config.dictConfig(config["logging"]) return logging.getLogger(name) + +def wait_xpath(driver, expr): + """ + Takes an XPath expression, and waits at most 20 seconds until it exists + """ + wait = WebDriverWait(driver, 20) + try: + wait.until(EC.presence_of_element_located((By.XPATH, expr))) + except SELENIUM_EXCEPTIONS: + return + NO_CHROME_DRIVER = """ You need to install the chromedriver for Selenium\n Please see this link https://github.com/weskerfoot/DeleteFB#how-to-use-it\n diff --git a/deletefb/tools/conversations.py b/deletefb/tools/conversations.py index 003924c..d94923d 100644 --- a/deletefb/tools/conversations.py +++ b/deletefb/tools/conversations.py @@ -1,12 +1,10 @@ from .archive import archiver from ..types import Conversation, Message -from .common import SELENIUM_EXCEPTIONS, logger, click_button -from selenium.webdriver.common.by import By -from selenium.webdriver.support import expected_conditions as EC -from selenium.webdriver.support.ui import WebDriverWait +from .common import SELENIUM_EXCEPTIONS, logger, click_button, wait_xpath from selenium.webdriver.common.action_chains import ActionChains from pendulum import now from json import loads +from time import sleep import lxml.html as lxh @@ -17,17 +15,7 @@ def get_conversations(driver): Get a list of conversations """ - actions = ActionChains(driver) - - wait = WebDriverWait(driver, 20) - - try: - wait.until( - EC.presence_of_element_located((By.XPATH, "//div[@id=\"threadlist_rows\"]")) - ) - except SELENIUM_EXCEPTIONS: - LOG.exception("No conversations") - return + wait_xpath(driver, "//div[@id=\"threadlist_rows\"]") # This function *cannot* be a generator # Otherwise elements will become stale @@ -98,14 +86,7 @@ def get_convo(driver, convo): """ driver.get(convo.url) - wait = WebDriverWait(driver, 20) - try: - wait.until( - EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'See Older Messages')]")) - ) - except SELENIUM_EXCEPTIONS: - LOG.exception("Could not load more messages") - return + wait_xpath(driver, "//*[contains(text(), 'See Older Messages')]") # Expand conversation until we've reached the beginning while True: @@ -131,6 +112,12 @@ def delete_conversation(driver, convo): Deletes a conversation """ + actions = ActionChains(driver) + + delete_button = driver.find_element_by_xpath("//select/option[contains(text(), 'Delete')]") + + actions.move_to_element(delete_button).click().perform() + return def extract_convo(driver, convo): @@ -150,8 +137,7 @@ def extract_convo(driver, convo): return convo - -def traverse_conversations(driver, year=None): +def traverse_conversations(driver, year=None, delete=False): """ Remove all conversations within a specified range """ @@ -169,9 +155,13 @@ def traverse_conversations(driver, year=None): if convo.date.year == int(year): extract_convo(driver, convo) archive_convo.archive(convo) + if delete: + delete_conversation(driver, convo) # Otherwise we're looking at all convos elif not year: extract_convo(driver, convo) archive_convo.archive(convo) + if delete: + delete_conversation(driver, convo)