This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
instagram_checker.py
332 lines (263 loc) · 10.1 KB
/
instagram_checker.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import sys, aiohttp, asyncio
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
__author__ = 'Alan Baumgartner'
class LoginDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowModality(Qt.ApplicationModal)
self.setWindowFlags(self.windowFlags() ^ Qt.WindowContextHelpButtonHint)
self.setWindowTitle('Login to Instagram')
layout = QGridLayout()
self.username_label = QLabel('Username')
self.password_label = QLabel('Password')
self.username_text = QLineEdit()
self.password_text = QLineEdit()
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
self.password_text.setEchoMode(2)
layout.addWidget(self.username_label, 0, 0)
layout.addWidget(self.password_label, 1, 0)
layout.addWidget(self.username_text, 0, 1)
layout.addWidget(self.password_text, 1, 1)
layout.addWidget(buttons, 2, 0, 3, 0)
self.setLayout(layout)
self.setGeometry(400, 400, 300, 60)
@staticmethod
def getLoginInfo():
dialog = LoginDialog()
result = dialog.exec_()
return dialog.username_text.text(), dialog.password_text.text(), result == QDialog.Accepted
class ImportDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowFlags(self.windowFlags() ^ Qt.WindowContextHelpButtonHint)
self.setWindowModality(Qt.ApplicationModal)
self.setWindowTitle('Import Usernames')
layout = QGridLayout()
self.file_label = QLabel('Filename')
self.file_text = QLineEdit()
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(self.file_label, 0, 0)
layout.addWidget(self.file_text, 0, 1)
layout.addWidget(buttons, 1, 0, 2, 0)
self.setLayout(layout)
self.setGeometry(400, 400, 300, 60)
@staticmethod
def getFileInfo():
dialog = ImportDialog()
result = dialog.exec_()
return dialog.file_text.text(), result == QDialog.Accepted
class ExportDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowFlags(self.windowFlags() ^ Qt.WindowContextHelpButtonHint)
self.setWindowModality(Qt.ApplicationModal)
self.setWindowTitle('Export Usernames')
layout = QGridLayout()
self.file_label = QLabel('Filename')
self.file_text = QLineEdit()
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(self.file_label, 0, 0)
layout.addWidget(self.file_text, 0, 1)
layout.addWidget(buttons, 1, 0, 2, 0)
self.setLayout(layout)
self.setGeometry(400, 400, 300, 60)
@staticmethod
def getFileInfo():
dialog = ExportDialog()
result = dialog.exec_()
return dialog.file_text.text(), result == QDialog.Accepted
class Checker(QThread):
#Signal Variables.
update = pyqtSignal(object)
pupdate = pyqtSignal(object)
count = 0
#Global Variables.
LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/'
URL = 'https://www.instagram.com/{}'
def __init__(self, igname, igpass):
super().__init__()
self.igname = igname
self.igpass = igpass
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(self.main())
finally:
loop.close()
#Checks username availability.
async def check_usernames(self, username, sem, session, lock):
async with sem:
try:
async with session.get(self.URL.format(username)) as resp:
text = await resp.text()
if "Page Not Found" in text:
self.update.emit(username)
except:
pass
finally:
with await lock:
self.count += 1
self.pupdate.emit(self.count)
#Creates a task for each username and then runs each task.
async def main(self):
sem = asyncio.BoundedSemaphore(50)
lock = asyncio.Lock()
async with aiohttp.ClientSession() as session:
cont = await self.login(self.igname, self.igpass, session)
if cont:
usernames = get_usernames()
tasks = [self.check_usernames(username, sem, session, lock) for username in usernames]
await asyncio.gather(*tasks)
else:
pass
#Logs into Instagram.
async def login(self, username, password, session):
async with session.get(self.URL.format('')) as response:
csrftoken = await response.text()
csrftoken = csrftoken.split('csrf_token":"')[1].split('"')[0]
async with session.post(
self.LOGIN_URL,
headers={
'x-csrftoken': csrftoken, 'x-instagram-ajax':'1',
'x-requested-with': 'XMLHttpRequest',
'Origin': self.URL, 'Referer': self.URL
},
data={
'username':username, 'password':password
}
) as response:
text = await response.json()
if text['authenticated']:
return True
else:
self.update.emit('Login Failed')
return False
class App(QMainWindow):
def __init__(self):
#Declares some constructor variables.
super().__init__()
self.title = 'Instagram Username Checker'
self.left = 300
self.top = 300
self.width = 500
self.height = 500
self.initUI()
def initUI(self):
#Setup layout.
wid = QWidget(self)
self.setCentralWidget(wid)
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
layout = QGridLayout()
wid.setLayout(layout)
#Create Widgets.
menu_bar = self.menuBar()
menu = menu_bar.addMenu("File")
import_action = QAction("Import Usernames", self)
import_action.triggered.connect(self.import_usernames)
export_action = QAction("Export Usernames", self)
export_action.triggered.connect(self.export_usernames)
quit_action = QAction("Close", self)
quit_action.triggered.connect(self.quit)
menu.addAction(import_action)
menu.addAction(export_action)
menu.addAction(quit_action)
self.start_button = QPushButton('Start')
self.start_button.clicked.connect(self.start_clicked)
self.stop_button = QPushButton('Stop')
self.stop_button.clicked.connect(self.stop_clicked)
self.input_text = QTextEdit()
self.output_text = QTextEdit()
input_label = QLabel('Usernames to Check')
input_label.setAlignment(Qt.AlignCenter)
output_label = QLabel('Available Usernames')
output_label.setAlignment(Qt.AlignCenter)
self.progress_bar = QProgressBar()
#Add widgets to the window.
layout.addWidget(input_label, 0, 0)
layout.addWidget(output_label, 0, 1)
layout.addWidget(self.input_text, 1, 0)
layout.addWidget(self.output_text, 1, 1)
layout.addWidget(self.start_button, 2, 0)
layout.addWidget(self.stop_button, 2, 1)
layout.addWidget(self.progress_bar, 3, 0, 4, 0)
#When the start button is clicked, start the checker thread.
def start_clicked(self):
login = LoginDialog()
igname, igpass, result = login.getLoginInfo()
if result:
usernames = get_usernames()
self.progress_bar.setMaximum(len(usernames))
self.output_text.setText('')
self.thread = Checker(igname, igpass)
self.thread.update.connect(self.update_text)
self.thread.pupdate.connect(self.update_progress)
self.thread.start()
else:
pass
#When the stop button is clicked, terminate the checker thread.
def stop_clicked(self):
try:
self.thread.terminate()
except:
pass
#When the checker thread emits a signal, update the output textbox.
def update_text(self, text):
self.output_text.append(str(text))
#When the checker thread emits a signal, update the progress bar.
def update_progress(self, val):
self.progress_bar.setValue(val)
#Saves usernames from the output text.
def export_usernames(self):
exportDialog = ExportDialog()
filename, result = exportDialog.getFileInfo()
if result:
try:
usernames = self.output_text.toPlainText()
usernames = usernames.strip()
with open(filename, "w") as a:
a.write(usernames)
except:
pass
else:
pass
def import_usernames(self):
importDialog = ImportDialog()
filename, result = importDialog.getFileInfo()
if result:
try:
with open(filename, "r") as f:
out = f.read()
self.input_text.setText(out)
except:
pass
else:
pass
def quit(self):
sys.exit()
if __name__ == '__main__':
#Get usernames from the input textbox.
def get_usernames():
usernames = window.input_text.toPlainText()
usernames = usernames.strip()
usernames = usernames.lower()
usernames = usernames.split('\n')
return usernames
app = QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_())