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 4
/
twitter_checker.py
253 lines (197 loc) · 7.33 KB
/
twitter_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
import sys, aiohttp, asyncio
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
__author__ = "Alan Baumgartner"
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.
URL = 'https://www.twitter.com/{}'
def __init__(self):
super().__init__()
async def check_usernames(self, username, sem, session, lock):
#Checks username availability
async with sem:
try:
async with session.get(self.URL.format(username)) as resp:
assert resp.status == 404
self.update.emit(username)
except:
pass
finally:
with await lock:
self.count += 1
self.pupdate.emit(self.count)
async def main(self):
#Packs all usernames into a tasklist
sem = asyncio.BoundedSemaphore(50)
lock = asyncio.Lock()
async with aiohttp.ClientSession() as session:
usernames = get_usernames()
tasks = [self.check_usernames(username, sem, session, lock) for username in usernames]
await asyncio.gather(*tasks)
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(self.main())
finally:
loop.close()
class App(QMainWindow):
def __init__(self):
#Declares some constructor variables.
super().__init__()
self.title = 'Twitter 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):
usernames = get_usernames()
self.progress_bar.setMaximum(len(usernames))
self.output_text.setText('')
self.thread = Checker()
self.thread.update.connect(self.update_text)
self.thread.pupdate.connect(self.update_progress)
self.thread.start()
#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:
proxies = self.output_text.toPlainText()
proxies = proxies.strip()
with open(filename, "w") as a:
a.write(proxies)
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__":
def get_usernames():
proxies = window.input_text.toPlainText()
proxies = proxies.strip()
proxies = proxies.split('\n')
return proxies
app = QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_())