Skip to content

Instantly share code, notes, and snippets.

@south1907
Created June 20, 2020 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save south1907/7e8e2554d28441f5b1b9e1b2e96c2263 to your computer and use it in GitHub Desktop.
Save south1907/7e8e2554d28441f5b1b9e1b2e96c2263 to your computer and use it in GitHub Desktop.
Process main chat song messenger facebook by selenium
#!usr/bin/python3
# created by namph
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.keys import Keys
import time
import json
import os
email = 'email1'
password = 'passowrd1'
email2 = 'email2'
password2 = 'password2'
browser = Chrome()
browser2 = Chrome()
url = 'https://www.facebook.com/'
browser.get(url)
browser2.get(url)
email_input = browser.find_element_by_xpath('//input[@type="email"]')
email_input.send_keys(email)
pass_input = browser.find_element_by_xpath('//input[@type="password"]')
pass_input.send_keys(password)
email_input2 = browser2.find_element_by_xpath('//input[@type="email"]')
email_input2.send_keys(email2)
pass_input2 = browser2.find_element_by_xpath('//input[@type="password"]')
pass_input2.send_keys(password2)
pass_input.send_keys(Keys.RETURN)
pass_input2.send_keys(Keys.RETURN)
time.sleep(5)
url_mes = 'https://www.messenger.com'
url_mes_account_1 = 'https://www.messenger.com/t/100037068474173'
url_mes_account_2 = 'https://www.messenger.com/t/100052278181234'
browser.get(url_mes)
browser2.get(url_mes)
while True:
try:
submit_input = browser.find_element_by_xpath('//button[@type="submit"]')
submit_input.click()
break
except Exception as e:
print('loi roi')
while True:
try:
submit_input2 = browser2.find_element_by_xpath('//button[@type="submit"]')
submit_input2.click()
break
except Exception as e:
print('loi roi')
browser.get(url_mes_account_2)
browser2.get(url_mes_account_1)
text_input = browser.find_element_by_xpath('//div[@role="combobox"]')
text_input2 = browser2.find_element_by_xpath('//div[@role="combobox"]')
with open('banhmikhong.json', 'r', encoding="utf-8") as f:
song = json.loads(f.read())
current_time = 0
end_time_word = 0
# cần lưu lại thời gian xử lý bị chênh nếu thời gian xử lý send_keys nhiều hơn thời gian nhạc của từ đó
# và sẽ bù vào từ tiếp theo (nếu từ tiếp theo có thời gian nghỉ nhiều)
time_not_enough = 0
count = 0
print('init success')
# sau khi login các kiểu xong, cần chat start và nhấn Enter (thực ra chat gì cũng được) và đồng thời nhấn nút play nhạc đúng đoạn bắt đầu hát (chờ nhạc, bắt đầu hát thì nhấn enter)
check_done = input("chat 'start' to start\n")
for line in song['data']:
if 'change' in line:
count += 1
words = line['words']
if count % 2 == 0:
current_input = text_input
else:
current_input = text_input2
start_time_line = time.time()
for w in words:
# logic giống phần trước, chỉ khác thay vì thời gian delay cố định, thì cần phải tính lại thời gian xử lý và bù trừ lại
# thay vì in ra terminal thì cần sử dụng Selenium để send_keys vào input chat của trình duyệt
start_time_word = w['startTime']
if end_time_word != 0:
sleep_word = (start_time_word - end_time_word) / 1000 - time_not_enough
if sleep_word < 0:
time_not_enough = -sleep_word
else:
time_not_enough = 0
time.sleep(sleep_word)
end_time_word = w['endTime']
word = w['data']
start_time = time.time()
time_sleep = end_time_word - start_time_word
time_sleep = time_sleep/1000
if time_sleep < 0:
time_sleep = 0
current_input.send_keys(word + " ")
end_time = time.time()
total_time = end_time - start_time
diff_time = time_sleep - total_time - time_not_enough
if diff_time < 0:
time_not_enough = -diff_time
else:
time_not_enough = 0
time.sleep(diff_time)
end_time_line = time.time()
total_time_line = end_time_line - start_time_line
# bù thời gian nhấn phím Enter
start_time = time.time()
current_input.send_keys(Keys.RETURN)
end_time = time.time()
time_not_enough += (end_time - start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment