from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
import os
import os.path
import sys
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pickle
import datetime
dt_now = datetime.datetime.now()
PATH = os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0] + '/'
options = Options()
options.add_argument("--mute-audio")
options.add_argument("--disable-blink-features=AutomationControlled")
options.headless = False
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36")
driver = webdriver.Chrome(executable_path=PATH + "chromedriver",
options=options)
with open('settings.txt' , 'r') as file:
text = file.readlines()
for line in text:
if 'Запрос на поиск' in line:
constipation = str(line.replace('Запрос на поиск - ', ''))
constipation = constipation.strip()
elif 'Количевство взятых из поиска видео' in line:
vd = int(line.replace('Количевство взятых из поиска видео - ', ''))
elif 'Текст комментария' in line:
comments_text = str(line.replace('Текст комментария - ', ''))
comments_text = comments_text.strip()
elif 'Количевство отправленных комментариев' in line:
vdcomments = int(line.replace('Количевство отправленных комментариев - ', ''))
try:
print('Захожу на сайт')
with open('logs.txt' , 'a') as file:
file.write('[ ' + str(dt_now) + ' ]' + ' Бот заходит на ютуб' + '\n')
driver.set_window_position(9999999, 99999999)
driver.get('https://www.youtube.com/')
for cookie in pickle.load(open('cookies_to_youtube', 'rb')):
driver.add_cookie(cookie)
sleep(2)
driver.refresh()
#pickle.dump(driver.get_cookies(), open('cookies_to_youtube', 'wb'))
#driver.get('https://www.youtube.com/')
driver.implicitly_wait(5)
with open('logs.txt' , 'a') as file:
file.write('[ ' + str(dt_now) + ' ]' + ' Бот ищет видео по запросу ' + constipation + '\n')
search = driver.find_element_by_name('search_query')
search.clear()
search.send_keys(constipation)
sleep(1)
search.send_keys(Keys.ENTER)
sleep(3)
driver.implicitly_wait(5)
videos = driver.find_elements_by_class_name('text-wrapper')
for video in range(0, vd):
videos[video].click()
driver.implicitly_wait(5)
with open('logs.txt' , 'a') as file:
file.write('[ ' + str(dt_now) + ' ]' + ' Бот зашёл на видео: ' + driver.current_url + '\n')
sleep(5)
qcom = 0
#Send the keys to the input field
for comment in range(0, vdcomments):
driver.implicitly_wait(2)
if 20 < qcom:
qcom = 0
commentBox = driver.find_element_by_id('placeholder-area')
commentBox.click()
sleep(1)
inputBox =driver.find_element_by_id('contenteditable-root')
inputBox.send_keys('$')
sleep(0.5)
inputBox.send_keys(Keys.CONTROL + Keys.ENTER)
else:
commentBox = driver.find_element_by_id('placeholder-area')
commentBox.click()
sleep(1)
inputBox =driver.find_element_by_id('contenteditable-root')
inputBox.send_keys(comments_text)
sleep(0.5)
#driver.find_element_by_partial_link_text('КОММЕНТАРИЙ').click()
inputBox.send_keys(Keys.CONTROL + Keys.ENTER)
sleep(1.5)
qcom = qcom + 1
with open('logs.txt' , 'a') as file:
file.write('[ ' + str(dt_now) + ' ]' + ' Бот оставил комментарии' + '\n')
sleep(1)
driver.back()
sleep(1)
sleep(3)
print('Операция завершина успешно!')
except Exception as ex:
print(ex)
finally:
driver.close()
driver.quit()
message.from_user.first_name
даёт имя бота from fake_useragent import UserAgent
ua = UserAgent()
options.add_argument(f'user-agent={ua.google}')
driver = webdriver.Chrome(executable_path=PATH + "chromedriver",
options=options)