-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainCode.py
More file actions
241 lines (201 loc) · 8 KB
/
mainCode.py
File metadata and controls
241 lines (201 loc) · 8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
import random
import sys
import sqlite3 as db
from datetime import datetime
conn = db.connect('userData.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS listUser(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT COLLATE NOCASE,
password TEXT,
msg TEXT,
date TEXT
)''')
def addUser(user,password,msg = None):
timeDate = datetime.now().strftime('%Y - %m - %d | %H : %M')
c.execute('INSERT INTO listUser VALUES (:id,:user,:password,:msg,:date)'
,{'id':None,'user':user,'password':password,'msg':msg,'date':timeDate})
conn.commit()
conn.close()
def show():
c.execute('SELECT * FROM listUser')
res = c.fetchall()
c.execute('SELECT COUNT(*) FROM listUser')
countRes = c.fetchone()[0]
return res,countRes
def lastJoin():
c.execute('SELECT MAX(id) FROM listUser')
last = c.fetchone()[0]
c.execute('SELECT user FROM listUser WHERE id >= :id',
{'id':last})
user = c.fetchone()[0]
c.execute('SELECT password FROM listUser WHERE id >= :id',
{'id':last})
password = c.fetchone()[0]
return user,password
class instabot:
def __init__(self,userName,pas):
self.driver = webdriver.Chrome()
self.userName = userName
self.pas = pas
def closeBrowser(self):
self.driver.close()
@property
def SignIn(self):
self.driver.get('https://www.instagram.com/accounts/login/')
userNameInput = self.driver.find_element_by_xpath('//input[@name= "username"]')
passwordInput = self.driver.find_element_by_xpath('//input[@name = "password"]')
userNameInput.send_keys(self.userName)
passwordInput.send_keys(self.pas)
passwordInput.send_keys(Keys.ENTER)
notNowButton = WebDriverWait(self.driver, 15).until(lambda d: d.find_element_by_xpath('//button[text()="Not Now"]'))
notNowButton.click()
def likeTagPhoto(self, hashtag):
driver = self.driver
driver.get("https://www.instagram.com/explore/tags/{}/".format(hashtag))
sleep(2)
pic_hrefs = []
for i in range(1,2):
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(2)
hrefs_in_view = driver.find_elements_by_tag_name('a')
hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view
if '.com/p/' in elem.get_attribute('href')]
[pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
except Exception:
continue
for pic_href in pic_hrefs:
driver.get(pic_href)
sleep(2)
try:
sleep(random.randint(2, 4))
like_button = lambda: driver.find_element_by_class_name('fr66n').click()
like_button()
sleep(2)
except Exception as e:
sleep(2)
def likeUserPhoto(self,user):
driver = self.driver
driver.get('https://www.instagram.com/{}'.format(user))
sleep(2)
pic_hrefs = []
for i in range(1,2):
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(2)
hrefs_in_view = driver.find_elements_by_tag_name('a')
hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view
if '.com/p/' in elem.get_attribute('href')]
[pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
except Exception:
continue
for pic_href in pic_hrefs:
driver.get(pic_href)
sleep(2)
try:
sleep(random.randint(2, 4))
like_button = lambda: driver.find_element_by_class_name('fr66n').click()
like_button()
sleep(2)
except Exception as e:
sleep(2)
def follow(self,user):
driver = self.driver
driver.get('https://www.instagram.com/{}/'.format(user))
followButtom = driver.find_element_by_css_selector('button')
followButtom.click()
sleep(3)
def unFollow(self,user):
driver = self.driver
driver.get('https://www.instagram.com/{}/'.format(user))
unfollowStepOne = driver.find_element_by_css_selector('button')
unfollowStepOne.click()
unfollowButtom = driver.find_element_by_class_name('aOOlW.-Cab_')
unfollowButtom.click()
def seenStroy(self,user):
driver = self.driver
driver.get('https://www.instagram.com/{}/'.format(user))
story = driver.find_element_by_class_name('_2dbep')
story.click()
sleep(2)
while True:
try:
nextBottom = driver.find_element_by_class_name('coreSpriteRightChevron')
nextBottom.click()
sleep(2)
except Exception:
break
def reportUser(self,user):
driver = self.driver
driver.get('https://www.instagram.com/{}/'.format(user))
threePoint = driver.find_element_by_class_name('AFWDX')
threePoint.click()
sleep(random.randint(2,5))
reportButtom = driver.find_element_by_xpath('//button[text() = "Report User"]')
reportButtom.click()
sleep(random.randint(2,5))
resoneButtom = driver.find_element_by_css_selector('div[role=\'list\'] button')
resoneButtom.click()
sleep(random.randint(2,5))
close = driver.find_element_by_xpath('//button[text() = "Close"]')
close.click()
sleep(6)
def blockUser(self,user):
driver = self.driver
driver.get('https://www.instagram.com/{}/'.format(user))
threePoint = driver.find_element_by_class_name('AFWDX')
threePoint.click()
sleep(2)
blockButtom = driver.find_element_by_xpath('//button[text() = "Block this user"]')
blockButtom.click()
sureBlock = driver.find_element_by_class_name('aOOlW.bIiDR')
sureBlock.click()
dismiss = driver.find_element_by_class_name('aOOlW.HoLwm')
dismiss.click()
sleep(3)
def getUserFollowers(self, username):
driver = self.driver
driver.get('https://www.instagram.com/{}'.format(username)
followersLink = driver.find_element_by_css_selector('ul li a')
followersLink.click()
sleep(2)
followersList = driver.find_element_by_css_selector('div[role=\'dialog\'] ul')
followersList.click()
followers = []
for user in followersList.find_elements_by_css_selector('li'):
userLink = user.find_element_by_css_selector('a').get_attribute('href')
followers.append(userLink)
if (len(followers) == max):
break
listId = []
for idName in followers:
listId.append(idName[26:-1])
return listId
def getUserFollowing(self,username):
driver = self.driver
driver.get('https://www.instagram.com/{}'.format(username))
followingLink = driver.find_elements_by_css_selector('ul li a')[1]
followingLink.click()
sleep(2)
followingList = driver.find_element_by_css_selector('div[role=\'dialog\'] ul')
followingList.click()
following = []
for user in followingList.find_elements_by_css_selector('li'):
userLink = user.find_element_by_css_selector('a').get_attribute('href')
following.append(userLink)
listId = []
for idName in following:
listId.append(idName[26:-1])
return listId
if __name__ == "__main__":
username = "hocoHelper"
password = "##########"
ig = instabot(username, password)
ig.SignIn
ig.closeBrowser()
conn.close()