Browse Source

Cleanup & refactoring

pull/53/head
Wesley Kerfoot 6 years ago
parent
commit
8ac0351ab2
  1. 12
      deletefb/deletefb.py
  2. 58
      deletefb/tools/archive.py
  3. 13
      deletefb/tools/common.py
  4. 18
      deletefb/tools/likes.py
  5. 8
      deletefb/tools/login.py
  6. 26
      deletefb/tools/wall.py

12
deletefb/deletefb.py

@ -1,17 +1,17 @@
#!/usr/bin/env python #!/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 argparse
import getpass import getpass
import json import json
import os import os
import sys 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") LOG = logger("deletefb")
def run_delete(): def run_delete():

58
deletefb/tools/archive.py

@ -1,40 +1,18 @@
import attr
import datetime
import uuid
import json
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path from pathlib import Path
import attr
import json
# Used to avoid duplicates in the log # Used to avoid duplicates in the log
from pybloom_live import BloomFilter from pybloom_live import BloomFilter
def acquire_path(): def make_filter():
log_file = open(log_path, mode="ta", buffering=1) return BloomFilter(
bfilter = BloomFilter(
capacity=settings["MAX_POSTS"], capacity=settings["MAX_POSTS"],
error_rate=0.001 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 @attr.s
class Archive: class Archive:
archive_type = attr.ib() archive_type = attr.ib()
@ -44,25 +22,29 @@ class Archive:
# should not know about anything related to filesystem paths # should not know about anything related to filesystem paths
archive_file = attr.ib() archive_file = attr.ib()
_bloom_filter = attr.ib(factory=make_filter)
def archive(self, content): def archive(self, content):
# do something """
print("Archiving type {0} with content {1} to file".format(self.archive_type, content)) Archive an object
self.archive_file.write(json.dumps(attr.asdict(content))) """
print("Archiving {0}".format(content))
def close(self): if content.name not in self._bloom_filter:
self.archive_file.close() self.archive_file.write(json.dumps(attr.asdict(content)))
self._bloom_filter.add(content.name)
return
@contextmanager @contextmanager
def archiver(archive_type): 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, archiver_instance = Archive(
mode="ta", archive_type=archive_type,
buffering=1 archive_file=archive_file
)
) )
try: try:
yield archiver_instance yield archiver_instance
finally: finally:
archiver_instance.close() archive_file.close()

13
deletefb/tools/common.py

@ -1,11 +1,4 @@
import json
import logging
import logging.config
import os
import time
from .config import settings from .config import settings
from os.path import abspath, relpath, split, isfile from os.path import abspath, relpath, split, isfile
from selenium.common.exceptions import ( from selenium.common.exceptions import (
NoSuchElementException, NoSuchElementException,
@ -13,6 +6,12 @@ from selenium.common.exceptions import (
TimeoutException TimeoutException
) )
import json
import logging
import logging.config
import os
import time
SELENIUM_EXCEPTIONS = ( SELENIUM_EXCEPTIONS = (
NoSuchElementException, NoSuchElementException,
StaleElementReferenceException, StaleElementReferenceException,

18
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.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 selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from .common import SELENIUM_EXCEPTIONS, logger, click_button import attr
from .archive import archiver
LOG = logger(__name__) 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): def load_likes(driver, profile_url):
""" """
Loads the page that lists all pages you like 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) click_button(driver, unlike_button)
if archive: if archive:
archive(url) archive(Page(name=url))
def unlike_pages(driver, profile_url): def unlike_pages(driver, profile_url):
""" """

8
deletefb/tools/login.py

@ -1,12 +1,10 @@
import time from .common import NO_CHROME_DRIVER
import sys
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from seleniumrequests import Chrome from seleniumrequests import Chrome
from .common import NO_CHROME_DRIVER import sys
import time
def login(user_email_address, def login(user_email_address,
user_password, user_password,

26
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 selenium.webdriver.common.action_chains import ActionChains
from .config import settings import attr
from .common import SELENIUM_EXCEPTIONS, click_button import datetime
from .archive import archiver, Post 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 # Used as a threshold to avoid running forever
MAX_POSTS = settings["MAX_POSTS"] MAX_POSTS = settings["MAX_POSTS"]

Loading…
Cancel
Save