Advertisement
ghiwar

autokontraksi

Jun 10th, 2024
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. import json
  7. import time
  8. import random
  9.  
  10. def load_cookies(file_path):
  11.     """Memuat cookies dari sebuah file JSON."""
  12.     with open(file_path, 'r') as file:
  13.         cookies = json.load(file)
  14.     return cookies
  15.  
  16. def load_comments(file_path):
  17.     """Membaca komentar dari sebuah file teks."""
  18.     with open(file_path, 'r', encoding='utf-8') as file:
  19.         comments = file.readlines()
  20.     return comments
  21.  
  22. def initialize_driver(driver_path):
  23.     """Inisialisasi WebDriver untuk browser Chrome."""
  24.     driver = webdriver.Chrome(executable_path=driver_path)
  25.     return driver
  26.  
  27. def type_slowly(element, text, delay=0.1):
  28.     """Mengirimkan teks secara lambat ke sebuah elemen web."""
  29.     for char in text:
  30.         element.send_keys(char)
  31.         time.sleep(delay)
  32.  
  33. def open_comment_box(driver):
  34.     """Membuka kotak komentar pada video."""
  35.     comment_buttons = WebDriverWait(driver, 10).until(
  36.         EC.presence_of_all_elements_located((By.CLASS_NAME, 'x1b0d499'))
  37.     )
  38.     comment_buttons = [button for button in comment_buttons if 'xep6ejk' in button.get_attribute('class')]
  39.     if len(comment_buttons) > 1:
  40.         comment_buttons[1].click()
  41.         time.sleep(10)  # Menunggu bagian komentar dimuat
  42.         return True
  43.     else:
  44.         print("Tombol komentar tidak ditemukan")
  45.         return False
  46.  
  47. def post_comment(driver, comment_text):
  48.     """Memposting sebuah komentar pada video yang sedang ditonton."""
  49.     comment_box = WebDriverWait(driver, 15).until(
  50.         EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-label='Write a comment…'][contenteditable='true']"))
  51.     )
  52.     comment_box.click()
  53.     type_slowly(comment_box, comment_text.strip())
  54.     comment_box.send_keys(Keys.RETURN)
  55.     time.sleep(random.uniform(3, 6))
  56.  
  57. def navigate_to_next_video(driver):
  58.     """Menavigasi ke video berikutnya dalam daftar video."""
  59.     next_buttons = WebDriverWait(driver, 10).until(
  60.         EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".x14yjl9h.xudhj91.x18nykt9.xww2gxu.x197sbye.x10l6tqk.xwa60dl.x1cb1t30.x1k90msu.x6o7n8i.x9lcvmn.x1so62im"))
  61.     )
  62.     if len(next_buttons) > 1:
  63.         next_buttons[1].click()  # Klik pada kemunculan kedua
  64.         time.sleep(7)  # Menunggu video berikutnya dimuat
  65.         return True
  66.     else:
  67.         print("Tombol video berikutnya tidak ditemukan")
  68.         return False
  69.  
  70. def automate_comments(driver, cookies_file, comments_file):
  71.     """Mengotomatisasi proses komentar pada Facebook Reels."""
  72.     cookies = load_cookies(cookies_file)
  73.     comments = load_comments(comments_file)
  74.  
  75.     try:
  76.         # Buka Facebook
  77.         driver.get('https://www.facebook.com/')
  78.         time.sleep(5)  # Menunggu halaman dimuat
  79.  
  80.         # Tambahkan cookies ke browser
  81.         for cookie in cookies:
  82.             if 'sameSite' in cookie:
  83.                 del cookie['sameSite']
  84.             driver.add_cookie(cookie)
  85.  
  86.         # Perbarui halaman untuk menerapkan cookies
  87.         driver.refresh()
  88.         time.sleep(5)  # Menunggu halaman dimuat dengan cookies
  89.  
  90.         # Navigasi ke URL reel
  91.         driver.get('https://www.facebook.com/reel')
  92.         time.sleep(5)  # Menunggu halaman reel dimuat
  93.  
  94.         first_video = True
  95.  
  96.         for comment_text in comments:
  97.             if first_video:
  98.                 if not open_comment_box(driver):
  99.                     break
  100.                 post_comment(driver, comment_text)
  101.                 first_video = False
  102.             else:
  103.                 post_comment(driver, comment_text)
  104.  
  105.             if not navigate_to_next_video(driver):
  106.                 break
  107.  
  108.         print("Komentar berhasil diposting.")
  109.  
  110.     finally:
  111.         # Tutup browser
  112.         driver.quit()
  113.  
  114. if __name__ == "__main__":
  115.     driver_path = r'D:\Software\Belajar Python\autocomment\chromedriver-win64\chromedriver-win64\chromedriver.exe'
  116.     cookies_file = 'cookies.json'
  117.     comments_file = 'komen.txt'
  118.  
  119.     driver = initialize_driver(driver_path)
  120.     automate_comments(driver, cookies_file, comments_file)
  121.  
  122.  
  123. Download chrome driver https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.60/win64/chromedriver-win64.zip
  124. Export cookie JSON file https://chrome.google.com/webstore/detail/nmckokihipjgplolmcmjakknndddifde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement