Browse Source

Gathering conversations

pull/72/head
Wesley Kerfoot 5 years ago
parent
commit
d17919f675
  1. 16
      deletefb/tools/common.py
  2. 40
      deletefb/tools/conversations.py

16
deletefb/tools/common.py

@ -2,7 +2,8 @@ from os.path import isfile
from selenium.common.exceptions import (
NoSuchElementException,
StaleElementReferenceException,
TimeoutException
TimeoutException,
JavascriptException
)
import json
@ -19,13 +20,18 @@ SELENIUM_EXCEPTIONS = (
def click_button(driver, el):
"""
Click a button using Javascript
Args:
driver: seleniumrequests.Chrome Driver instance
Returns:
None
"""
driver.execute_script("arguments[0].click();", el)
def scroll_to(driver, el):
"""
Scroll an element into view, using JS
"""
try:
driver.execute_script("arguments[0].scrollIntoView();", el)
except SELENIUM_EXCEPTIONS:
return
def logger(name):
"""
Args:

40
deletefb/tools/conversations.py

@ -1,6 +1,6 @@
from .archive import archiver
from ..types import Conversation
from .common import SELENIUM_EXCEPTIONS, logger, click_button
from .common import SELENIUM_EXCEPTIONS, logger, scroll_to
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
@ -8,18 +8,44 @@ from selenium.webdriver.common.action_chains import ActionChains
LOG = logger(__name__)
def get_conversation_list(driver):
def get_conversation_list(driver, offset=0):
"""
Get a list of conversations
"""
actions = ActionChains(driver)
convos = driver.find_elements_by_xpath("//ul[@aria-label=\"Conversation list\"]/li")
convos = driver.find_elements_by_xpath("//ul[@aria-label=\"Conversation list\"]/li/div/a[@role=\"link\"]")
for convo in convos:
for convo in convos[offset:]:
actions.move_to_element(convo).perform()
yield convo.find_element_by_xpath("//a")
yield convo
actions.move_to_element(current_convo).perform()
def get_all_conversations(driver):
conversation_urls = set()
current_convo = None
while True:
l = len(conversation_urls)
for convo in get_conversation_list(driver, offset=l):
url = convo.get_attribute("data-href")
conversation_urls.add(url)
current_convo = convo
if current_convo:
scroll_to(driver, current_convo)
print(l)
print(len(conversation_urls))
if len(conversation_urls) == l:
# no more conversations left
break
return list(conversation_urls)
def delete_conversations(driver):
"""
@ -30,5 +56,5 @@ def delete_conversations(driver):
wait = WebDriverWait(driver, 20)
for convo_url in get_conversation_list(driver):
print(convo_url.get_property("data-href"))
for convo_url in get_all_conversations(driver):
print(convo_url)

Loading…
Cancel
Save