1
1
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
4
4
from selenium .webdriver .common .by import By
5
5
from selenium .webdriver .common .keys import Keys
6
6
from selenium .webdriver .support .ui import WebDriverWait
7
7
from selenium .webdriver .support import expected_conditions as EC
8
+ from selenium .common .exceptions import NoSuchElementException , TimeoutException
8
9
import time
9
10
import os
10
11
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 '
14
15
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")
19
24
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 )
22
28
23
29
# File to store the last sent tweet content
24
30
last_sent_file = 'telegram_last_sent_tweet.txt'
25
31
26
32
# Function to read the last sent tweet content from a file
27
33
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 } " )
31
40
return None
32
41
33
42
# Function to write the latest tweet content to a file
34
43
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 ())
37
54
38
55
while True :
39
56
try :
40
- # Open the X.com (formerly Twitter) profile page
57
+ # Open the X.com (formerly Twitter) profile page of ghazayel
41
58
driver .get ('https://x.com/ghazayel' )
42
59
43
60
# Wait for the page to load completely
@@ -55,31 +72,55 @@ def write_last_sent_tweet(content):
55
72
if tweet_content == last_sent_tweet :
56
73
print ("Tweet already sent. Waiting for the next check..." )
57
74
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
+
58
87
# Open the Telegram channel
59
88
driver .get ('https://web.telegram.org/k/#@ghazayel' )
60
89
61
90
# Wait for the Telegram Web channel to load
62
91
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")]' )
66
92
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
+
67
101
# Clear any existing text (optional)
68
102
input_box .clear ()
69
103
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 )
72
111
73
- # Simulate pressing Enter to send the message
74
- input_box .send_keys (Keys .RETURN )
75
112
76
113
# Update the last sent tweet content in the file
77
114
write_last_sent_tweet (tweet_content )
78
115
79
116
print ("Tweet sent successfully!" )
80
117
118
+ except (NoSuchElementException , TimeoutException ) as e :
119
+ print (f"Element not found or timed out: { e } " )
81
120
except Exception as e :
82
121
print (f"An error occurred: { e } " )
122
+ finally :
123
+ pass
83
124
84
125
# Wait for 2 minutes before running the script again
85
126
time .sleep (120 )
0 commit comments