From 8ac0351ab279815f1ffd4c2025fd6526dd3a8a9d Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 9 Jun 2019 17:02:54 -0400 Subject: [PATCH] Cleanup & refactoring --- deletefb/deletefb.py | 12 ++++---- deletefb/tools/archive.py | 62 ++++++++++++++------------------------- deletefb/tools/common.py | 13 ++++---- deletefb/tools/likes.py | 18 ++++++++---- deletefb/tools/login.py | 8 ++--- deletefb/tools/wall.py | 26 +++++++++++++--- 6 files changed, 71 insertions(+), 68 deletions(-) diff --git a/deletefb/deletefb.py b/deletefb/deletefb.py index 20db6e3..a69dfea 100755 --- a/deletefb/deletefb.py +++ b/deletefb/deletefb.py @@ -1,17 +1,17 @@ #!/usr/bin/env python +from .tools.common import logger +from .tools.config import settings +from .tools.likes import unlike_pages +from .tools.login import login +from .tools.wall import delete_posts + import argparse import getpass import json import os import sys -from .tools.config import settings -from .tools.common import logger -from .tools.login import login -from .tools.wall import delete_posts -from .tools.likes import unlike_pages - LOG = logger("deletefb") def run_delete(): diff --git a/deletefb/tools/archive.py b/deletefb/tools/archive.py index c8f3e4c..c2e0e04 100644 --- a/deletefb/tools/archive.py +++ b/deletefb/tools/archive.py @@ -1,39 +1,17 @@ -import attr -import datetime -import uuid -import json - from contextlib import contextmanager from pathlib import Path +import attr +import json # Used to avoid duplicates in the log from pybloom_live import BloomFilter -def acquire_path(): - log_file = open(log_path, mode="ta", buffering=1) - - bfilter = BloomFilter( +def make_filter(): + return BloomFilter( capacity=settings["MAX_POSTS"], error_rate=0.001 - ) - - return - -@attr.s -class Post: - content = attr.ib() - comments = attr.ib(default=[]) - date = attr.ib(factory=datetime.datetime.now) - 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=lambda: uuid.uuid4().hex) + ) @attr.s class Archive: @@ -44,25 +22,29 @@ class Archive: # should not know about anything related to filesystem paths archive_file = attr.ib() + _bloom_filter = attr.ib(factory=make_filter) + def archive(self, content): - # do something - print("Archiving type {0} with content {1} to file".format(self.archive_type, content)) - self.archive_file.write(json.dumps(attr.asdict(content))) + """ + Archive an object + """ + print("Archiving {0}".format(content)) - def close(self): - self.archive_file.close() + if content.name not in self._bloom_filter: + self.archive_file.write(json.dumps(attr.asdict(content))) + self._bloom_filter.add(content.name) + return @contextmanager def archiver(archive_type): - archiver_instance = Archive(archive_type=archive_type, - archive_file=open( - "./%s.log" % archive_type, - mode="ta", - buffering=1 - ) - ) + archive_file = open("./%s.log" % archive_type, mode="ta", buffering=1) + + archiver_instance = Archive( + archive_type=archive_type, + archive_file=archive_file + ) try: yield archiver_instance finally: - archiver_instance.close() + archive_file.close() diff --git a/deletefb/tools/common.py b/deletefb/tools/common.py index a831fc3..b508afc 100644 --- a/deletefb/tools/common.py +++ b/deletefb/tools/common.py @@ -1,11 +1,4 @@ -import json -import logging -import logging.config -import os -import time - from .config import settings - from os.path import abspath, relpath, split, isfile from selenium.common.exceptions import ( NoSuchElementException, @@ -13,6 +6,12 @@ from selenium.common.exceptions import ( TimeoutException ) +import json +import logging +import logging.config +import os +import time + SELENIUM_EXCEPTIONS = ( NoSuchElementException, StaleElementReferenceException, diff --git a/deletefb/tools/likes.py b/deletefb/tools/likes.py index 3246d85..9f8ef05 100644 --- a/deletefb/tools/likes.py +++ b/deletefb/tools/likes.py @@ -1,13 +1,19 @@ -from selenium.webdriver.common.by import By +from .archive import archiver +from .common import SELENIUM_EXCEPTIONS, logger, click_button from selenium.webdriver.common.action_chains import ActionChains -from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC - -from .common import SELENIUM_EXCEPTIONS, logger, click_button -from .archive import archiver +from selenium.webdriver.support.ui import WebDriverWait +import attr LOG = logger(__name__) +# Data type definitions of posts and comments +@attr.s +class Page: + date = attr.ib(factory=datetime.datetime.now) + name = attr.ib() + def load_likes(driver, profile_url): """ Loads the page that lists all pages you like @@ -90,7 +96,7 @@ def unlike_page(driver, url, archive=None): click_button(driver, unlike_button) if archive: - archive(url) + archive(Page(name=url)) def unlike_pages(driver, profile_url): """ diff --git a/deletefb/tools/login.py b/deletefb/tools/login.py index 847649d..2bca993 100644 --- a/deletefb/tools/login.py +++ b/deletefb/tools/login.py @@ -1,12 +1,10 @@ -import time -import sys - +from .common import NO_CHROME_DRIVER from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.chrome.options import Options from seleniumrequests import Chrome -from .common import NO_CHROME_DRIVER - +import sys +import time def login(user_email_address, user_password, diff --git a/deletefb/tools/wall.py b/deletefb/tools/wall.py index f95d17e..0fc94f2 100644 --- a/deletefb/tools/wall.py +++ b/deletefb/tools/wall.py @@ -1,9 +1,27 @@ -import time +from .archive import archiver, Post +from .common import SELENIUM_EXCEPTIONS, click_button +from .config import settings from selenium.webdriver.common.action_chains import ActionChains -from .config import settings -from .common import SELENIUM_EXCEPTIONS, click_button -from .archive import archiver, Post +import attr +import datetime +import time +import uuid + +# Data type definitions of posts and comments +@attr.s +class Post: + content = attr.ib() + comments = attr.ib(default=[]) + date = attr.ib(factory=datetime.datetime.now) + 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=lambda: uuid.uuid4().hex) # Used as a threshold to avoid running forever MAX_POSTS = settings["MAX_POSTS"]