fix issue with year argument #143

Open
weskerfoot wants to merge 10 commits from fix-year into master
  1. 11
      deletefb/deletefb.py
  2. 95
      deletefb/tools/activity.py
  3. 8
      deletefb/tools/wall.py
  4. 6
      requirements.txt
  5. 2
      run.sh
  6. 4
      setup.py

11
deletefb/deletefb.py

@ -6,6 +6,7 @@ from .tools.login import login
from .tools.wall import delete_posts
from .tools.conversations import traverse_conversations
from .tools.comments import delete_comments
from .tools.activity import delete_activity
from .quit_driver import quit_driver_and_reap_children
import argparse
@ -24,8 +25,8 @@ def run_delete():
default="wall",
dest="mode",
type=str,
choices=["wall", "unlike_pages", "conversations"],
help="The mode you want to run in. Default is `wall' which deletes wall posts"
choices=["wall", "unlike_pages", "conversations", "activity"],
help="The mode you want to run in. Default is `wall' which deletes all wall posts"
)
parser.add_argument(
@ -104,7 +105,7 @@ def run_delete():
settings["ARCHIVE"] = not args.archive_off
if args.year and args.mode not in ("wall", "conversations"):
if args.year and args.mode not in ("activity", "conversations"):
parser.error("The --year option is not supported in this mode")
args_user_password = args.password or getpass.getpass('Enter your password: ')
@ -122,9 +123,11 @@ def run_delete():
delete_posts(
driver,
args.profile_url,
year=args.year
)
elif args.mode == "activity":
delete_activity(driver, year=args.year)
elif args.mode == "unlike_pages":
unlike_pages(driver, args.profile_url)

95
deletefb/tools/activity.py

@ -0,0 +1,95 @@
from ..types import Post
from .archive import archiver
from .common import SELENIUM_EXCEPTIONS, click_button, wait_xpath, force_mobile
from .config import settings
from selenium.webdriver.common.action_chains import ActionChains
from calendar import month_name
months = [m for m in month_name if m]
import time
# Used as a threshold to avoid running forever
MAX_POSTS = settings["MAX_POSTS"]
def get_load_more(driver):
"""
Click the "Load more from X" button repeatedly
"""
button_expr = f"//div[contains(text(), 'Load more from')]"
print("Trying to load more")
while True:
try:
wait_xpath(driver, button_expr)
driver.find_element_by_xpath(button_expr).click()
except SELENIUM_EXCEPTIONS:
break
def get_timeslices(driver):
"""
Get a list of the time slices Facebook is going to let us click.
"""
slice_expr = "//header"
wait_xpath(driver, slice_expr)
print("Loaded")
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
def delete_activity(driver,
year=None):
"""
Deletes or hides all activity related to posts.
Args:
driver: seleniumrequests.Chrome Driver instance
year: optional int YYYY year
"""
driver.get("https://m.facebook.com/allactivity/?category_key=statuscluster")
if year is None:
print("Deleting all years")
years = [elem.text.strip() for elem in get_timeslices(driver)]
else:
years = [elem.text.strip() for elem in get_timeslices(driver) if year in elem.text.strip()]
actions = ActionChains(driver)
print(years)
for year in years:
year_elems = [y for y in get_timeslices(driver) if y.text.strip() == year]
if not year_elems:
raise ValueError("Non-existent year") # FIXME use a better exception
year_elem = year_elems[0]
# Need to figure out how to ignore the ones with nothing in them
print(f"Deleting activity from {year_elem.text}")
actions.move_to_element(year_elem)
year_elem.click()
get_load_more(driver)
time.sleep(10)
driver.refresh()
#with archiver("activity") as archive_wall_post:

8
deletefb/tools/wall.py

@ -10,8 +10,7 @@ import time
MAX_POSTS = settings["MAX_POSTS"]
def delete_posts(driver,
user_profile_url,
year=None):
user_profile_url):
"""
Deletes or hides all posts from the wall
@ -21,9 +20,6 @@ def delete_posts(driver,
year: optional int YYYY year
"""
if year is not None:
user_profile_url = "{0}/timeline?year={1}".format(user_profile_url, year)
user_profile_url = force_mobile(user_profile_url)
driver.get(user_profile_url)
@ -45,7 +41,7 @@ def delete_posts(driver,
# 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"]
button_types = ["Delete post", "Remove Tag", "Hide from timeline", "Hide from profile"]
while True:
try:

6
requirements.txt

@ -1,15 +1,15 @@
appdirs==1.4.3
args==0.1.0
attrs==19.1.0
attrs==20.3.0
bitarray==0.9.3
bleach==3.1.4
cattrs-3.8==0.9.1
cattrs==1.1.2
certifi==2018.11.29
chardet==3.0.4
clint==0.5.1
docutils==0.14
idna==2.8
lxml==4.4.0
lxml==4.6.2
pendulum==2.0.5
pkginfo==1.5.0.1
progressbar==2.5

2
run.sh

@ -1,2 +1,2 @@
#!/bin/bash
/usr/bin/python3 -m deletefb.deletefb -E $EMAIL -P $PASS -U $URL
python3 -m deletefb.deletefb -E $EMAIL -P $PASS -U $URL

4
setup.py

@ -24,8 +24,8 @@ setuptools.setup(
"selenium-requests",
"requests",
"pybloom-live",
"attrs",
"cattrs-3.8",
"attrs>=20.3.0",
"cattrs>=1.1.2",
"lxml",
"pendulum",
"clint",

Loading…
Cancel
Save