Advertisement
sourav8256

Untitled

Aug 26th, 2023 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 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. import time
  5. import os
  6. import pickle
  7.  
  8. class TwitterBot:
  9. def __init__(self):
  10. self.driver = webdriver.Chrome() # You can use any browser driver you prefer
  11. self.base_url = "https://twitter.com"
  12. self.cookie_filename = "twitter_cookies.pkl"
  13.  
  14. def login(self):
  15. self.driver.get(self.base_url)
  16.  
  17. if os.path.exists(self.cookie_filename):
  18. self.load_cookies()
  19. else:
  20. input("Please log in manually and press Enter once you're on the home page...")
  21. self.save_cookies()
  22.  
  23. def load_cookies(self):
  24. with open(self.cookie_filename, 'rb') as f:
  25. cookies = pickle.load(f)
  26. for cookie in cookies:
  27. self.driver.add_cookie(cookie)
  28.  
  29. self.driver.get(self.base_url)
  30.  
  31. def save_cookies(self):
  32. cookies = self.driver.get_cookies()
  33. with open(self.cookie_filename, 'wb') as f:
  34. pickle.dump(cookies, f)
  35.  
  36. def post_tweet(self, text, media_paths=[]):
  37. self.driver.get(self.base_url)
  38.  
  39. tweet_box = self.driver.find_element(By.XPATH, '//div[@role="textbox"]')
  40. tweet_box.send_keys(text)
  41.  
  42. if media_paths:
  43. media_button = self.driver.find_element(By.XPATH, '//input[@data-testid="fileInput"]')
  44. for media_path in media_paths:
  45. media_path = os.path.abspath(media_path)
  46. media_button.send_keys(media_path)
  47.  
  48. tweet_button = self.driver.find_element(By.XPATH, '//div[@data-testid="tweetButtonInline"]')
  49. tweet_button.click()
  50.  
  51. def close(self):
  52. self.driver.quit()
  53.  
  54. if __name__ == "__main__":
  55. bot = TwitterBot()
  56.  
  57. try:
  58. bot.login()
  59.  
  60. media_folder = "media_folder" # Replace with the actual folder containing media files
  61.  
  62. media_files = [f for f in os.listdir(media_folder) if os.path.isfile(os.path.join(media_folder, f)) and not f.endswith("posted")]
  63.  
  64. for media_file in media_files:
  65. media_path = os.path.join(media_folder, media_file)
  66.  
  67. # Read tweet text from corresponding .txt file
  68. tweet_text_path = os.path.join(media_folder, f"{os.path.splitext(media_file)[0]}.txt")
  69. if os.path.exists(tweet_text_path):
  70. with open(tweet_text_path, "r") as tweet_file:
  71. tweet_text = tweet_file.read().strip()
  72. else:
  73. tweet_text = "Default tweet text" # Provide a default if no .txt file is found
  74.  
  75. bot.post_tweet(tweet_text, [media_path])
  76.  
  77. # Rename the file to indicate that it's been posted
  78. new_media_path = os.path.join(media_folder, media_file.replace(".","_posted."))
  79. os.rename(media_path, new_media_path)
  80.  
  81. time.sleep(5) # Adjust as needed
  82.  
  83. except Exception as e:
  84. print("An error occurred:", e)
  85.  
  86. finally:
  87. bot.close()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement