This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
wall.py
112 lines (86 loc) · 4.08 KB
/
wall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
import time
# Used as a threshold to avoid running forever
MAX_POSTS = settings["MAX_POSTS"]
def delete_posts(driver,
user_profile_url,
year=None):
"""
Deletes or hides all posts from the wall
Args:
driver: seleniumrequests.Chrome Driver instance
user_profile_url: str
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)
finished = False
with archiver("wall") as archive_wall_post:
for _ in range(MAX_POSTS):
if finished:
break
post_button_sel = "_4s19"
post_content_sel = "userContent"
post_timestamp_sel = "timestampContent"
confirmation_button_exp = "//div[contains(@data-sigil, 'undo-content')]//*/a[contains(@href, 'direct_action_execute')]"
# Cannot return a text node, so it returns the parent.
# 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"]
while True:
try:
try:
timeline_element = driver.find_element_by_xpath("//div[@data-sigil='story-popup-causal-init']/a")
except SELENIUM_EXCEPTIONS:
print("Could not find any posts")
finished = True
break
post_content_element = driver.find_element_by_xpath("//article/div[@class='story_body_container']/div")
post_content_ts = driver.find_element_by_xpath(timestamp_exp)
if not (post_content_element or post_content_ts):
break
# 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()
# Wait until the buttons show up
wait_xpath(driver, "//*[contains(@data-sigil, 'removeStoryButton')]")
delete_button = None
# Search for visible buttons in priority order
# Delete -> Untag -> Hide
for button_type in button_types:
try:
delete_button = driver.find_element_by_xpath("//*[text()='{0}']".format(button_type))
if not delete_button.is_displayed():
continue
break
except SELENIUM_EXCEPTIONS as e:
print(e)
continue
if not delete_button:
print("Could not find anything to delete")
break
click_button(driver, delete_button)
wait_xpath(driver, confirmation_button_exp)
confirmation_button = driver.find_element_by_xpath(confirmation_button_exp)
print(confirmation_button)
click_button(driver, confirmation_button)
except SELENIUM_EXCEPTIONS as e:
print(e)
continue
else:
break
# Required to sleep the thread for a bit after using JS to click this button
time.sleep(5)
driver.refresh()