Browse Source

wip activity log deletion

pull/143/head
wes 4 years ago
parent
commit
cb777d4c6d
  1. 118
      deletefb/tools/activity.py

118
deletefb/tools/activity.py

@ -3,105 +3,71 @@ from .archive import archiver
from .common import SELENIUM_EXCEPTIONS, click_button, wait_xpath, force_mobile from .common import SELENIUM_EXCEPTIONS, click_button, wait_xpath, force_mobile
from .config import settings from .config import settings
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.action_chains import ActionChains
from calendar import month_name
months = [m for m in month_name if m]
import time import time
# Used as a threshold to avoid running forever # Used as a threshold to avoid running forever
MAX_POSTS = settings["MAX_POSTS"] MAX_POSTS = settings["MAX_POSTS"]
def delete_activity(driver, def get_load_more(driver):
year=None):
""" """
Deletes or hides all activity related to posts. Click the "Load more from X" button repeatedly
Args:
driver: seleniumrequests.Chrome Driver instance
year: optional int YYYY year
""" """
driver.get("https://m.facebook.com/allactivity/?category_key=statuscluster") button_expr = f"//div[contains(text(), 'Load more from')]"
time.sleep(1000)
finished = False
with archiver("wall") as archive_wall_post:
for _ in range(MAX_POSTS):
if finished:
break
post_button_sel = "_4s19"
post_content_sel = "userContent"
post_timestamp_sel = "timestampContent"
confirmation_button_exp = "//div[contains(@data-sigil, 'undo-content')]//*/a[contains(@href, 'direct_action_execute')]"
# Cannot return a text node, so it returns the parent.
# Tries to be pretty resilient against DOM re-organizations
timestamp_exp = "//article//*/header//*/div/a[contains(@href, 'story_fbid')]//text()/.."
button_types = ["Delete post", "Remove tag", "Hide from timeline"] print("Trying to load more")
while True: while True:
try: try:
try: wait_xpath(driver, button_expr)
timeline_element = driver.find_element_by_xpath("//div[@data-sigil='story-popup-causal-init']/a") driver.find_element_by_xpath(button_expr).click()
except SELENIUM_EXCEPTIONS: except SELENIUM_EXCEPTIONS:
print("Could not find any posts")
finished = True
break break
post_content_element = driver.find_element_by_xpath("//article/div[@class='story_body_container']/div") def get_timeslices(driver):
post_content_ts = driver.find_element_by_xpath(timestamp_exp) """
Get a list of the time slices Facebook is going to let us click.
if not (post_content_element or post_content_ts): """
break
# Archive the post slice_expr = "//header"
archive_wall_post.archive(
Post(
content=post_content_element.text,
date=post_content_ts.text
)
)
actions = ActionChains(driver) wait_xpath(driver, slice_expr)
actions.move_to_element(timeline_element).click().perform() for ts in driver.find_elements_by_xpath(slice_expr):
if any(w in months for w in ts.text.strip().split()):
yield ts
try:
int(ts.text.strip())
if len(ts.text.strip()) == 4: # it's a year
yield ts
except ValueError:
continue
# Wait until the buttons show up def delete_activity(driver,
wait_xpath(driver, "//*[contains(@data-sigil, 'removeStoryButton')]") year=None):
"""
Deletes or hides all activity related to posts.
delete_button = None Args:
driver: seleniumrequests.Chrome Driver instance
year: optional int YYYY year
"""
# Search for visible buttons in priority order driver.get("https://m.facebook.com/allactivity/?category_key=statuscluster")
# Delete -> Untag -> Hide
for button_type in button_types:
try:
delete_button = driver.find_element_by_xpath("//*[text()='{0}']".format(button_type))
if not delete_button.is_displayed():
continue
break
except SELENIUM_EXCEPTIONS as e:
print(e)
continue
if not delete_button: #print(get_load_more(driver))
print("Could not find anything to delete")
break
click_button(driver, delete_button) actions = ActionChains(driver)
wait_xpath(driver, confirmation_button_exp)
confirmation_button = driver.find_element_by_xpath(confirmation_button_exp)
print(confirmation_button) for ts in get_timeslices(driver):
click_button(driver, confirmation_button) # Need to figure out how to ignore the ones with nothing in them
print(ts.text)
actions.move_to_element(ts)
get_load_more(driver)
except SELENIUM_EXCEPTIONS as e: time.sleep(1000)
print(e)
continue
else:
break
# Required to sleep the thread for a bit after using JS to click this button #with archiver("activity") as archive_wall_post:
time.sleep(5)
driver.refresh()

Loading…
Cancel
Save