|
@@ -0,0 +1,115 @@
|
|
|
+import time
|
|
|
+import numpy as np
|
|
|
+import pandas as pd
|
|
|
+import requests
|
|
|
+import os
|
|
|
+from bs4 import BeautifulSoup
|
|
|
+from selenium import webdriver
|
|
|
+from selenium.webdriver.common.by import By
|
|
|
+from selenium.webdriver.support.ui import WebDriverWait
|
|
|
+from selenium.webdriver.support import expected_conditions as EC
|
|
|
+from selenium.webdriver.common.keys import Keys
|
|
|
+from selenium.common.exceptions import NoSuchElementException
|
|
|
+from selenium_stealth import stealth
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def open_driver():
|
|
|
+ op = webdriver.ChromeOptions()
|
|
|
+ # op.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
|
|
|
+ # op.add_argument('--headless')
|
|
|
+ op.add_argument('--disable-dev-shm-usage')
|
|
|
+ op.add_argument('--no-sandbox')
|
|
|
+ op.add_argument('user-data-dir=/Users/zooeytsai/Library/Application Support/Google/Chrome/')
|
|
|
+ op.add_argument('profile-directory=Profile 3')
|
|
|
+ op.add_experimental_option("excludeSwitches", ["enable-automation"])
|
|
|
+ op.add_experimental_option('useAutomationExtension', False)
|
|
|
+ driver = webdriver.Chrome(options=op,executable_path=r'/Users/zooeytsai/Downloads/chromedriver 2')
|
|
|
+ # stealth(driver,
|
|
|
+ # languages=["en-US", "en"],
|
|
|
+ # vendor="Google Inc.",
|
|
|
+ # platform="Win32",
|
|
|
+ # webgl_vendor="Intel Inc.",
|
|
|
+ # renderer="Intel Iris OpenGL Engine",
|
|
|
+ # fix_hairline=True,
|
|
|
+ # )
|
|
|
+ time.sleep(5)
|
|
|
+ # driver.get("https://www.youtube.com/watch?v=eFZckOTi24c")
|
|
|
+
|
|
|
+ # driver.find_element(By.XPATH,'// *[ @ id = "openid-buttons"] / button[1]').click()
|
|
|
+ time.sleep(1000)
|
|
|
+ return driver
|
|
|
+
|
|
|
+
|
|
|
+def comment_page(driver, urls, comment):
|
|
|
+ if len(urls) == 0:
|
|
|
+ print('Youtube Comment Bot: Finished!')
|
|
|
+ return []
|
|
|
+
|
|
|
+ url = urls.pop()
|
|
|
+
|
|
|
+ driver.get(url)
|
|
|
+ print(url)
|
|
|
+ driver.implicitly_wait(1)
|
|
|
+
|
|
|
+ if not check_exists_by_xpath(driver, '//*[@id="movie_player"]'):
|
|
|
+ return comment_page(driver, urls, random_comment())
|
|
|
+ time.sleep(4)
|
|
|
+ driver.execute_script("window.scrollTo(0, 600);")
|
|
|
+
|
|
|
+ if not check_exists_by_xpath(driver, '//*[@id="simple-box"]/ytd-comment-simplebox-renderer'):
|
|
|
+ return comment_page(driver, urls, random_comment())
|
|
|
+
|
|
|
+ if check_exists_by_xpath(driver, '//*[@id="contents"]/ytd-message-renderer'):
|
|
|
+ return comment_page(driver, urls, random_comment())
|
|
|
+
|
|
|
+ WebDriverWait(driver, 20).until(
|
|
|
+ EC.presence_of_element_located((By.CSS_SELECTOR, "ytd-comments ytd-comment-simplebox-renderer")))
|
|
|
+
|
|
|
+ driver.find_element_by_css_selector("ytd-comments ytd-comment-simplebox-renderer div#placeholder-area").click()
|
|
|
+ driver.implicitly_wait(5)
|
|
|
+ driver.find_element_by_xpath('//*[@id="contenteditable-root"]').send_keys(comment)
|
|
|
+ driver.find_element_by_xpath('//*[@id="contenteditable-root"]').send_keys(Keys.CONTROL, Keys.ENTER)
|
|
|
+
|
|
|
+ post = WebDriverWait(driver, 15).until(
|
|
|
+ EC.element_to_be_clickable((By.CSS_SELECTOR, 'ytd-comments ytd-comment-simplebox-renderer'))
|
|
|
+ )
|
|
|
+ post.click()
|
|
|
+
|
|
|
+ r = np.random.randint(2, 5)
|
|
|
+ time.sleep(r)
|
|
|
+
|
|
|
+ return comment_page(driver, urls, random_comment())
|
|
|
+
|
|
|
+
|
|
|
+def random_comment():
|
|
|
+ # You can edit these lines=======
|
|
|
+ df = pd.read_excel('/Users/zooeytsai/Downloads/yt留言.xlsx')
|
|
|
+ messages = df['文字']
|
|
|
+ # ===============================
|
|
|
+ r = np.random.randint(0, len(messages))
|
|
|
+ print(messages[r])
|
|
|
+ return messages[r]
|
|
|
+
|
|
|
+
|
|
|
+def check_exists_by_xpath(driver, xpath):
|
|
|
+ try:
|
|
|
+ driver.find_element_by_xpath(xpath)
|
|
|
+ except NoSuchElementException:
|
|
|
+ return False
|
|
|
+
|
|
|
+ return True
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ urls = [
|
|
|
+ 'https://www.youtube.com/watch?v=eFZckOTi24c',
|
|
|
+ ]
|
|
|
+
|
|
|
+ # inp = open("url.txt", "r")
|
|
|
+ # for line in inp.readlines():
|
|
|
+ # urls.append(line)
|
|
|
+
|
|
|
+ driver = open_driver()
|
|
|
+ comment_page(driver, urls, random_comment())
|