Browse Source

New archiver semi-working

pull/53/head
Wesley Kerfoot 5 years ago
parent
commit
8bc51b88d7
  1. 29
      deletefb/tools/archive.py
  2. 30
      deletefb/tools/likes.py
  3. 72
      deletefb/tools/wall.py

29
deletefb/tools/archive.py

@ -11,7 +11,6 @@ from pathlib import Path
from pybloom_live import BloomFilter
def acquire_path():
log_file = open(log_path, mode="ta", buffering=1)
bfilter = BloomFilter(
@ -26,14 +25,15 @@ class Post:
content = attr.ib()
comments = attr.ib(default=[])
date = attr.ib(factory=datetime.datetime.now)
name = attr.ib(factory=uuid.uuid4)
name = attr.ib(factory=lambda: uuid.uuid4().hex)
@attr.s
class Comment:
commenter = attr.ib()
content = attr.ib()
date = attr.ib(factory=datetime.datetime.now)
name = attr.ib(factory=uuid.uuid4)
name = attr.ib(factory=lambda: uuid.uuid4().hex)
@attr.s
class Archive:
@ -46,12 +46,23 @@ class Archive:
def archive(self, content):
# do something
print("Archiving type {0} with content {1} to directory {2}".format(self.archive_type, content, self.archive_dir))
self.archive_file.write(json.dumps(content.asdict()))
print("Archiving type {0} with content {1} to file".format(self.archive_type, content))
self.archive_file.write(json.dumps(attr.asdict(content)))
wall_archive = Archive(archive_type="wall")
def close(self):
self.archive_file.close()
comments = [Comment(commenter="Bob", content="Nice post!"),
Comment(commenter="Alice", content="I hate this")]
@contextmanager
def archiver(archive_type):
archiver_instance = Archive(archive_type=archive_type,
archive_file=open(
"./%s.log" % archive_type,
mode="ta",
buffering=1
)
)
wall_archive.archive(Post(content="A post!", comments=comments))
try:
yield archiver_instance
finally:
archiver_instance.close()

30
deletefb/tools/likes.py

@ -3,7 +3,8 @@ 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, logger, click_button
from .common import SELENIUM_EXCEPTIONS, logger, click_button
from .archive import archiver
LOG = logger(__name__)
@ -102,21 +103,18 @@ def unlike_pages(driver, profile_url):
None
"""
like_log, archive_likes = archiver("likes")
with archiver("likes") as archive_likes:
load_likes(driver, profile_url)
load_likes(driver, profile_url)
urls = get_page_links(driver)
urls = get_page_links(driver)
while urls:
for url in urls:
unlike_page(driver, url, archive=archive_likes.archive)
try:
load_likes(driver, profile_url)
urls = get_page_links(driver)
except SELENIUM_EXCEPTIONS:
# We're done
break
while urls:
for url in urls:
unlike_page(driver, url, archive=archive_likes)
try:
load_likes(driver, profile_url)
urls = get_page_links(driver)
except SELENIUM_EXCEPTIONS:
# We're done
break
# Explicitly close the log file when we're done with it
like_log.close()

72
deletefb/tools/wall.py

@ -2,7 +2,8 @@ import time
from selenium.webdriver.common.action_chains import ActionChains
from .config import settings
from .common import SELENIUM_EXCEPTIONS, archiver, click_button
from .common import SELENIUM_EXCEPTIONS, click_button
from .archive import archiver, Post
# Used as a threshold to avoid running forever
MAX_POSTS = settings["MAX_POSTS"]
@ -32,47 +33,52 @@ def delete_posts(driver,
button_types = ["FeedDeleteOption", "HIDE_FROM_TIMELINE", "UNTAG"]
wall_log, archive_wall_post = archiver("wall")
with archiver("wall") as archive_wall_post:
while True:
try:
timeline_element = driver.find_element_by_class_name(post_button_sel)
while True:
try:
timeline_element = driver.find_element_by_class_name(post_button_sel)
post_content_element = driver.find_element_by_class_name(post_content_sel)
post_content_ts = driver.find_element_by_class_name(post_timestamp_sel)
post_content_element = driver.find_element_by_class_name(post_content_sel)
post_content_ts = driver.find_element_by_class_name(post_timestamp_sel)
archive_wall_post(post_content_element.text, timestamp=post_content_ts.text)
# Archive the post
archive_wall_post.archive(
Post(
content=post_content_element.text,
date=post_content_ts.text
)
)
actions = ActionChains(driver)
actions.move_to_element(timeline_element).click().perform()
actions = ActionChains(driver)
actions.move_to_element(timeline_element).click().perform()
menu = driver.find_element_by_css_selector("#globalContainer > div.uiContextualLayerPositioner.uiLayer > div")
actions.move_to_element(menu).perform()
menu = driver.find_element_by_css_selector("#globalContainer > div.uiContextualLayerPositioner.uiLayer > div")
actions.move_to_element(menu).perform()
delete_button = None
delete_button = None
for button_type in button_types:
try:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"{0}\"]".format(button_type))
break
except SELENIUM_EXCEPTIONS:
continue
for button_type in button_types:
try:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"{0}\"]".format(button_type))
break
except SELENIUM_EXCEPTIONS:
continue
if not delete_button:
print("Could not find anything to delete")
break
if not delete_button:
print("Could not find anything to delete")
break
actions.move_to_element(delete_button).click().perform()
confirmation_button = driver.find_element_by_class_name("layerConfirm")
actions.move_to_element(delete_button).click().perform()
confirmation_button = driver.find_element_by_class_name("layerConfirm")
click_button(driver, confirmation_button)
click_button(driver, confirmation_button)
except SELENIUM_EXCEPTIONS:
continue
else:
break
wall_log.close()
except SELENIUM_EXCEPTIONS:
continue
else:
break
# Required to sleep the thread for a bit after using JS to click this button
time.sleep(5)
driver.refresh()
# Required to sleep the thread for a bit after using JS to click this button
time.sleep(5)
driver.refresh()

Loading…
Cancel
Save