Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- 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 json
- import time
- import random
- def load_cookies(file_path):
- """Memuat cookies dari sebuah file JSON."""
- with open(file_path, 'r') as file:
- cookies = json.load(file)
- return cookies
- def load_comments(file_path):
- """Membaca komentar dari sebuah file teks."""
- with open(file_path, 'r', encoding='utf-8') as file:
- comments = file.readlines()
- return comments
- def initialize_driver(driver_path):
- """Inisialisasi WebDriver untuk browser Chrome."""
- driver = webdriver.Chrome(executable_path=driver_path)
- return driver
- def type_slowly(element, text, delay=0.1):
- """Mengirimkan teks secara lambat ke sebuah elemen web."""
- for char in text:
- element.send_keys(char)
- time.sleep(delay)
- def open_comment_box(driver):
- """Membuka kotak komentar pada video."""
- comment_buttons = WebDriverWait(driver, 10).until(
- EC.presence_of_all_elements_located((By.CLASS_NAME, 'x1b0d499'))
- )
- comment_buttons = [button for button in comment_buttons if 'xep6ejk' in button.get_attribute('class')]
- if len(comment_buttons) > 1:
- comment_buttons[1].click()
- time.sleep(10) # Menunggu bagian komentar dimuat
- return True
- else:
- print("Tombol komentar tidak ditemukan")
- return False
- def post_comment(driver, comment_text):
- """Memposting sebuah komentar pada video yang sedang ditonton."""
- comment_box = WebDriverWait(driver, 15).until(
- EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-label='Write a comment…'][contenteditable='true']"))
- )
- comment_box.click()
- type_slowly(comment_box, comment_text.strip())
- comment_box.send_keys(Keys.RETURN)
- time.sleep(random.uniform(3, 6))
- def navigate_to_next_video(driver):
- """Menavigasi ke video berikutnya dalam daftar video."""
- next_buttons = WebDriverWait(driver, 10).until(
- EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".x14yjl9h.xudhj91.x18nykt9.xww2gxu.x197sbye.x10l6tqk.xwa60dl.x1cb1t30.x1k90msu.x6o7n8i.x9lcvmn.x1so62im"))
- )
- if len(next_buttons) > 1:
- next_buttons[1].click() # Klik pada kemunculan kedua
- time.sleep(7) # Menunggu video berikutnya dimuat
- return True
- else:
- print("Tombol video berikutnya tidak ditemukan")
- return False
- def automate_comments(driver, cookies_file, comments_file):
- """Mengotomatisasi proses komentar pada Facebook Reels."""
- cookies = load_cookies(cookies_file)
- comments = load_comments(comments_file)
- try:
- # Buka Facebook
- driver.get('https://www.facebook.com/')
- time.sleep(5) # Menunggu halaman dimuat
- # Tambahkan cookies ke browser
- for cookie in cookies:
- if 'sameSite' in cookie:
- del cookie['sameSite']
- driver.add_cookie(cookie)
- # Perbarui halaman untuk menerapkan cookies
- driver.refresh()
- time.sleep(5) # Menunggu halaman dimuat dengan cookies
- # Navigasi ke URL reel
- driver.get('https://www.facebook.com/reel')
- time.sleep(5) # Menunggu halaman reel dimuat
- first_video = True
- for comment_text in comments:
- if first_video:
- if not open_comment_box(driver):
- break
- post_comment(driver, comment_text)
- first_video = False
- else:
- post_comment(driver, comment_text)
- if not navigate_to_next_video(driver):
- break
- print("Komentar berhasil diposting.")
- finally:
- # Tutup browser
- driver.quit()
- if __name__ == "__main__":
- driver_path = r'D:\Software\Belajar Python\autocomment\chromedriver-win64\chromedriver-win64\chromedriver.exe'
- cookies_file = 'cookies.json'
- comments_file = 'komen.txt'
- driver = initialize_driver(driver_path)
- automate_comments(driver, cookies_file, comments_file)
- Download chrome driver https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.60/win64/chromedriver-win64.zip
- Export cookie JSON file https://chrome.google.com/webstore/detail/nmckokihipjgplolmcmjakknndddifde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement