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())