Browse Source

Cleanup & refactoring

pull/53/head
Wesley Kerfoot 5 years ago
parent
commit
8ac0351ab2
  1. 12
      deletefb/deletefb.py
  2. 62
      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
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():

62
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()

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 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,

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.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):
"""

8
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,

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 .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"]

Loading…
Cancel
Save