Browse Source

fix unliking (#134)

pull/136/head
Wesley Kerfoot 4 years ago
committed by GitHub
parent
commit
50162ba996
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      deletefb/tools/common.py
  2. 42
      deletefb/tools/likes.py

17
deletefb/tools/common.py

@ -20,13 +20,20 @@ SELENIUM_EXCEPTIONS = (
TimeoutException TimeoutException
) )
def click_button(driver, el): def click_button(driver, el):
""" """
Click a button using Javascript Click a button using Javascript
""" """
driver.execute_script("arguments[0].click();", el) driver.execute_script("arguments[0].click();", el)
def scroll_until_element_exists(driver, xpath_expr):
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
try:
element = driver.find_element_by_xpath(xpath_expr)
except SELENIUM_EXCEPTIONS:
continue
break
def scroll_to(driver, el): def scroll_to(driver, el):
""" """
@ -37,7 +44,6 @@ def scroll_to(driver, el):
except SELENIUM_EXCEPTIONS: except SELENIUM_EXCEPTIONS:
return return
def logger(name): def logger(name):
""" """
Args: Args:
@ -57,12 +63,11 @@ def logger(name):
logging.config.dictConfig(config["logging"]) logging.config.dictConfig(config["logging"])
return logging.getLogger(name) return logging.getLogger(name)
def wait_xpath(driver, expr, timeout=20):
def wait_xpath(driver, expr):
""" """
Takes an XPath expression, and waits at most 20 seconds until it exists Takes an XPath expression, and waits at most `timeout` seconds until it exists
""" """
wait = WebDriverWait(driver, 20) wait = WebDriverWait(driver, timeout)
try: try:
wait.until(EC.presence_of_element_located((By.XPATH, expr))) wait.until(EC.presence_of_element_located((By.XPATH, expr)))
except SELENIUM_EXCEPTIONS: except SELENIUM_EXCEPTIONS:

42
deletefb/tools/likes.py

@ -1,6 +1,6 @@
from .archive import archiver from .archive import archiver
from ..types import Page from ..types import Page
from .common import SELENIUM_EXCEPTIONS, logger, click_button from .common import SELENIUM_EXCEPTIONS, logger, click_button, wait_xpath, force_mobile, scroll_to, scroll_until_element_exists
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
@ -18,17 +18,25 @@ def load_likes(driver, profile_url):
None None
""" """
driver.get("{0}/likes_all".format(profile_url)) likes_link_xpath = "//div[normalize-space(text())='Likes']/../..//a[contains(@href, 'app_section')]"
wait = WebDriverWait(driver, 20) all_likes_link_xpath = "//div[normalize-space(text())='All Likes']/../../../..//a[contains(@href, 'app_collection')]"
try: driver.get(force_mobile("{0}/about".format(profile_url)))
wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".PageLikeButton")) scroll_until_element_exists(driver, "//div[text()='Likes']")
)
except SELENIUM_EXCEPTIONS: likes_link_el = driver.find_element_by_xpath(likes_link_xpath)
LOG.exception("Traceback of load_likes")
return driver.get(likes_link_el.get_attribute("href"))
wait_xpath(driver, all_likes_link_xpath)
all_likes_link_el = driver.find_element_by_xpath(all_likes_link_xpath)
all_likes_link = all_likes_link_el.get_attribute("href")
driver.get(all_likes_link)
def get_page_links(driver): def get_page_links(driver):
""" """
@ -39,8 +47,7 @@ def get_page_links(driver):
Returns: Returns:
List of URLs to pages List of URLs to pages
""" """
pages = driver.find_elements_by_xpath("//li//div/div/a[contains(@class, 'lfloat')]") pages = driver.find_elements_by_xpath("//header/..//a")
return [page.get_attribute("href").replace("www", "mobile") for page in pages] return [page.get_attribute("href").replace("www", "mobile") for page in pages]
def unlike_page(driver, url, archive=None): def unlike_page(driver, url, archive=None):
@ -60,7 +67,7 @@ def unlike_page(driver, url, archive=None):
print("Unliking {0}".format(url)) print("Unliking {0}".format(url))
wait = WebDriverWait(driver, 20) wait = WebDriverWait(driver, 5)
try: try:
wait.until( wait.until(
@ -70,19 +77,14 @@ def unlike_page(driver, url, archive=None):
# Something went wrong with this page, so skip it # Something went wrong with this page, so skip it
return return
button = driver.find_element_by_xpath("//*[text()='Liked']") driver.find_element_by_xpath("//*[text()='Liked']/../../../..").click()
# Click the "Liked" button to open up "Unlike"
click_button(driver, button)
wait.until( wait.until(
EC.presence_of_element_located((By.XPATH, "//*[text()='Unlike']")) EC.presence_of_element_located((By.XPATH, "//*[text()='Unlike']"))
) )
# There should be an "Unlike" button now, click it # There should be an "Unlike" button now, click it
unlike_button = driver.find_element_by_xpath("//*[text()='Unlike']/..") driver.find_element_by_xpath("//*[text()='Unlike']/..").click()
click_button(driver, unlike_button)
if archive: if archive:
archive(Page(name=url)) archive(Page(name=url))

Loading…
Cancel
Save