Browse Source

Unliking pages working

pull/31/head
Wesley Kerfoot 5 years ago
parent
commit
a26fcf42f3
  1. 8
      deletefb/tools/common.py
  2. 66
      deletefb/tools/likes.py

8
deletefb/tools/common.py

@ -1,9 +1,13 @@
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.common.exceptions import (NoSuchElementException,
StaleElementReferenceException,
TimeoutException)
from time import sleep
from json import dumps
from os.path import abspath, relpath, split
SELENIUM_EXCEPTIONS = (NoSuchElementException, StaleElementReferenceException)
SELENIUM_EXCEPTIONS = (NoSuchElementException,
StaleElementReferenceException,
TimeoutException)
def try_move(actions, el):
for _ in range(10):

66
deletefb/tools/likes.py

@ -1,10 +1,28 @@
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from .common import SELENIUM_EXCEPTIONS, archiver
def load_likes(driver):
"""
Loads the page that lists all pages you like
"""
driver.get("https://www.facebook.com/pages/?category=liked")
wait = WebDriverWait(driver, 20)
try:
wait.until(
EC.presence_of_element_located((By.XPATH, "//button/div/div[text()='Liked']"))
)
wait.until(
EC.presence_of_element_located((By.XPATH, "//button/div/i[@aria-hidden=\"true\"]"))
)
except SELENIUM_EXCEPTIONS:
return
def unlike_pages(driver,
user_profile_url):
"""
@ -15,30 +33,40 @@ def unlike_pages(driver,
actions = ActionChains(driver)
driver.get("https://www.facebook.com/pages/?category=liked")
load_likes(driver)
wait = WebDriverWait(driver, 20)
pages_list = driver.find_element_by_css_selector("#all_liked_pages")
wait.until(
EC.presence_of_element_located((By.XPATH, "//div[text()='Liked']"))
)
actions.move_to_element(pages_list).perform()
sleep(10)
unlike_buttons = pages_list.find_elements_by_xpath("//button/div/div[text()='Liked']/../..")
pages_list = driver.find_element_by_css_selector("#all_liked_pages")
actions.move_to_element(pages_list).perform()
unlike_buttons = pages_list.find_elements_by_xpath("//button")
while unlike_buttons:
for button in unlike_buttons:
try:
if "Liked" in button.text:
page_name = button.find_element_by_xpath("./../..").text.split("\n")[0]
print("Trying to unlike {0}".format(page_name))
driver.execute_script("arguments[0].click();", button)
archive_likes(page_name)
print("{0} was unliked".format(page_name))
except SELENIUM_EXCEPTIONS as e:
continue
for button in unlike_buttons:
load_likes(driver)
try:
actions.move_to_element(button).perform()
page_name = button.find_element_by_xpath("./../..").text.split("\n")[0]
archive_likes(page_name)
except SELENIUM_EXCEPTIONS as e:
print(e)
continue
print(button)
pages_list = driver.find_element_by_css_selector("#all_liked_pages")
actions.move_to_element(pages_list).perform()
unlike_buttons = pages_list.find_elements_by_xpath("//button")
if not unlike_buttons:
break
except SELENIUM_EXCEPTIONS:
break
# Explicitly close the log file when we're done with it
like_log.close()

Loading…
Cancel
Save