-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (97 loc) · 4.69 KB
/
main.py
File metadata and controls
118 lines (97 loc) · 4.69 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
import os
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.support.wait import WebDriverWait
load_dotenv()
insta = 'https://www.instagram.com'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('detach', True)
chrome_options.add_argument("--disable-notifications")
USERNAME=os.environ['INSTA_NAME']
PASSWORD=os.environ['INSTA_PW']
class InstaFollower:
def __init__(self):
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.get(insta)
def login(self):
no_cookies = self.driver.find_element(By.XPATH, '//*[contains(text(),"Decline optional cookies")]')
no_cookies.click()
time.sleep(2)
username=self.driver.find_element(By.CSS_SELECTOR, '[aria-label="Phone number, username, or email"]')
username.send_keys(USERNAME)
password=self.driver.find_element(By.CSS_SELECTOR, '[aria-label="Password"]')
password.send_keys(PASSWORD)
password.send_keys(Keys.ENTER)
not_now= WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[contains(text(),"Not now")]')))
not_now.click()
def find_followers(self):
search=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Search"]')))
search.click()
search_input=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Search input"]')))
search_input.send_keys('explovdiv')
search_input.send_keys(Keys.ENTER)
explovdiv = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//img[@alt="explovdiv\'s profile picture"]/ancestor::a')))
explovdiv.click()
time.sleep(2)
followers_button= WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[href="/explovdiv/followers/"]')))
followers_amount = int((followers_button.text).split(" ")[0])
print(followers_amount)
followers_button.click()
time.sleep(4)
return followers_amount
def follow(self,followers_amount):
# self.driver.find_element(By.XPATH, f'/html/body/div[5]/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/div[1]/div/div[2]/div/div/div/div[3]/div/button/div/div').click()
x = 2
for _ in range(2,followers_amount):
follower = self.driver.find_element(By.XPATH, f'/html/body/div[5]/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/div[1]/div/div[{_}]/div/div/div/div[3]/div/button/div/div')
if follower.text == 'Follow':
follower.click()
print('Followed')
else:
print('Already Following')
print('Next person...')
time.sleep(1)
def unfollow(self, followers_amount):
for _ in range(2,followers_amount):
follower = self.driver.find_element(By.XPATH, f'/html/body/div[5]/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/div[1]/div/div[{_}]/div/div/div/div[3]/div/button/div/div')
if follower.text == 'Following':
print('Unfollowing...')
follower.click()
time.sleep(1)
unfollow_button = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[6]/div[1]/div/div[2]/div/div/div/div/div/div/button[1]')))
unfollow_button.click()
print('UnFollowed')
else:
print('Already Unfollowed')
if _%8 == 0:
insta.scroll()
print('Next person...')
time.sleep(2)
def scroll(self):
modal = self.driver.find_element(By.XPATH,
'/html/body/div[5]/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]')
self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modal)
print('scroll')
time.sleep(2)
insta = InstaFollower()
insta.login()
number = insta.find_followers()
insta.unfollow(number)
# print(len(followers))
# for _ in followers:
# print(_.text)
# try:
# _.click()
# print('!')
# except ElementClickInterceptedException:
# print('1')
# driver.execute_script("arguments[0].click()", _)
#
# time.sleep(.8)
# driver.find_element(By.TAG_NAME, 'html').send_keys(Keys.ARROW_DOWN)