Advertisement
gur111

Whatsapp Binary Counter Bot

Nov 24th, 2020 (edited)
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # Import required packages
  2. from selenium import webdriver
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.common.by import By
  7. from selenium.common.exceptions import TimeoutException
  8. import pickle
  9. import time
  10. import os
  11.  
  12. # Driver to open a browser
  13. from selenium.webdriver.chrome.options import Options
  14. chrome_options = Options()
  15. chrome_options.add_argument("user-data-dir=selenium")
  16. driver = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
  17.  
  18. #link to open a site
  19. driver.get("https://web.whatsapp.com/")
  20.  
  21. # 10 sec wait time to load, if good internet connection is not good then increase the time
  22. # units in seconds
  23. # note this time is being used below also
  24. wait = WebDriverWait(driver, 10)
  25. wait5 = WebDriverWait(driver, 5)
  26. time.sleep(10)
  27. try:
  28.     wait5.until(EC.presence_of_element_located((
  29.                             By.CLASS_NAME, "landing-title"
  30.                         )))
  31. except TimeoutException:
  32.     print("No need to log in (we think)")
  33.     driver.find_element(By.XPATH, '//span[text()="סופרים עד אינסוף - בינארי"]').click()
  34. else:
  35.     input("Scan the QR code and then press Enter")
  36.  
  37. # if os.path.isfile('cookies.pkl'):
  38. #     with open('cookies.pkl', 'rb') as f:
  39. #         cookies = pickle.load(f)
  40. #     for cookie in cookies:
  41. #         driver.add_cookie(cookie)
  42. #     if not cookies:
  43. #         input("Scan the QR code and then press Enter")
  44. #         with open('cookies.pkl', 'wb') as f:
  45. #             pickle.dump( driver.get_cookies(), f)    
  46. # else:
  47. #     input("Scan the QR code and then press Enter")
  48. #     pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
  49.  
  50.  
  51. LAST_NUM = -1
  52.  
  53. def send_message():
  54.     global LAST_NUM
  55.  
  56.     # Click on input box
  57.     inp_xpath = "//div[@contenteditable='true']"
  58.     inputs = driver.find_elements_by_xpath(inp_xpath)
  59.     time.sleep(1)
  60.  
  61.     # Calculate next number
  62.     all_msgs = driver.find_elements_by_xpath("//span[contains(@class, 'selectable-text') and contains(@class, 'copyable-text') and contains(@class, 'invisible-space')]")
  63.     out_msgs = driver.find_elements_by_class_name('message-out')
  64.  
  65.     if not out_msgs:
  66.         last_out = False
  67.     else:
  68.         last_out = out_msgs[-1]
  69.  
  70.     pured_msgs = [x.text for x in all_msgs if not last_out or out_msgs[-1].location['y'] < x.location['y'] and x.text not in out_msgs[-1].text]
  71.  
  72.     for msg in reversed(pured_msgs):
  73.         try:
  74.             sanitized = msg.replace(' ', '').replace('-', '')
  75.             if sanitized == '=x':
  76.                 print(f'Current num is {hex(LAST_NUM)}')
  77.                 inputs[1].send_keys(f'{hex(LAST_NUM)}')
  78.                 inputs[1].send_keys(Keys.ENTER)
  79.             elif sanitized == '=d':
  80.                 print(f'Current num is {LAST_NUM}')
  81.                 inputs[1].send_keys(f'{LAST_NUM}')
  82.                 inputs[1].send_keys(Keys.ENTER)
  83.             elif sanitized == '=o':
  84.                 print(f'Current num is {LAST_NUM}')
  85.                 inputs[1].send_keys(f'{oct(LAST_NUM)}')
  86.                 inputs[1].send_keys(Keys.ENTER)
  87.             else:
  88.                 bin_num = int(sanitized, base=2)
  89.                 break
  90.         except ValueError:
  91.             print(f'{msg} is not a binary')
  92.     else:
  93.         print('No bin number found')
  94.         return
  95.    
  96.     next_num = bin_num+1
  97.     LAST_NUM = next_num
  98.     inputs[1].send_keys(f'{bin(next_num)[2:]}')
  99.     inputs[1].send_keys(Keys.ENTER)
  100.  
  101. while True:
  102.     try:
  103.         send_message()
  104.     except:
  105.         pass
  106.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement