-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
213 lines (193 loc) · 7.11 KB
/
app.py
File metadata and controls
213 lines (193 loc) · 7.11 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import logging as log
import time
from getpass import getpass
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchWindowException, TimeoutException
from termcolor import colored
from base import (
clear_cmd,
site_login,
get_driver,
close_shop,
click_element,
random_time,
get_length_of_page,
start_end_log,
check_session,
)
from config import Settings
def delete_post(driver):
"""
Delete a post from the user's profile.
This function finds and clicks the more options, delete, and delete confirmation button.
Args:
driver: The Selenium WebDriver instance.
Returns:
bool: True if the post was deleted successfully, False otherwise.
"""
try:
# Wait for load and find the three-dot menu (more options)
wait = WebDriverWait(driver, 15)
menu_btn = wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "svg[aria-label='More options']")
)
)
if not menu_btn:
log.info("Options button not found.\nSkipping post...")
return False
click_element(driver, menu_btn, "More options")
random_time()
# Click delete option
delete_btn = wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button:not([disabled])[tabindex='0']")
)
)
if not delete_btn or "Delete" not in delete_btn.text:
log.info("Delete button not found.\nSkipping post...")
return False
click_element(driver, delete_btn, "Delete")
random_time()
# Click confirm deletion option
confirm_btn = wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button:not([disabled])[tabindex='0']")
)
)
if not confirm_btn or "Delete" not in confirm_btn.text:
log.info("Confirm delete button not found or incorrect.")
return False
click_element(driver, confirm_btn, "Confirm Delete")
random_time()
log.info("Post deleted successfully.")
return True
except Exception as ex:
log.error(f"Error deleting post: {ex}", exc_info=True)
return False
def main():
start_end_log(__file__)
driver = None
username = None
password = None
posts_deleted = 0
settings = Settings()
max_posts = settings.max_posts
max_scrolls = settings.max_scrolls
try:
clear_cmd()
# ASCII art banner
banner = colored(
"""
___ _ __ ___
|_ _|_ __ ___| |_ __ \\ \\ / (_)_ __ ___
| || '_ \\/ __| __/ _` \\ \\ /\\ / /| | '_ \\ / _ \\
| || | | \\__ \\ || (_| |\\ V V / | | |_) | __/
|___|_| |_|___/\\__\\__,_| \\_/\\_/ |_| .__/ \\___|
|_|
https://github.com/ajwdd
""",
color="magenta",
attrs=["bold"],
)
print(banner)
# Get user's Instagram credentials
print("Enter your Instagram credentials.")
username = input("Username: ").strip().lower()
password = getpass("Password: ").strip()
if not username or not password:
warning = colored(
"Username or password cannot be empty!", color="red", attrs=["blink"]
)
print(warning)
input("Press any key to retry...")
main()
driver = get_driver()
driver = site_login(driver, username, password)
# Navigate to profile and wait for load
driver.get(f"https://www.instagram.com/{username}/")
wait = WebDriverWait(driver, 20)
wait.until(
EC.presence_of_element_located(
(By.XPATH, f"//a[contains(@href, '/{username}/p/')]")
)
)
random_time()
while posts_deleted < max_posts:
# Check session validity
if not check_session(driver):
log.info("Session invalid, restarting driver")
close_shop(driver)
driver = get_driver()
driver = site_login(driver, username, password)
driver.get(f"https://www.instagram.com/{username}/")
wait.until(
EC.presence_of_element_located(
(By.XPATH, f"//a[contains(@href, '/{username}/p/')]")
)
)
random_time()
# Scroll multiple times to load posts
last_height = get_length_of_page(driver)
for _ in range(max_scrolls):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
new_height = get_length_of_page(driver)
if new_height == last_height:
log.info("No new posts loaded after scrolling.")
break
last_height = new_height
# Wait for post links to load (with retry)
posts = []
for attempt in range(2):
try:
wait = WebDriverWait(driver, 20)
posts = wait.until(
EC.presence_of_all_elements_located(
(By.XPATH, f"//a[contains(@href, '/{username}/p/')]")
)
)
break
except Exception as ex:
log.warning(
f"Attempt {attempt + 1}: No posts found, retrying... {ex}"
)
time.sleep(5)
log.info(f"Found {len(posts)} posts")
if not posts:
log.info("No more posts found")
break
# Process the first available post
try:
post = posts[0]
click_element(driver, post, "Post")
random_time()
if delete_post(driver):
posts_deleted += 1
except Exception as ex:
log.error(f"Error processing post: {ex}", exc_info=True)
# Navigate back and wait for profile to reload
driver.get(f"https://www.instagram.com/{username}/")
try:
wait.until(
EC.presence_of_element_located(
(By.XPATH, f"//a[contains(@href, '/{username}/p/')]")
)
)
except TimeoutException:
log.info("No posts found after navigation.\nExiting...")
break
random_time()
log.info(f"Deleted {posts_deleted} posts")
except NoSuchWindowException as ex:
log.error(f"Browser window closed unexpectedly:\n{ex}", exc_info=True)
except Exception as ex:
log.error(f"Error in main:\n{ex}", exc_info=True)
finally:
if driver:
close_shop(driver)
start_end_log(__file__, end_log=True)
if __name__ == "__main__":
main()