zooeytsai 2 лет назад
Родитель
Сommit
c2b54009d2
2 измененных файлов с 22 добавлено и 20 удалено
  1. 22 20
      youtube/yt_comment.py
  2. 0 0
      youtube/yt_video_collect.py

+ 22 - 20
SEO/yt_comment.py → youtube/yt_comment.py

@@ -13,18 +13,17 @@ from selenium_stealth import stealth
 import redis
 import json
 import random
+import fire
 
-profile_file = ['Profile 20', 'Profile 25']
 
-
-def open_driver():
+def open_driver(profile_file):
     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(r'user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data')
-    op.add_argument('profile-directory=Profile 20')
+    op.add_argument(f"profile-directory={profile_file}")
     op.add_experimental_option("excludeSwitches", ["enable-automation"])
     op.add_experimental_option('useAutomationExtension', False)
     driver = webdriver.Chrome(options=op,
@@ -44,7 +43,7 @@ def open_driver():
 
 
 def comment_page(driver, url, comment):
-    if len(urls) == 0:
+    if len(url) == 0:
         print('Youtube Comment Bot: Finished!')
         return []
     
@@ -55,15 +54,15 @@ def comment_page(driver, url, comment):
     driver.implicitly_wait(1)
     
     if not check_exists_by_xpath(driver, '//*[@id="movie_player"]'):
-        return comment_page(driver, urls, random_comment())
+        return comment_page(driver, url, 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())
+        return comment_page(driver, url, random_comment())
     
     if check_exists_by_xpath(driver, '//*[@id="contents"]/ytd-message-renderer'):
-        return comment_page(driver, urls, random_comment())
+        return comment_page(driver, url, random_comment())
     
     WebDriverWait(driver, 20).until(
         EC.presence_of_element_located((By.CSS_SELECTOR, "ytd-comments ytd-comment-simplebox-renderer")))
@@ -106,16 +105,19 @@ def check_exists_by_xpath(driver, xpath):
     return True
 
 
-if __name__ == '__main__':
-    r = redis.Redis(host='db.ptt.cx', port=6379, db=0, password='choozmo9')
-    js = r.get('yt_video')
-    jsobj = json.loads(js)
-    videoId = random.choice(jsobj)
-    url = f"https://www.youtube.com/watch?v={videoId}"
-    
-    # inp = open("url.txt", "r")
-    # for line in inp.readlines():
-    #     urls.append(line)
+class JParams(object):
+
+    def get(self, profile):
+        print(profile)
+        r = redis.Redis(host='db.ptt.cx', port=6379, db=0, password='choozmo9')
+        js = r.get('yt_video')
+        jsobj = json.loads(js)
+        videoId = random.choice(jsobj)
+        url = f"https://www.youtube.com/watch?v={videoId}"
     
-    driver = open_driver()
-    comment_page(driver, url, random_comment())
+        driver = open_driver(profile)
+        comment_page(driver, url, random_comment())
+        
+
+if __name__ == '__main__':
+    fire.Fire(JParams)

+ 0 - 0
SEO/yt_video_collect.py → youtube/yt_video_collect.py