-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
487 lines (405 loc) · 21.8 KB
/
bot.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
from selenium.common.exceptions import NoSuchElementException
from models_parser import parse_models, set_up_parser
from selenium.webdriver.common.keys import Keys
from seleniumwire import webdriver
import data_base_connector as db
import datetime
import random
import time
only_fans_url = 'http://onlyfans.com'
prev_comment = ""
models = []
comments = []
accounts = []
start_acc = 0
# Натсройка веб - драйвера
def set_up_bot(account):
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument("--disable-extensions")
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
proxy_options = {
"proxy": {
"https": f"https://{account[4]}:{account[5]}@{account[2]}:{account[3]}"
}
}
driver = webdriver.Chrome(options=options, seleniumwire_options=proxy_options)
driver.set_window_rect(980, 10)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.delete_all_cookies()
return driver
def new_models():
global prev_comment
for account in accounts[start_acc:]:
nickname = account[6]
print(f"Аккаунт : {account[0]} | {nickname}")
bot = set_up_bot(account)
time.sleep(2)
if not db.get_subs(account):
last_model = 0
else:
last_model = models.index(db.get_model(db.get_subs(account, last=True)[0]))
last_sub_time = datetime.datetime.strptime(db.get_subs(account, last=True)[1], '%Y-%m-%d %H:%M:%S.%f')
if db.get_today_subs(account) == 0:
print("Кол-во подписок на сегодня : ", db.get_today_subs(account))
print("Делаем проверку на прохождение 24 часов...")
if (datetime.datetime.now() - last_sub_time).days >= 1:
print("24 часа прошли можно начинать подписываться")
else:
print("24 часа ещё не прошли подписываться нельзя, переходим к следующему аккаунту...")
bot.close()
bot.quit()
continue
elif db.get_today_subs(account) == 100:
print("100 подписок есть на аккаунте : ", account[0])
bot.close()
bot.quit()
continue
print(
f"Модель с которой закончили на этом аккаунте : [Индекс] {last_model} | [Никнейм] {models[last_model]}")
if last_model == len(models) - 1:
print("Это последняя модель, нужно спарсить новые...")
parse_models(set_up_parser())
return
# Переходим на стартовую страницу
bot.get(only_fans_url)
time.sleep(random.randint(4, 6))
username = bot.find_elements_by_class_name('v-text-field__slot')[0].find_element_by_tag_name('input')
password = bot.find_elements_by_class_name('v-text-field__slot')[1].find_element_by_tag_name('input')
# Отправляем в поля username и password и нажимаем ENTER
username.send_keys(account[0])
time.sleep(random.randint(2, 3))
password.send_keys(account[1])
time.sleep(random.randint(1, 3))
password.send_keys(Keys.ENTER)
time.sleep(30)
# Начинаем проходиться по списку моделей (ссылок)
while db.get_today_subs(account) < 100:
try:
time.sleep(random.randint(2, 5))
bot.get(only_fans_url + '/' + models[last_model])
print("----------------------------------------------")
print("Переходим к модели : ", models[last_model])
time.sleep(6)
if exist(bot, "b-404", "class"):
db.delete_model(models[last_model])
time.sleep(1)
last_model += 1
continue
time.sleep(2)
if banned(bot):
print("Модель забанена")
last_model += 1
continue
sub_txt = bot.find_element_by_class_name('b-btn-text')
# Проверка на то что подписка бесплатная
if not free(bot):
print("Это платная модель")
db.delete_model(models[last_model])
time.sleep(1)
last_model += 1
continue
# Подписываемся если не подписались
if not subscribed(sub_txt):
print("Подписываемся на модель : ", models[last_model])
sub_txt.click()
time.sleep(random.randint(6, 8))
else:
print(f"Мы уже подписаны на {models[last_model]}")
# Записываем подписку в БД
if db.get_model_id(models[last_model]) not in db.get_subs(account):
print("Записываем модель в БД...")
db.add_sub(models[last_model], account, datetime.datetime.now())
else:
print("Такая модель уже есть в БД")
time.sleep(2)
print(f"Кол-во подписок за этот день : {db.get_today_subs(account)}")
# Находим посты
print("Находим посты...")
time.sleep(random.randint(2, 4))
bot.execute_script("window.scrollBy(0, 300)")
time.sleep(random.randint(3, 5))
posts = bot.find_elements_by_class_name('b-post__wrapper')
time.sleep(1)
# Фильтруем невидимые посты
posts = list(filter(lambda x: exist(x, "div", "tag"), posts))
print("Количество постов : ", len(posts))
# Делаем проверку на рестрикт (нельзя коментить посты)
posts = list(filter(lambda x: x.find_element_by_class_name(
'b-post__tools').find_elements_by_tag_name('button')[1].is_enabled(), posts))
print("Количество постов, которые можно комментировать : ", len(posts))
if not posts:
print("Нельзя комментировать посты")
db.delete_model_from_subs(models[last_model])
restrict_txt = ""
if exist(bot, name="b-dropdown-wrapper", by="class"):
if not exist(bot, name="b-dropdown-text", by="class"):
print("Мы в бане у этой модели")
last_model += 1
continue
else:
restrict_txt = bot.find_element_by_class_name("b-dropdown-text").text
else:
bot.find_element_by_class_name("g-icon.has-tooltip").click()
if not exist(bot, name="b-dropdown-text", by="class"):
print("Мы в бане у этой модели")
last_model += 1
continue
else:
restrict_txt = bot.find_element_by_class_name("b-dropdown-text").text
if restrict_txt == "Ограничить":
restrict(bot)
elif restrict_txt == "Неограниченный":
print("Модель уже в рестрикте")
print("Переходим к следующей моделе")
last_model += 1
continue
else:
print("Комментировать посты можно")
# Фильтруем посты без рекламы
print("Фильтруем посты с рекламой")
posts = list(filter(lambda x: not has_adv(x), posts))
print("Количество постов без рекламы : ", len(posts))
post = posts[0]
# Проверяем посты на лайки и коменты
print("Лайкаем и коментируем посты...")
if post.find_element_by_tag_name('div').get_attribute(
'class') == "b-post m-stream-post is-not-post-page":
print("Пост - стрим")
continue
# Если нет лайка и можно лайкать, то лайкаем пост
if post.find_element_by_class_name('b-post__tools').find_element_by_tag_name(
'button').is_enabled() and not liked(post):
post.find_element_by_class_name('b-post__tools').find_element_by_tag_name('button').click()
print("Лайк !")
time.sleep(random.randint(2, 4))
# Коменты
if not commented(post, nickname):
print("Пишем коментарий...")
bot.execute_script("window.scrollBy(0, 300)")
time.sleep(1)
JS_ADD_TEXT_TO_INPUT = """
var elm = arguments[0], txt = arguments[1];
elm.value += txt;
elm.dispatchEvent(new Event('change'));
"""
# Находим поле для ввода комента
text_area = post.find_element_by_class_name('b-comments__form').find_element_by_tag_name(
'textarea')
current_comment = comments[random.randint(0, len(comments) - 1)]
while current_comment == prev_comment:
current_comment = comments[random.randint(0, len(comments) - 1)]
time.sleep(1)
text_area.send_keys(" ")
time.sleep(0.5)
bot.execute_script(JS_ADD_TEXT_TO_INPUT, text_area, current_comment)
time.sleep(0.5)
text_area.send_keys(" ")
time.sleep(0.5)
prev_comment = current_comment
# Отправляем коментарий...
post.find_element_by_class_name('b-comments__form').find_element_by_class_name(
'g-btn.m-rounded.m-icon.m-icon-only.m-colored.m-sm-size.b-comments__btn-submit').click()
time.sleep(random.randint(2, 4))
last_model += 1
time.sleep(random.randint(2, 4))
except Exception as err:
with open('log_new_models.txt', 'w') as f:
f.write(f"Ошибка - {err} {type(err)=}, {err.args}, {datetime.datetime.now()}")
f.close()
print("Какая-то ошибка...")
print("Переходим к следующей моделе")
last_model += 1
continue
print(f"100 подписок на аккаунте {account[0]}, сделаны")
bot.close()
bot.quit()
def old_models():
global prev_comment
for account in accounts[start_acc:]:
nickname = account[6]
print(f"Аккаунт : {account[0]} | {nickname}")
sub_models = [db.get_model(value) for value in db.get_subs(account)]
sub_models = list(filter(lambda x: x is not None, sub_models))
bot = set_up_bot(account)
last_model = 0
time.sleep(2)
# Переходим на стартовую страницу
bot.get(only_fans_url)
time.sleep(random.randint(4, 6))
username = bot.find_elements_by_class_name('v-text-field__slot')[0].find_element_by_tag_name('input')
password = bot.find_elements_by_class_name('v-text-field__slot')[1].find_element_by_tag_name('input')
# Отправляем в поля username и password и нажимаем ENTER
username.send_keys(account[0])
time.sleep(random.randint(2, 3))
password.send_keys(account[1])
time.sleep(random.randint(1, 3))
password.send_keys(Keys.ENTER)
time.sleep(30)
# Начинаем проходиться по списку модолей (ссылок)
for i, model in enumerate(sub_models):
try:
time.sleep(random.randint(2, 5))
bot.get(only_fans_url + '/' + model)
print("----------------------------------------------")
print("Переходим к модели : ", model)
time.sleep(5)
# Находим посты
print("Находим посты...")
time.sleep(random.randint(2, 4))
bot.execute_script("window.scrollBy(0, 300)")
time.sleep(random.randint(3, 5))
posts = bot.find_elements_by_class_name('b-post__wrapper')
time.sleep(2)
# Фильтруем невидимые посты
posts = list(filter(lambda x: exist(x, "div", "tag"), posts))
time.sleep(1)
print("Количество постов : ", len(posts))
# Делаем проверку на рестрикт (нельзя коментить посты)
posts = list(filter(lambda x: x.find_element_by_class_name(
'b-post__tools').find_elements_by_tag_name('button')[1].is_enabled(), posts))
print("Количество постов, которые можно комментировать : ", len(posts))
if not posts:
print("Нельзя комментировать посты")
db.delete_model_from_subs(model)
restrict_txt = ""
if exist(bot, name="b-dropdown-wrapper", by="class"):
if not exist(bot, name="b-dropdown-text", by="class"):
print("Мы в бане у этой модели")
last_model += 1
continue
else:
restrict_txt = bot.find_element_by_class_name("b-dropdown-text").text
else:
bot.find_element_by_class_name("g-icon.has-tooltip").click()
if not exist(bot, name="b-dropdown-text", by="class"):
print("Мы в бане у этой модели")
last_model += 1
continue
else:
restrict_txt = bot.find_element_by_class_name("b-dropdown-text").text
if restrict_txt == "Ограничить":
restrict(bot)
elif restrict_txt == "Неограниченный":
print("Модель уже в рестрикте")
print("Переходим к следующей моделе")
last_model += 1
continue
else:
print("Комментировать посты можно")
# Фильтруем посты без рекламы
print("Фильтруем посты с рекламой")
posts = list(filter(lambda x: not has_adv(x), posts))
print("Количество постов без рекламы : ", len(posts))
# Проверяем посты на лайки и коменты
print("Лайкаем и коментируем посты...")
i = 0
for post in posts:
if i == 3:
break
if post.find_element_by_tag_name('div').get_attribute(
'class') == "b-post m-stream-post is-not-post-page":
print("Пост - стрим")
continue
# Если нет лайка и можно лайкать, то лайкаем пост
if post.find_element_by_class_name('b-post__tools').find_element_by_tag_name(
'button').is_enabled() and not liked(post):
post.find_element_by_class_name('b-post__tools').find_element_by_tag_name('button').click()
print("Лайк !")
time.sleep(random.randint(2, 4))
# Коменты
if post.find_element_by_class_name('b-post__tools').find_elements_by_tag_name(
'button')[1].is_enabled() and not commented(post, nickname):
print("Пишем коментарий...")
bot.execute_script("window.scrollBy(0, 300)")
time.sleep(1)
JS_ADD_TEXT_TO_INPUT = """
var elm = arguments[0], txt = arguments[1];
elm.value += txt;
elm.dispatchEvent(new Event('change'));
"""
# Находим поле для ввода комента
text_area = post.find_element_by_class_name('b-comments__form').find_element_by_tag_name(
'textarea')
current_comment = comments[random.randint(0, len(comments) - 1)]
while current_comment == prev_comment:
current_comment = comments[random.randint(0, len(comments) - 1)]
time.sleep(1)
text_area.send_keys(" ")
time.sleep(0.5)
bot.execute_script(JS_ADD_TEXT_TO_INPUT, text_area, current_comment)
time.sleep(0.5)
text_area.send_keys(" ")
time.sleep(0.5)
prev_comment = current_comment
# Отправляем коментарий...
post.find_element_by_class_name('b-comments__form').find_element_by_class_name(
'g-btn.m-rounded.m-icon.m-icon-only.m-colored.m-sm-size.b-comments__btn-submit').click()
time.sleep(random.randint(2, 4))
i += 1
last_model += 1
time.sleep(random.randint(1, 3))
except Exception as err:
with open('log_old_models.txt', 'w') as f:
f.write(f"Ошибка - {err} {type(err)=}, {datetime.datetime.now()}")
f.close()
print("Какая-то ошибка...")
print("Переходим к следующей моделе")
# raise
last_model += 1
continue
bot.close()
bot.quit()
def exist(elem, name="div", by="selector"):
try:
if by == "selector":
elem.find_element_by_css_selector(name)
elif by == "id":
elem.find_element_by_id(name)
elif by == "class":
elem.find_element_by_class_name(name)
elif by == "tag":
elem.find_element_by_tag_name(name)
except NoSuchElementException:
return False
return True
# Проверка на рекламу
def has_adv(post):
if exist(post, "b-post__text-el", "class"):
if exist(post.find_element_by_class_name("b-post__text-el"), "a", "tag"):
return True
return False
# Проверка на подписку
def subscribed(sub_txt):
return "подписной" in sub_txt.text.lower()
# Провекра что подписка бесплатная
def free(bot):
return not exist(bot, "b-btn-text__small", "class")
# Проверка что лайк поставлен
def liked(post):
like = post.find_element_by_class_name('b-post__tools').find_element_by_tag_name('button')
return "m-active" in like.get_attribute('class')
# Проверка что комент есть
def commented(post, nickname):
comment = post.find_element_by_class_name('b-post__tools').find_elements_by_tag_name('button')[1]
comment.click()
time.sleep(3)
comments = post.find_element_by_class_name('b-comments').get_attribute('innerHTML')
if nickname in comments:
print("Коментарий уже есть")
time.sleep(2)
return True
print("Коментария нет")
return False
def restrict(bot):
bot.find_element_by_class_name("b-dropdown-text").click()
print("Рестрикт !")
def banned(bot):
if exist(bot, "g-icon.has-tooltip", "class"):
bot.find_element_by_class_name("g-icon.has-tooltip").click()
time.sleep(1)
text = bot.find_element_by_class_name("m-danger").find_element_by_class_name("dropdown-item").text
if text == "Разблокировать":
return True
return False