|
@ -2,13 +2,14 @@ from selenium.webdriver.common.by import By |
|
|
from selenium.webdriver.common.action_chains import ActionChains |
|
|
from selenium.webdriver.common.action_chains import ActionChains |
|
|
from selenium.webdriver.support.ui import WebDriverWait |
|
|
from selenium.webdriver.support.ui import WebDriverWait |
|
|
from selenium.webdriver.support import expected_conditions as EC |
|
|
from selenium.webdriver.support import expected_conditions as EC |
|
|
|
|
|
from time import sleep |
|
|
|
|
|
|
|
|
from .common import SELENIUM_EXCEPTIONS, archiver, logger |
|
|
from .common import SELENIUM_EXCEPTIONS, archiver, logger |
|
|
|
|
|
|
|
|
LOG = logger(__name__) |
|
|
LOG = logger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_likes(driver): |
|
|
def load_likes(driver, profile_url): |
|
|
""" |
|
|
""" |
|
|
Loads the page that lists all pages you like |
|
|
Loads the page that lists all pages you like |
|
|
|
|
|
|
|
@ -19,24 +20,19 @@ def load_likes(driver): |
|
|
None |
|
|
None |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
driver.refresh() |
|
|
driver.get("{0}/likes_all".format(profile_url)) |
|
|
driver.get("https://www.facebook.com/pages/?category=liked") |
|
|
|
|
|
|
|
|
|
|
|
wait = WebDriverWait(driver, 20) |
|
|
wait = WebDriverWait(driver, 20) |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
wait.until( |
|
|
wait.until( |
|
|
EC.presence_of_element_located((By.XPATH, "//button/div/div[text()='Liked']")) |
|
|
EC.presence_of_element_located((By.CSS_SELECTOR, ".PageLikeButton")) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
wait.until( |
|
|
|
|
|
EC.presence_of_element_located((By.XPATH, "//button/div/i[@aria-hidden=\"true\"]")) |
|
|
|
|
|
) |
|
|
) |
|
|
except SELENIUM_EXCEPTIONS: |
|
|
except SELENIUM_EXCEPTIONS: |
|
|
LOG.exception("Traceback of load_likes") |
|
|
LOG.exception("Traceback of load_likes") |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
def unlike_pages(driver): |
|
|
def unlike_pages(driver, profile_url): |
|
|
""" |
|
|
""" |
|
|
Unlike all pages |
|
|
Unlike all pages |
|
|
|
|
|
|
|
@ -49,40 +45,20 @@ def unlike_pages(driver): |
|
|
|
|
|
|
|
|
like_log, archive_likes = archiver("likes") |
|
|
like_log, archive_likes = archiver("likes") |
|
|
|
|
|
|
|
|
|
|
|
wait = WebDriverWait(driver, 20) |
|
|
actions = ActionChains(driver) |
|
|
actions = ActionChains(driver) |
|
|
|
|
|
|
|
|
load_likes(driver) |
|
|
load_likes(driver, profile_url) |
|
|
|
|
|
|
|
|
pages_list = driver.find_element_by_css_selector("#all_liked_pages") |
|
|
|
|
|
|
|
|
|
|
|
actions.move_to_element(pages_list).perform() |
|
|
pages = driver.find_elements_by_xpath("//li//div/div/a[contains(@class, 'lfloat')]") |
|
|
|
|
|
|
|
|
unlike_buttons = pages_list.find_elements_by_xpath("//button/div/div[text()='Liked']/../..") |
|
|
actions = ActionChains(driver) |
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
driver.execute_script("arguments[0].click();", button) |
|
|
|
|
|
|
|
|
|
|
|
archive_likes(page_name) |
|
|
|
|
|
|
|
|
|
|
|
print("{0} was unliked".format(page_name)) |
|
|
page_urls = [page.get_attribute("href").replace("www", "mobile") for page in pages] |
|
|
|
|
|
|
|
|
except SELENIUM_EXCEPTIONS: |
|
|
for url in page_urls: |
|
|
continue |
|
|
driver.get(url) |
|
|
|
|
|
|
|
|
load_likes(driver) |
|
|
|
|
|
try: |
|
|
|
|
|
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 |
|
|
# Explicitly close the log file when we're done with it |
|
|
like_log.close() |
|
|
like_log.close() |
|
|