Various improvements #31
Merged
weskerfoot
merged 15 commits from various-improvements
into master
6 years ago
6 changed files with 242 additions and 30 deletions
@ -1,3 +1,50 @@ |
|||||
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 |
||||
|
from os import environ |
||||
|
|
||||
SELENIUM_EXCEPTIONS = (NoSuchElementException, StaleElementReferenceException) |
SELENIUM_EXCEPTIONS = (NoSuchElementException, |
||||
|
StaleElementReferenceException, |
||||
|
TimeoutException) |
||||
|
|
||||
|
def try_move(actions, el): |
||||
|
for _ in range(10): |
||||
|
try: |
||||
|
actions.move_to_element(el).perform() |
||||
|
except StaleElementReferenceException: |
||||
|
sleep(5) |
||||
|
continue |
||||
|
|
||||
|
|
||||
|
def archiver(category): |
||||
|
""" |
||||
|
category: the category of logs you want to log |
||||
|
return values: (log_file_handle, archiver) |
||||
|
|
||||
|
call archiver like archive("some content") |
||||
|
""" |
||||
|
log_path = "{0}.log".format(abspath(relpath(split(category)[-1], "."))) |
||||
|
|
||||
|
log_file = open(log_path, mode="wt", buffering=1) |
||||
|
|
||||
|
def log(content, timestamp=False): |
||||
|
if environ.get("DELETEFB_ARCHIVE", "true") == "false": |
||||
|
return |
||||
|
structured_content = { |
||||
|
"category" : category, |
||||
|
"content" : content, |
||||
|
"timestamp" : timestamp |
||||
|
} |
||||
|
|
||||
|
log_file.write("{0}\n".format(dumps(structured_content))) |
||||
|
|
||||
|
return (log_file, log) |
||||
|
|
||||
|
|
||||
|
no_chrome_driver = """ |
||||
|
You need to install the chromedriver for Selenium\n |
||||
|
Please see this link https://github.com/weskerfoot/DeleteFB#how-to-use-it\n |
||||
|
""" |
||||
|
@ -1,8 +1,69 @@ |
|||||
|
from selenium.webdriver.common.by import By |
||||
from selenium.webdriver.common.action_chains import ActionChains |
from selenium.webdriver.common.action_chains import ActionChains |
||||
from .common import SELENIUM_EXCEPTIONS |
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): |
def unlike_pages(driver): |
||||
""" |
""" |
||||
Unlike all pages |
Unlike all pages |
||||
""" |
""" |
||||
return |
|
||||
|
like_log, archive_likes = archiver("likes") |
||||
|
|
||||
|
actions = ActionChains(driver) |
||||
|
|
||||
|
load_likes(driver) |
||||
|
|
||||
|
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/div/div[text()='Liked']/../..") |
||||
|
|
||||
|
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)) |
||||
|
|
||||
|
except SELENIUM_EXCEPTIONS as e: |
||||
|
continue |
||||
|
|
||||
|
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 |
||||
|
like_log.close() |
||||
|
Loading…
Reference in new issue