-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selenium Instagram Terminal.py
157 lines (138 loc) · 6.08 KB
/
Selenium Instagram Terminal.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
__author__ = "CAN ÇAKAR"
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Chrome()
def close_browser(self):
self.driver.close()
def login(self):
try:
self.driver.get("https://www.instagram.com/accounts/login/")
time.sleep(4)
inp_username = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/label/input')
inp_password = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/div/label/input')
inp_username.click()
inp_username.send_keys(self.username)
inp_password.click()
inp_password.send_keys(self.password)
inp_password.send_keys(Keys.RETURN)
time.sleep(3)
not_now = self.driver.find_element_by_xpath('/html/body/div[3]/div/div/div[3]/button[2]')
not_now.click()
time.sleep(2)
except Exception:
print("Invalid username or password")
def get_followers(self):
try:
self.driver.get("https://www.instagram.com/" + self.username + "/")
followers_form = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')
followers_form.click()
time.sleep(2)
script = """
flw_form = document.querySelector('.isgrP');
flw_form.scrollTo(0, flw_form.scrollHeight);
var lenPage = flw_form.scrollHeight;
return lenPage;
"""
page_len = self.driver.execute_script(script)
x = False
while (not(x)):
page_last = page_len
time.sleep(2)
page_len = self.driver.execute_script(script)
if page_last == page_len:
x = True
time.sleep(3)
followers_list = []
followers_name = self.driver.find_elements_by_css_selector(".FPmhX.notranslate._0imsa")
for i in followers_name:
followers_list.append(i.text)
with open("followers.txt", "w", encoding="UTF-8") as flw:
for j in followers_list:
flw.write(j + "\n")
except Exception:
print("Something went wrong!")
def get_followings(self):
try:
self.driver.get("https://www.instagram.com/" + self.username + "/")
followings_form = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[3]/a')
followings_form.click()
time.sleep(2)
script = """
flw_form = document.querySelector('.isgrP');
flw_form.scrollTo(0, flw_form.scrollHeight);
var lenPage = flw_form.scrollHeight;
return lenPage;
"""
page_len = self.driver.execute_script(script)
x = False
while (not(x)):
page_last = page_len
time.sleep(2)
page_len = self.driver.execute_script(script)
if page_last == page_len:
x = True
time.sleep(3)
followings_list = []
followings_name = self.driver.find_elements_by_css_selector(".FPmhX.notranslate._0imsa")
for i in followings_name:
followings_list.append(i.text)
with open("followings.txt", "w", encoding="UTF-8") as flwing:
for j in followings_list:
flwing.write(j + "\n")
except Exception:
print("Something went wrong!")
def check_followers(self, old_list_file, new_list_file):
with open(old_list_file, "r", encoding="UTF-8") as check:
for i in check.readlines():
new_flw = open(new_list_file, "r", encoding="UTF-8")
control = False
for j in new_flw.readlines():
if j.find(i) != -1:
control = False
break
else:
control = True
if control:
i = i.strip("\n")
print("{} no longer following you".format(i))
new_flw.close()
def __exit__(self, exc_type, exc_value, traceback):
self.driver.close()
username = input("Kullanıcı adı: ")
password = input("Parola: ")
bot = InstagramBot(username=username, password=password)
bot.login()
print("""
******************************************************************
* Instagram Terminal v.1.0 *
* *
* Operations: *
* *
* 1 - Get Followers and Followings *
* 2 - Check users who unfollowed you *
* 3 - Quit Application *
* *
******************************************************************
""")
while True:
op = input("Enter a operation")
if op == 1:
bot.get_followers()
bot.get_followings()
print("Followers file: followers.txt, Followings file: followings.txt")
elif op == 2:
old = input("Enter old followers list file (like old.txt)")
bot.get_followers()
bot.check_followers(old, "followers.txt")
elif op == 3:
bot.close_browser()
else:
print("Invalid input!")