Browse Source

Iterating through all conversations working!

pull/72/head
Wesley Kerfoot 6 years ago
parent
commit
65e6286234
  1. 59
      deletefb/tools/conversations.py

59
deletefb/tools/conversations.py

@ -1,60 +1,53 @@
from .archive import archiver from .archive import archiver
from ..types import Conversation from ..types import Conversation
from .common import SELENIUM_EXCEPTIONS, logger, scroll_to from .common import SELENIUM_EXCEPTIONS, logger, scroll_to, click_button
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
LOG = logger(__name__) LOG = logger(__name__)
def get_conversation_list(driver, offset=0): def get_conversations(driver):
""" """
Get a list of conversations Get a list of conversations
""" """
actions = ActionChains(driver) actions = ActionChains(driver)
convos = driver.find_elements_by_xpath("//ul[@aria-label=\"Conversation list\"]/li/div/a[@role=\"link\"]") wait = WebDriverWait(driver, 20)
for convo in convos[offset:]:
actions.move_to_element(convo).perform()
yield convo
actions.move_to_element(current_convo).perform()
def get_all_conversations(driver):
conversation_urls = set()
current_convo = None try:
wait.until(
EC.presence_of_element_located((By.XPATH, "//div[@id=\"threadlist_rows\"]"))
)
except SELENIUM_EXCEPTIONS:
LOG.exception("No conversations")
return
while True: while True:
l = len(conversation_urls) for convo in driver.find_elements_by_xpath("//a"):
url = convo.get_attribute("href")
for convo in get_conversation_list(driver, offset=l): if url and "messages/read" in url:
url = convo.get_attribute("data-href") yield url
conversation_urls.add(url)
current_convo = convo try:
next_url = driver.find_element_by_id("see_older_threads").find_element_by_xpath("a").get_attribute("href")
if current_convo: except SELENIUM_EXCEPTIONS:
scroll_to(driver, current_convo)
print(l)
print(len(conversation_urls))
if len(conversation_urls) == l:
# no more conversations left
break break
if not next_url:
return list(conversation_urls) break
driver.get(next_url)
def delete_conversations(driver): def delete_conversations(driver):
""" """
Remove all conversations within a specified range Remove all conversations within a specified range
""" """
driver.get("https://www.facebook.com/messages/t/") driver.get("https://mobile.facebook.com/messages/?pageNum=1&selectable&see_older_newer=1")
wait = WebDriverWait(driver, 20) convos = list(get_conversations(driver))
for convo_url in get_all_conversations(driver): for convo in convos:
print(convo_url) driver.get(convo)

Loading…
Cancel
Save