-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageLiker.py
293 lines (163 loc) · 8.37 KB
/
messageLiker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import time
import telebot
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import undetected_chromedriver as uc
from sleepRandomTime import sleepRand
chatIdTG = "ENTER YOUR TELEGRAM CHAT ID HERE"
botTokenTG = """ENTER YOUR TELEGRAM BOT API TOKEN HERE"""
browserProfileName = "ENTER YOUR BROWSER PROFILE NAME HERE"
browserProfilePath = "ENTER YOUR BROWSER PROFILE PATH HERE"
browserLanguage = "ENTER YOUR BROWSER LANGUAGE HERE"
browserUserAgent = "ENTER YOUR BROWSER USER AGENT HERE"
driverExecutablePath = "driver/chromedriver"
chatURLTG = "ENTER YOUR TELEGRAM WEB CHAT URL HERE"
emojiDataDocId = "ENTER YOUR REACTION EMOJI ID HERE"
whiteListedText = "ENTER YOUR WHITELISTED TEXT HERE"
def work(workTime):
bot = telebot.TeleBot(botTokenTG)
endTime = time.time() + workTime
browserPointer = [None,]
while time.time() < endTime:
try:
procedure(browserPointer, endTime)
closeBrowser(browserPointer)
except Exception as e:
closeBrowser(browserPointer)
try:
bot.send_message(chatIdTG, "Error in work loop\n\n" + str(e))
print("Error in work loop\n\n" + str(e))
try:
lastStatusText = ""
with open("last_status.txt") as statusFile:
lastStatusText = statusFile.read()
bot.send_photo(chatIdTG, photo = open("last_status.png", 'rb'), caption = lastStatusText)
except Exception as eee:
print("Error while sending last status screenshot\n\n" + str(eee))
except Exception as ee:
print("Internet error?")
print(ee)
sleepRand(187.372, 230.184)
closeBrowser(browserPointer)
def closeBrowser(browserPointer):
try:
browserPointer[0].quit() #the browserPointer was added to being able to quit the browser in the procedure loop, as all errors are caught here
except:
pass
browserPointer[0] = None
def procedure(browserPointer, endTime):
options = uc.ChromeOptions()
options.add_argument('--user-agent="' + browserUserAgent + '"')
prefs = {
"intl.accept_languages": browserLanguage, #Also change the language in the settings of your used Chrome profile
"intl.selected_languages": browserLanguage
}
options.add_experimental_option("prefs", prefs)
options.add_argument("--user-data-dir=" + browserProfilePath)
options.add_argument("--profile-directory=" + browserProfileName)
browserPointer[0] = uc.Chrome(options=options, driver_executable_path=driverExecutablePath)
browser = browserPointer[0]
sleepRand(10.365, 12.426)
browser.set_window_size(820,1180)
login(browser)
saveStatus(browser, "Logged in...")
scrollUp(browser)
saveStatus(browser, "Scrolled up...")
while time.time() < endTime:
messageElements = getMessages(browser)
unlikedMessages = getUnlikedMessages(browser, messageElements)
if(len(unlikedMessages) > 0):
likeMessage(browser, unlikedMessages[0])
saveStatus(browser, "Liked Message...")
else:
browser.execute_script("arguments[0].scrollIntoView(true);", messageElements[-1])
sleepRand(22.526, 44.184)
def getMessages(browser):
messageElements = []
messagesParentGroups = browser.find_elements(By.CLASS_NAME, "bubbles-date-group")
for _messagesParentGroup in messagesParentGroups:
messagesContainerGroup = _messagesParentGroup.find_elements(By.CLASS_NAME, "bubbles-group")
for messageContainer in messagesContainerGroup:
messageElements.extend(messageContainer.find_elements(By.XPATH, "./div[@data-mid]"))
return messageElements
def login(browser):
browser.get(chatURLTG)
sleepRand(20.231, 25.949)
def scrollUp(browser):
for i in range(3):
messageElements = getMessages(browser)
browser.execute_script("arguments[0].scrollIntoView(true);", messageElements[0])
sleepRand(11.262, 18.376)
saveStatus(browser, f"Scrolled up for {i}. time...")
def likeMessage(browser, message):
browser.execute_script("arguments[0].scrollIntoView({block: 'center', inline: 'nearest'});", message)
sleepRand(1.253, 2.35)
saveStatus(browser, "Liking Message scrollIntoView1...")
actions = ActionChains(browser)
actions.context_click(message).perform()
sleepRand(1.534, 2.78)
saveStatus(browser, "Liking Message performed right click...")
emojiReactionDiv = browser.find_element(By.CSS_SELECTOR, '.btn-menu-reactions-reaction-select[data-doc-id="' + emojiDataDocId + '"]')
emojiReactionDivParent = emojiReactionDiv.find_element(By.XPATH, '..')
emojiReactionElement = emojiReactionDivParent.find_element(By.XPATH, '..')
browser.execute_script("arguments[0].scrollIntoView({block: 'center', inline: 'nearest'});", emojiReactionElement)
sleepRand(1.769, 2.903)
saveStatus(browser, "Liking Message scrollIntoView2...")
emojiReactionElement.click()
sleepRand(7.467, 9.778)
saveStatus(browser, "Liking Message performed left click...")
def getUnlikedMessages(browser, messageElements):
unlikedMessages = []
for msg in messageElements:
try:
if(not isWhiteListed(msg)):
reactions_element = msg.find_element(By.CSS_SELECTOR,"reactions-element")
if(reactions_element):
chosenReactionElement = reactions_element.find_element(By.XPATH, ".//reaction-element[contains(concat(' ', @class, ' '), ' is-chosen ')]")
if(not chosenReactionElement):
unlikedMessages.append(msg)
else:
unlikedMessages.append(msg)
except NoSuchElementException:
unlikedMessages.append(msg)
return unlikedMessages
def isWhiteListed(msgElement):
try:
ownMessage = msgElement.find_element(By.XPATH, ".//*[contains(text(), '" + whiteListedText + "')]") #check if trigger text is in message
elementClasses = msgElement.get_attribute("class")
if("hide-name" in elementClasses and "is-out" in elementClasses): #check if message was sent by me
return True
except NoSuchElementException:
pass
return False
def saveStatus(browser, text):
browser.save_screenshot("last_status.png")
with open("last_status.txt", "w") as f: f.write(text)
#Possible endless scrolling prevention (not working):
#in procedure:
#startMessageDataMid = getMessages(browser)[0].get_attribute("data-mid")
#in getUnlikedMessages:
#unlikedMessages = removeOverStartMessages(startMessageDataMid, unlikedMessages)
#def removeOverStartMessages(startMessageDataMid, messageElements): #This function should delete/clean the unlikedMessages list from messages that were sent higher than the highest scrolled message. This prevents endless scrolling until the last liked messages were found
# Option 1:
# beforeStart = []
# afterStart = []
# found = False
# for element in messageElements:
# if(found == False):
# if(element.get_attribute("data-mid") == startMessageDataMid):
# found = True
# afterStart.append(element)
# else:
# beforeStart.append(element)
#Both of these versions currently do not work, if you want to try it:) (A unique message Identifier is needed, data-mid attribute is not persistent...)
# else:
# afterStart.append(element)
# if(len(afterStart) > 0):
# messageElements = afterStart
# else:
# messageElements = beforeStart
# return messageElements
# Option 2: return messageElements[messageElements.index(startMessage):] if startMessage in messageElements else messageElements