فهرست منبع

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/kw_tools

Jared 2 سال پیش
والد
کامیت
9e743f4ee6
4فایلهای تغییر یافته به همراه222 افزوده شده و 0 حذف شده
  1. 53 0
      SEO/fb_comment.py
  2. 7 0
      youtube/run_yt.py
  3. 123 0
      youtube/yt_comment.py
  4. 39 0
      youtube/yt_video_collect.py

+ 53 - 0
SEO/fb_comment.py

@@ -0,0 +1,53 @@
+from selenium.webdriver.common.keys import Keys
+from selenium import webdriver
+from selenium.webdriver.common.by import By
+from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
+import time
+import redis
+import json
+import numpy as np
+
+account = ['enjoylisteningswift@yandex.ru']
+pd = ['']
+path = '/Users/zooeytsai/Downloads/chromedriver 2'
+post_url = ['']
+
+
+def send_comment(text):
+    s = Service(path)
+    driver = webdriver.Chrome(service=s)
+    driver.get('https://www.facebook.com/')
+    time.sleep(5)
+    a = driver.find_element(By.ID,"email")
+    p = driver.find_element(By.ID,"pass")
+    a.send_keys(account[0])
+    p.send_keys(pd[0])
+    time.sleep(3)
+    login = driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div/div[1]/form/div[2]/button')
+    login.click()
+    time.sleep(10)
+    driver.get(post_url[0])
+    time.sleep(5)
+    # comment = driver.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div[1]/div[5]/div/div[2]/div[2]/div[1]/form/div/div/div[1]/p').send_keys(text)
+    comment = driver.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div[1]/div[5]/div/div/div[2]/div[1]/form/div/div/div[1]/p')
+    comment.send_keys(text)
+    driver.implicitly_wait(5)
+    comment.send_keys(Keys.ENTER)
+    driver.implicitly_wait(5)
+    driver.quit()
+
+    
+def random_comment():
+    r = redis.Redis(host='db.ptt.cx', port=6379, db=0, password='choozmo9')
+    js = r.get('yt_comment')
+    messages = json.loads(js)
+    # ===============================
+    r = np.random.randint(0, len(messages))
+    print(messages[r])
+    return messages[r]
+
+
+if __name__ == "__main__":
+    send_comment(random_comment())

+ 7 - 0
youtube/run_yt.py

@@ -0,0 +1,7 @@
+import random
+import os
+
+profile_file = ['Profile 20', 'Profile 25']
+p = random.choice(profile_file)
+
+os.system('python yt_comment.py get --profile="'+p+'"')

+ 123 - 0
youtube/yt_comment.py

@@ -0,0 +1,123 @@
+import time
+import numpy as np
+import pandas as pd
+import requests
+import os
+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
+import redis
+import json
+import random
+import fire
+
+
+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(f"profile-directory={profile_file}")
+    op.add_experimental_option("excludeSwitches", ["enable-automation"])
+    op.add_experimental_option('useAutomationExtension', False)
+    driver = webdriver.Chrome(options=op,
+                              executable_path=r'C:\Users\Administrator\Downloads\chromedriver_win32 (4)\chromedriver')
+    # 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()
+    return driver
+
+
+def comment_page(driver, url, comment):
+    if len(url) == 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, 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, url, random_comment())
+    
+    if check_exists_by_xpath(driver, '//*[@id="contents"]/ytd-message-renderer'):
+        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")))
+    
+    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)
+    driver.quit()
+    return comment_page(driver, url, random_comment())
+
+
+def random_comment():
+    # You can edit these lines=======
+    # df = pd.read_excel('/Users/zooeytsai/Downloads/yt留言.xlsx')
+    # messages = df['文字']
+    r = redis.Redis(host='db.ptt.cx', port=6379, db=0, password='choozmo9')
+    js = r.get('yt_comment')
+    messages = json.loads(js)
+    # ===============================
+    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
+
+
+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(profile)
+        comment_page(driver, url, random_comment())
+        
+
+if __name__ == '__main__':
+    fire.Fire(JParams)

+ 39 - 0
youtube/yt_video_collect.py

@@ -0,0 +1,39 @@
+import requests
+import json
+import redis
+
+api_key = 'AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U'
+
+
+def collect_video():
+    all_video_id = []
+    video_list = f"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key={api_key}&maxResults=50"
+    r = requests.get(video_list)
+    res = json.loads(r.content)
+    next_page = res['nextPageToken']
+    for i in res['items']:
+        all_video_id.append(i['snippet']['resourceId']['videoId'])
+    while True:
+        next_video_list = f"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key={api_key}&maxResults=50&pageToken={next_page}"
+        r = requests.get(next_video_list)
+        res = json.loads(r.content)
+        for i in res['items']:
+            all_video_id.append(i['snippet']['resourceId']['videoId'])
+        try:
+            next_page = res['nextPageToken']
+        except:
+            break
+    print(len(all_video_id))
+    r = redis.Redis(host='db.ptt.cx', port=6379, db=0,password='choozmo9')
+    js = json.dumps(all_video_id)
+    r.set('yt_video',js)
+    
+    
+if __name__ == '__main__':
+    collect_video()
+    
+    "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U&maxResults=50&pageToken=EAAaBlBUOkNESQ"
+
+"https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=UC2Qda9PhJWuiMTCZ-j7K_hg&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U"
+"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U&maxResults=50"
+'''觀看率'''"https://www.googleapis.com/youtube/v3/videos?part=statistics&id=eFZckOTi24c&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U"