Skip to content

Commit 17ba320

Browse files
authored
Update x2telegram.py
few changes
1 parent 6912d68 commit 17ba320

File tree

1 file changed

+65
-24
lines changed

1 file changed

+65
-24
lines changed

x2telegram.py

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
11
from selenium import webdriver
2-
from selenium.webdriver.chrome.service import Service
3-
from selenium.webdriver.chrome.options import Options
2+
from selenium.webdriver.edge.service import Service
3+
from selenium.webdriver.edge.options import Options
44
from selenium.webdriver.common.by import By
55
from selenium.webdriver.common.keys import Keys
66
from selenium.webdriver.support.ui import WebDriverWait
77
from selenium.webdriver.support import expected_conditions as EC
8+
from selenium.common.exceptions import NoSuchElementException, TimeoutException
89
import time
910
import os
1011

11-
# Define the path to your Chrome user data directory and the profile name
12-
user_data_dir = '/path/to/your/chrome/user/data'
13-
profile_dir = 'Profile 2'
12+
# Specify the user data directory and profile directory (FIX USERNAME)
13+
user_data_dir = r'C:\Users\USERNAME\AppData\Local\Microsoft\Edge\User Data'
14+
profile_dir = 'Profile 1'
1415

15-
# Define Chrome options with the user data directory and profile directory
16-
chrome_options = Options()
17-
chrome_options.add_argument(f"user-data-dir={user_data_dir}")
18-
chrome_options.add_argument(f"profile-directory={profile_dir}")
16+
edge_options = Options()
17+
edge_options.add_argument(f"user-data-dir={user_data_dir}")
18+
edge_options.add_argument(f"profile-directory={profile_dir}")
19+
# edge_options.add_argument("--disable-gpu") # Disable GPU acceleration
20+
# edge_options.add_argument("--headless") # Run in headless mode (optional)
21+
# edge_options.add_argument("--remote-debugging-port=9222")
22+
# edge_options.add_argument("--no-sandbox")
23+
# edge_options.add_argument("--disable-dev-shm-usage")
1924

20-
# Initialize the Chrome WebDriver with the specified options
21-
driver = webdriver.Chrome(options=chrome_options)
25+
26+
# Initialize the Edge WebDriver with options INSTALL DRIVER
27+
driver = webdriver.Edge(service=Service('C:/edgedriver/msedgedriver.exe'), options=edge_options)
2228

2329
# File to store the last sent tweet content
2430
last_sent_file = 'telegram_last_sent_tweet.txt'
2531

2632
# Function to read the last sent tweet content from a file
2733
def read_last_sent_tweet():
28-
if os.path.exists(last_sent_file):
29-
with open(last_sent_file, 'r', encoding='utf-8') as file:
30-
return file.read().strip()
34+
try:
35+
if os.path.exists(last_sent_file):
36+
with open(last_sent_file, 'r', encoding='utf-8') as file:
37+
return file.read().strip()
38+
except Exception as e:
39+
print(f"Error reading last sent tweet: {e}")
3140
return None
3241

3342
# Function to write the latest tweet content to a file
3443
def write_last_sent_tweet(content):
35-
with open(last_sent_file, 'w', encoding='utf-8') as file:
36-
file.write(content)
44+
try:
45+
with open(last_sent_file, 'w', encoding='utf-8') as file:
46+
file.write(content)
47+
except Exception as e:
48+
print(f"Error writing last sent tweet: {e}")
49+
50+
# Remove specific word from sentence
51+
def remove_word(sentence, word_to_remove):
52+
updated_sentence = sentence.replace(word_to_remove, '')
53+
return ' '.join(updated_sentence.split())
3754

3855
while True:
3956
try:
40-
# Open the X.com (formerly Twitter) profile page
57+
# Open the X.com (formerly Twitter) profile page of ghazayel
4158
driver.get('https://x.com/ghazayel')
4259

4360
# Wait for the page to load completely
@@ -55,31 +72,55 @@ def write_last_sent_tweet(content):
5572
if tweet_content == last_sent_tweet:
5673
print("Tweet already sent. Waiting for the next check...")
5774
else:
75+
sentence = tweet_content
76+
word_to_remove = ['عاجل |', 'عاجل|']
77+
78+
for word in word_to_remove:
79+
if word in sentence:
80+
new_tweet_content = remove_word(sentence, word)
81+
break
82+
else:
83+
new_tweet_content = sentence
84+
85+
new_tweet_content = new_tweet_content.replace('_', ' ').replace('#', '')
86+
5887
# Open the Telegram channel
5988
driver.get('https://web.telegram.org/k/#@ghazayel')
6089

6190
# Wait for the Telegram Web channel to load
6291
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//span[text()="Broadcast"]')))
63-
64-
# Find the message input box that is associated with the placeholder "Broadcast"
65-
input_box = driver.find_element(By.XPATH, '//span[text()="Broadcast"]/ancestor::div[contains(@class, "input-message-container")]/div[contains(@class, "input-message-input")]')
6692

93+
# Try to find the message input box associated with the placeholder "Broadcast"
94+
try:
95+
input_box = driver.find_element(By.XPATH, '//span[text()="Broadcast"]/ancestor::div[contains(@class, "input-message-container")]/div[contains(@class, "input-message-input")]')
96+
except NoSuchElementException:
97+
# Handle the case where "Broadcast" is not found
98+
print("Broadcast element not found. Trying to find the input box without it.")
99+
input_box = driver.find_element(By.XPATH, '//div[contains(@class, "input-message-container")]/div[contains(@class, "input-message-input")]')
100+
67101
# Clear any existing text (optional)
68102
input_box.clear()
69103

70-
# Find the message input box within that context
71-
input_box.send_keys(tweet_content)
104+
# Send the new tweet content
105+
# input_box.send_keys(new_tweet_content)
106+
# input_box.send_keys(Keys.RETURN)
107+
108+
# new code here
109+
driver.execute_script("arguments[0].innerText = arguments[1];", input_box, new_tweet_content)
110+
input_box.send_keys(Keys.ENTER)
72111

73-
# Simulate pressing Enter to send the message
74-
input_box.send_keys(Keys.RETURN)
75112

76113
# Update the last sent tweet content in the file
77114
write_last_sent_tweet(tweet_content)
78115

79116
print("Tweet sent successfully!")
80117

118+
except (NoSuchElementException, TimeoutException) as e:
119+
print(f"Element not found or timed out: {e}")
81120
except Exception as e:
82121
print(f"An error occurred: {e}")
122+
finally:
123+
pass
83124

84125
# Wait for 2 minutes before running the script again
85126
time.sleep(120)

0 commit comments

Comments
 (0)