-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blocker.py
141 lines (118 loc) · 4.26 KB
/
Blocker.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
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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from dotenv import load_dotenv
import os
import time
load_dotenv()
tweet_author = ""
targets = []
blocked = 0
def init_driver():
chrome_path = os.getenv("chrome_path")
chrome_options = Options()
chrome_options.binary_location = chrome_path
return webdriver.Chrome(options=chrome_options)
def delay(seconds):
time.sleep(seconds)
def login(driver):
driver.get("https://x.com/i/flow/login")
delay(2)
username_field = driver.find_element(By.NAME, "text")
username_field.click()
delay(1)
username_field.send_keys(os.getenv("account_username"))
delay(1)
username_field.send_keys(Keys.RETURN)
delay(1)
password_field = driver.find_element(By.NAME, "password")
password_field.send_keys(os.getenv("PASSWORD"))
password_field.send_keys(Keys.RETURN)
delay(2)
print("Logged in successfully")
def get_target(driver, url):
global tweet_author
driver.get(url)
delay(5)
try:
target_username = driver.find_element(
By.XPATH,
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/section/div/div/div[2]/div/div/article/div/div/div[2]/div[2]/div/div/div[1]/div/div/div[2]/div/div/a/div/span",
)
except Exception:
target_username = driver.find_element(
By.XPATH,
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/section/div/div/div[1]/div/div/article/div/div/div[2]/div[2]/div/div/div[1]/div/div/div[2]/div/div/a/div/span",
)
tweet_author = target_username.text[1:]
targets.append(tweet_author)
print(f"Target is {tweet_author}")
def get_followers(driver):
driver.get(f"https://x.com/{tweet_author}/followers")
delay(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
delay(2)
followers = driver.find_elements(
By.XPATH,
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/section/div/div/div[*]/div/div/div/div/div[2]/div[1]/div[1]/div/div[2]/div/a/div/div/span",
)
for follower in followers:
targets.append(follower.text[1:])
driver.execute_script("window.scrollTo(0, 0);")
delay(2)
followers = driver.find_elements(
By.XPATH,
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/section/div/div/div[*]/div/div/div/div/div[2]/div[1]/div[1]/div/div[2]/div/a/div/div/span",
)
for follower in followers:
follower_text = follower.text[1:]
if follower_text not in targets:
targets.append(follower_text)
print(f"Found {len(targets)-1} followers")
def mass_block(driver):
global blocked
for target in targets:
print(f"Blocked {blocked} out of {len(targets)}")
driver.get(f"https://x.com/{target}")
delay(2)
try:
three_dots_button = driver.find_element(
By.XPATH,
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/div[3]/div/div/div/div/div[1]/div[2]/div[1]",
)
three_dots_button.click()
except Exception:
continue
delay(1)
try:
block_button = driver.find_element(
By.XPATH,
"/html/body/div[1]/div/div/div[1]/div[2]/div/div/div/div[2]/div/div[3]/div/div/div/div[4]/div[2]/div/span",
)
block_button.click()
delay(1)
confirm_block_button = driver.find_element(
By.XPATH,
"/html/body/div[1]/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div[2]/div[1]",
)
confirm_block_button.click()
delay(2)
blocked += 1
except Exception:
continue
def collect_targets(driver, url):
get_target(driver, url)
get_followers(driver)
def main():
driver = init_driver()
login(driver)
if not os.path.exists("targets.txt"):
open("targets.txt", "w").close()
with open("targets.txt", "r") as file:
for url in file:
collect_targets(driver, url.strip())
mass_block(driver)
open("targets.txt", "w").close()
if __name__ == "__main__":
main()