-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch_follower.py
380 lines (307 loc) · 14.9 KB
/
twitch_follower.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
import pandas as pd
import PySimpleGUI as sg
import threading
import time
import math
# Selenium setup
PATH = 'C:\Program Files\chromedriver.exe' # Path to driver
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=Service(PATH), options=options)
driver.set_window_position(0, 0)
driver.set_window_size(500, 650)
handle_of_the_window = driver.current_window_handle
driver.set_window_size(1280, 720)
driver.minimize_window()
def login():
"""Logs into Twitch"""
# Go to Twitch and click login
driver.get("https://www.twitch.tv/")
driver.find_element(
By.CSS_SELECTOR, "[data-a-target='login-button']").click()
# Wait for login form to appear and fill in username and password and click on "Login"
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "login-username")))
except:
driver.quit()
usernameField = driver.find_element(By.ID, "login-username")
usernameField.send_keys(values['username'])
passwordField = driver.find_element(By.ID, "password-input")
passwordField.send_keys(values['password'])
driver.find_element(
By.CSS_SELECTOR, "[data-a-target='passport-login-button']").click()
# Wait for a response of logging in
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//h4[text()='Verify login code'] | //strong[text()='That password was incorrect. Please try again.'] | //strong[text()='This username does not exist.'] | //strong[contains(text(), 'recognize this username. Please try again.')] | //a[@id='fc_meta_audio_btn']")))
# Successfull login enable 2FA
try:
WebDriverWait(driver, 1).until(EC.presence_of_element_located(
(By.XPATH, "//h4[text()='Verify login code']")))
window['loginmessage'].update(
'Logged in. Confirm 2FA through email.')
window['2fa'].update(disabled=False)
window['CONFIRM'].update(disabled=False)
window['RESEND CODE'].update(disabled=False)
except:
# Incorrect password
try:
WebDriverWait(driver, 1).until(EC.presence_of_element_located(
(By.XPATH, "//strong[text()='That password was incorrect. Please try again.']")))
window['loginmessage'].update('Incorrect password.')
except:
# Username does not exist
try:
WebDriverWait(driver, 1).until(EC.presence_of_element_located(
(By.XPATH, "//strong[text()='This username does not exist.']")))
window['loginmessage'].update(
'Username does not exist.')
except:
# Incorrectly written usernam
try:
WebDriverWait(driver, 1).until(EC.presence_of_element_located(
(By.XPATH, "//strong[contains(text(), 'recognize this username. Please try again.')]")))
window['loginmessage'].update(
'Username incorrectly written.')
except:
# Anti-bot protection. Requires manual intervention. Possibly bugged
try:
WebDriverWait(driver, 1).until(EC.presence_of_element_located(
(By.XPATH, "//a[@id='fc_meta_audio_btn']")))
window['loginmessage'].update(
'Caught by anti-bot protection. Solve puzzle.')
# Unminimize window
driver.switch_to.window(handle_of_the_window)
driver.set_window_rect(0, 0)
# Check for succesfull completion of anti-bot protection
WebDriverWait(driver, 300).until(EC.presence_of_element_located(
(By.XPATH, "//h4[text()='Verify login code']")))
window['loginmessage'].update(
'Logged in. Confirm 2FA through email.')
window['2fa'].update(disabled=False)
window['CONFIRM'].update(disabled=False)
except:
window['loginmessage'].update(
'Unexpected error detected.')
except:
window['loginmessage'].update(
'Error: Could not locate login element.')
def run_follower():
"""Follow accounts on the CSV file"""
global followed
followed = 0
global skipped
skipped = 0
# Loop through every link in csv
for ind in df.index:
# Go to link
link = df[0][ind]
driver.get(link)
print(ind + 1, '- Go to', link)
# Wait for either follow, unfollow button or error message to appear.
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[@data-a-target='follow-button'] | //button[@data-a-target='unfollow-button'] | //p[@data-a-target='core-error-message']")))
print('Found either (un)follow button or error page.')
# Give 1 second to find unfollow button or error page. Should be sufficient time, because of previous delay.
try:
WebDriverWait(driver, 1).until(EC.presence_of_element_located(
(By.XPATH, "//button[@data-a-target='unfollow-button'] | //p[@data-a-target='core-error-message']")))
print('Unfollow button or error page found. Skip.')
skipped += 1
# If no unfollow button or error page was found
except:
print('No unfollow button found.')
# Locate follow button
elementFB = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
(By.XPATH, "//button[@data-a-target='follow-button']")))
if elementFB:
print('Follow button found.')
# Click follow button
followButton = ActionChains(driver).move_to_element(
elementFB).click(elementFB).perform()
print('Clicked follow button.')
# Check if user is followed
elementFollowed = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
(By.XPATH, "//button[@data-a-target='unfollow-button']")))
if elementFollowed:
print('Verified followed.')
followed += 1
except:
print('Did not find anything.')
skipped += 1
global ended
ended = True
# Layout UI
sg.theme('SystemDefault1')
layout = [
[sg.Frame('Select & Load CSV File', [
[sg.Text("CSV file:", size=(8, 1), key='csvtext'), sg.Input(
key="csvpath", size=(63, 1)), sg.FileBrowse(key='csvbrowser')],
[sg.Text('\n', key="csvmessage")],
[sg.Button('LOAD')]], size=(610, 140))],
[sg.Frame('Login To Twitch', [
[sg.Text('Username:', size=(8, 1), key='usernametext'),
sg.InputText(key="username", size=(28, 1))],
[sg.Text('Password:', size=(8, 1), key="passwordtext"),
sg.InputText(key="password", password_char='*', size=(28, 1))],
[sg.Text(key="loginmessage")],
[sg.Button('LOGIN')]], size=(300, 140)),
sg.Frame('2FA Login Confirmation', [
[sg.Text('2FA Code:', size=(8, 1)),
sg.InputText(key="2fa", size=(28, 1), disabled=True)],
[sg.Text(key="2famessage")],
[sg.Button('CONFIRM', disabled=True), sg.Button('RESEND CODE', disabled=True)]], size=(300, 140))],
[sg.Frame('Twitch Autofollower', [
[sg.ProgressBar(1, key="progressbar", size=(46, 20))],
[sg.Text('Time:', size=(8, 1)),
sg.Text(key='time')],
[sg.Text('Followed:', size=(8, 1)),
sg.Text(key='follows')],
[sg.Text('Skipped:', size=(8, 1)),
sg.Text(key='skipped')],
[sg.Text(key="starttext")],
[sg.Button('START', disabled=True)]], size=(610, 200))]
]
window = sg.Window('Twitch Autofollower', layout,
size=(640, 500), finalize=True)
window['username'].bind("<Return>", "_Enter")
window['password'].bind("<Return>", "_Enter")
window['2fa'].bind("<Return>", "_Enter")
window['csvpath'].bind("<Return>", "_Enter")
window['csvbrowser'].bind("<Return>", "_Enter")
# Variables needed for updating UI
confirmed = False
loggedIn = False
fileLoaded = False
startTime = False
current_time = 0
hours = 0
minutes = 0
seconds = 0
started = False
ended = False
followed = 0
skipped = 0
# Loop to update UI ever 100ms
while True:
event, values = window.read(timeout=100)
# If window closes stop program
if event == sg.WIN_CLOSED:
break
# If LOAD button is clicked
if (event == 'LOAD' or event == "csvpath" + "_Enter" or event == "csvbrowser" + "_Enter") and values["csvpath"] != "" and values["csvpath"][-4:] == ".csv":
df = pd.read_csv(values["csvpath"], header=None)
window['csvmessage'].update('CSV file loaded: ...' + values["csvpath"]
[-50:] + '\n' + str(len(df.index)) + ' accounts found to follow.')
fileLoaded = True
if loggedIn:
window['START'].update(disabled=False)
if (event == 'LOAD' or event == "csvpath" + "_Enter" or event == "csvbrowser" + "_Enter") and values["csvpath"] == "":
window['csvmessage'].update('\nNo file selected.')
if (event == 'LOAD' or event == "csvpath" + "_Enter" or event == "csvbrowser" + "_Enter") and values["csvpath"][-4:] != ".csv":
window['csvmessage'].update(
'\nThe file you selected was not a CSV file.')
# If LOGIN button is clicked
if (event == 'LOGIN' or event == "username" + "_Enter" or event == "password" + "_Enter") and values["username"] != "" and values["password"] != "":
# Delete current message (does not work)
window['loginmessage'].update('')
login()
if (event == 'LOGIN' or event == "username" + "_Enter" or event == "password" + "_Enter") and values["username"] == "" and values["password"] != "":
window['loginmessage'].update('No username provided.')
if (event == 'LOGIN' or event == "username" + "_Enter" or event == "password" + "_Enter") and values["username"] != "" and values["password"] == "":
window['loginmessage'].update('No password provided.')
if (event == 'LOGIN' or event == "username" + "_Enter" or event == "password" + "_Enter") and values["username"] == "" and values["password"] == "":
window['loginmessage'].update('No username and password provided.')
# If CONFIRM button is clicked
if (event == 'CONFIRM' or event == "2fa" + "_Enter") and len(values["2fa"]) == 6 and values["2fa"].isdigit():
confirmationFields = driver.find_elements(
By.XPATH, "//input[@inputmode='numeric']")
# If confirmed has been clicked before remove previous input
if confirmed:
for field in reversed(confirmationFields):
field.send_keys(Keys.BACKSPACE)
confirmed = True
# Fill in 2FA
for count, value in enumerate(confirmationFields):
value.send_keys(values['2fa'][count])
# Wait for reponse from 2FA
try:
WebDriverWait(driver, 60).until(
EC.presence_of_element_located(
(By.XPATH, "//figure[@data-a-target='top-nav-avatar'] | //strong[text() = 'Verification failed']"))
)
# Avatar found, user logged in
try:
WebDriverWait(driver, 1).until(
EC.presence_of_element_located(
(By.XPATH, "//figure[@data-a-target='top-nav-avatar']"))
)
window['2famessage'].update(
'Succesfully logged in.')
start_time = int(round(time.time()))
window['2famessage'].update('Succesful confirmation.')
loggedIn = True
if fileLoaded:
window['START'].update(disabled=False)
except:
# Wrong 2FA code was put in
try:
WebDriverWait(driver, 1).until(
EC.presence_of_element_located(
(By.XPATH, "//strong[text() = 'Verification failed']"))
)
window['2famessage'].update(
'2FA code was not correct.')
except:
window['2famessage'].update(
'Unexpected error detected.')
except:
window['2famessage'].update(
'Error: Could not locate verification element.')
if (event == 'CONFIRM' or event == "2fa" + "_Enter") and values["2fa"] == '':
window['2famessage'].update(
'No 2FA code provided. Check your email.')
if (event == 'CONFIRM' or event == "2fa" + "_Enter") and len(values["2fa"]) != 6:
window['2famessage'].update(
'A 2FA code has 6 digits.')
if (event == 'CONFIRM' or event == "2fa" + "_Enter") and not values["2fa"].isdigit():
window['2famessage'].update(
'A 2FA code only has digits.')
# When RESEND CODE button is clicked
if event == 'RESEND CODE':
WebDriverWait(driver, 60).until(
EC.presence_of_element_located(
(By.XPATH, "//button[text() = 'Resend code']"))
).click()
window['2famessage'].update(
'Code was resent.')
# If START button is clicked
if event == 'START':
started = True
start_time = int(round(time.time()))
threading.Thread(target=run_follower).start()
# If program started following accounts
if started and not ended:
# Calculate time
current_time = int(round(time.time()) - start_time)
hours = math.floor(current_time / 3600)
minutes = math.floor((current_time - hours * 3600) / 60)
seconds = current_time - hours * 3600 - minutes * 60
# Update UI elements
window['progressbar'].update_bar((followed + skipped) / len(df.index))
window['time'].update(
'{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds))
window['follows'].update(str(followed))
window['skipped'].update(str(skipped))
# When loop ends, close program and browser
driver.quit()
window.close()