-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
1483 lines (1382 loc) · 57.8 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from requests import *
from comtypes import CoInitialize, CoUninitialize
from time import *
from sys import *
import os
import win32clipboard
from PIL import Image
from io import BytesIO
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from colorama import Fore
import threading
import pyperclip
import soundcard as sc
import soundfile as sf
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume, IAudioMeterInformation
CoInitialize()
class Bot:
def __init__(self, nameBrowser, headless):
options = webdriver.FirefoxOptions()
if headless:
options.add_argument('--headless')
nameBrowser = nameBrowser.lower()
if str(nameBrowser) == "firefox":
self.driver = webdriver.Firefox(options=options)
elif str(nameBrowser) == "chrome":
self.driver = webdriver.Chrome(options=options)
try:
self.driver.get("https://web.eitaa.com/")
except:
return "Error in go_eitaa_web"
while True:
try:
phone_input = self.driver.find_element(By.CSS_SELECTOR, "div.input-field:nth-child(2) > div:nth-child(1)")
except:
continue
else:
phone = input("What's your "+Fore.GREEN+"phone number"+Fore.WHITE+" for login in Eitaa? +")
phone_input.send_keys(Keys.CONTROL + 'a')
phone_input.send_keys(Keys.BACKSPACE)
phone_input.send_keys(str(phone))
self.driver.find_element(By.CSS_SELECTOR, "button.btn-primary:nth-child(4)").click()
break
while True:
try:
code_input = self.driver.find_element(By.CSS_SELECTOR, "input.input-field-input")
except:
continue
else:
Otp_code = input("What's The "+Fore.GREEN+"OTP Code"+Fore.WHITE+" Sent you in Eitaa or sms? ")
while True:
try:
code_input.send_keys(str(Otp_code))
except:
continue
else:
break
break
while True:
try:
self.driver.find_element(By.CSS_SELECTOR, '#main-search')
except:
status = self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/div[3]/div/div[3]/div/label/span")
if str(status.text) == "کد نامعتبر است":
Otp_code = input("OTP code is wrong try again!")
exit()
else:
print("Waithing...", end="\r")
continue
else:
os.system('cls')
sleep(15.5)
break
def list_active_sessions(self):
sessions = AudioUtilities.GetAllSessions()
active_sessions = []
print("Programs currently playing sound:")
for session in sessions:
if session.Process:
process_name = session.Process.name()
pid = session.Process.pid
try:
audio_meter = session._ctl.QueryInterface(IAudioMeterInformation)
peak = audio_meter.GetPeakValue()
if peak > 0:
print(f"Program: {process_name}, PID: {pid}, Peak Volume: {peak:.2f}")
active_sessions.append((session, process_name, pid))
except Exception as e:
print(f"Could not retrieve audio info for {process_name}: {e}")
return active_sessions
def mute_sessions(self, active_sessions):
for session, process_name, pid in active_sessions:
try:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
print(f"Muting Program: {process_name}, PID: {pid}")
volume.SetMasterVolume(0, None)
except Exception as e:
print(f"Could not mute {process_name}: {e}")
def unmute_sessions(self, active_sessions):
for session, process_name, pid in active_sessions:
try:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
print(f"Unmuting Program: {process_name}, PID: {pid}")
volume.SetMasterVolume(1, None)
except Exception as e:
print(f"Could not unmute {process_name}: {e}")
def copy_to_clipboard(self, file_name):
command = f"powershell Set-Clipboard -LiteralPath {file_name}"
os.system(command)
def save_audio(self, s):
OUTPUT_FILE_NAME = "output.mp3" # file name.
SAMPLE_RATE = 48000 # [Hz]. sampling rate.
RECORD_SEC = int(s) # [sec]. duration recording audio.
with sc.get_microphone(id=str(sc.default_speaker().name), include_loopback=True).recorder(samplerate=SAMPLE_RATE) as mic:
# record audio with loopback from default speaker.
data = mic.record(numframes=SAMPLE_RATE*RECORD_SEC)
# change "data=data[:, 0]" to "data=data", if you would like to write audio as multiple-channels.
sf.write(file=OUTPUT_FILE_NAME, data=data[:, 0], samplerate=SAMPLE_RATE)
def scroll(self):
self.driver.execute_script("window.scrollTo(0, window.scrollY + 30)")
def send_to_clipboard(self, clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
def isMessageNew(self, msg1, msg2):
if str(msg1) == str(msg2):
return False
else:
return True
def canSendToUser(self, chat_id):
r = self.driver.execute_script("return appUsersManager.canSendToUser("+str(chat_id)+");")
if str(r) == "true":
return True
elif str(r) == "None":
return "None"
else:
return False
def isUserOnline(self, chat_id):
r = self.driver.execute_script("return appUsersManager.isUserOnlineVisible("+str(chat_id)+");")
if str(r) == "true":
return True
else:
return False
def myId(self):
r = self.driver.execute_script("return appImManager.myId;")
return str(r)
def getContactList(self):
r = self.driver.execute_script("return appUsersManager.contactsList;")
return str(r)
def isContact(self, chat_id):
r = self.driver.execute_script("return appUsersManager.isContact("+str(chat_id)+");")
if str(r) == "true":
return True
else:
return False
def getPeerUsername(self, chat_id):
r = self.driver.execute_script("return appPeersManager.getPeerUsername("+str(chat_id)+");")
if str(r) == "true":
return True
else:
return False
def isAnyGroup(self, chat_id):
r = self.driver.execute_script("return appPeersManager.isAnyGroup("+str(chat_id)+");")
if str(r) == "true":
return True
else:
return False
def isUser(self, chat_id):
if str(chat_id[0]) == "-":
return False
else:
return True
# appPeersManager.getPeerSearchText
def getPeerSearchText(self, chat_id):
r = self.driver.execute_script("return appPeersManager.getPeerSearchText("+str(chat_id)+");")
return str(r)
def getPeer(self, chat_id):
r = self.driver.execute_script("return appPeersManager.getPeer("+str(chat_id)+");")
return str(r)
# appPeersManager.getDialogType
def getDialogType(self, chat_id):
r = self.driver.execute_script("return appPeersManager.getDialogType("+str(chat_id)+");")
return str(r)
def canPinMessage(self, msg_id):
r = self.driver.execute_script("return appPeersManager.canPinMessage("+str(msg_id)+");")
if str(r) == "true":
return True
else:
return False
def canDeleteMessage(self, msg_id):
r = self.driver.execute_script("return appMessagesManager.canDeleteMessage("+str(msg_id)+");")
if str(r) == "true":
return True
else:
return False
# appMessagesManager.canEditMessage()
def canEditMessage(self, msg_id):
r = self.driver.execute_script("return appMessagesManager.canEditMessage("+str(msg_id)+");")
if str(r) == "true":
return True
else:
return False
# appMessagesManager.canForwardMessage
def canEditMessage(self, msg_id):
r = self.driver.execute_script("return appMessagesManager.canForwardMessage("+str(msg_id)+");")
if str(r) == "true":
return True
else:
return False
def back(self):
self.driver.execute_script("appNavigationController.back()")
def get_currentHash(self):
r = self.driver.execute_script("return appNavigationController.currentHash;")
return str(r)
def chat_id(self):
chat = self.driver.find_element(By.CSS_SELECTOR, "div.user-title > span:nth-child(1)")
chatid = chat.get_attribute("data-peer-id")
return str(chatid)
def send_message(self, chat_id, text):
global message_sent
message_sent = True
text = str(text)
text = text.replace('""', '')
text = text.replace("\n", "\\n")
text = text.replace("\"", '\\"')
self.driver.execute_script(f'appMessagesManager.sendText({chat_id}, "{text}")')
try:
res, map = self.onchatupdate()
except:
return None
else:
return res, map
def reply_to_message(self, text, chatid, msg_id):
global message_sent
message_sent = True
text = str(text)
text = text.replace('""', '')
text = text.replace("\n", "\\n")
text = text.replace("\"", '\\"')
self.driver.execute_script('appMessagesManager.sendText('+str(chatid)+', "'+str(text)+'", { replyToMsgId: '+str(msg_id)+' });')
try:
res, map = self.onchatupdate()
except:
return None
else:
return res, map
def edit_message(self, textnew, message):
action = ActionChains(self.driver)
action.context_click(on_element = message)
sleep(1)
action.perform()
sleep(1)
try:
edit = self.driver.find_element(By.CSS_SELECTOR, 'div.tgico-edit > div:nth-child(1)')
except:
return "Error in find_edit"
try:
edit.click()
except:
return "Error in click_edit"
try:
map = self.driver.find_element(By.CSS_SELECTOR, 'div.input-message-input:nth-child(1)')
except:
return "Error in find_message_box"
map.send_keys(Keys.CONTROL + 'a')
map.send_keys(Keys.BACKSPACE)
map.send_keys(textnew)
map.send_keys(Keys.ENTER)
def forward_message(self, target, message, quote):
action = ActionChains(self.driver)
action.context_click(on_element = message)
sleep(1)
action.perform()
sleep(1)
try:
if quote == True:
forward = self.driver.find_element(By.CSS_SELECTOR, 'div.btn-menu-item:nth-child(18)')
elif quote == False:
forward = self.driver.find_element(By.CSS_SELECTOR, 'div.btn-menu-item:nth-child(19)')
except:
return "Error in find_forward"
try:
forward.click()
except:
return "Error in click_forward"
textBox = self.driver.find_element(By.CSS_SELECTOR, '.selector-search-input')
textBox.send_keys(target)
textBox.send_keys(Keys.ENTER)
sleep(1)
try:
forwardtarget = self.driver.find_element(By.CSS_SELECTOR, '.selector > div:nth-child(1) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(1)')
except:
print("Not find a target")
return
forwardtarget.click()
sleep(1)
but = self.driver.find_element(By.CSS_SELECTOR, 'div.input-message-input:nth-child(1)')
but.send_keys(Keys.ENTER)
res, map = self.onchatupdate(self.driver)
return res, map
def pin_message(self, message):
action = ActionChains(self.driver)
action.context_click(on_element = message)
sleep(1)
action.perform()
sleep(1)
try:
pinbox = self.driver.find_element(By.CSS_SELECTOR, 'div.btn-menu-item:nth-child(18) > div:nth-child(1)')
except:
return "Error in find_pin"
try:
pinbox.click()
except:
return "Error in click_pin"
pin = self.driver.find_element(By.CSS_SELECTOR, 'button.btn:nth-child(1) > div:nth-child(1)')
pin.click()
sleep(2)
def delete_message(self, message):
action = ActionChains(self.driver)
action.context_click(on_element = message)
sleep(1)
action.perform()
sleep(1)
try:
deletebox = self.driver.find_element(By.CSS_SELECTOR, 'div.btn-menu-item:nth-child(26)')
except:
return "Error in find_delete"
try:
deletebox.click()
except:
return "Error in click_delete"
delete = self.driver.find_element(By.CSS_SELECTOR, 'button.btn:nth-child(1) > div:nth-child(1)')
delete.click()
def search(self, x, text):
search = self.driver.find_element(By.CSS_SELECTOR, '#main-search')
sleep(3)
search.click()
button = self.driver.find_element(By.CSS_SELECTOR, "#search-container > div:nth-child(1) > div:nth-child(1) > nav:nth-child(1) > div:nth-child("+str(x)+")")
button.click()
searchbox = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[1]/div/div/div[1]/div[2]/input')
sleep(4)
searchbox.send_keys(text)
sleep(1)
tab = self.driver.find_element(By.CSS_SELECTOR, ".search-super-tabs > div:nth-child("+str(x)+")")
tab.click()
try:
chat = self.driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div[1]/div/div/div[2]/div[2]/div[3]/div/div/div["+str(x)+"]/div/div[1]/ul/li[1]")
except:
return "Not Found !"
else:
try:
message_id = chat.get_attribute("data-mid")
except:
chat.click()
else:
chat.click()
return message_id
def FindActiveFolderTabs(self):
elements = self.driver.find_element(By.CSS_SELECTOR, '.menu-horizontal-div .menu-horizontal-div-item.rp')
index = None
for i, el in enumerate(elements):
if 'active' in el.get_attribute('class'):
index = i + 1
break
return int(index)
def on_new_message(self, chat):
try:
chat = self.driver.find_element(By.CSS_SELECTOR, "li.chatlist-chat:nth-child("+str(chat)+")")
except:
return "not found chat"
cp = chat.find_element(By.CLASS_NAME, "user-caption")
sub = cp.find_element(By.CLASS_NAME, "dialog-subtitle")
try:
bubble = sub.find_element(By.CLASS_NAME, "dialog-subtitle-badge")
bubbletext = str(bubble.get_attribute('innerHTML'))
except:
return None
else:
chatid = str(chat.get_attribute('data-peer-id'))
chat.click()
sleep(7.5)
chat.click()
sleep(1)
response = {}
for y in range(1, int(int(bubbletext)+1)):
x = int(str("-"+str(y)))
try:
bubble = self.driver.find_elements(By.CLASS_NAME, "bubble")[int(x)]
except:
self.go_chat(chatid)
sleep(10)
try:
bubble = self.driver.find_elements(By.CLASS_NAME, "bubble")[int(x)]
except:
break
message_id = bubble.get_attribute("data-mid")
chatbox = bubble.find_element(By.CLASS_NAME, "bubble-content-wrapper")
day = chatbox.find_element(By.CLASS_NAME, "bubble-content")
try:
namebox = day.find_element(By.CLASS_NAME, "name")
except:
is_from = False
else:
is_from = True
try:
chatid_from = str(namebox.get_attribute('data-peer-id'))
except:
j = namebox.find_element(By.CLASS_NAME, "i18n").text
if "هدایت شده از " in str(j):
is_forward = True
peer_title = namebox.find_element(By.CLASS_NAME, "peer-title")
chatid_from = str(peer_title.get_attribute("data-peer-id"))
name_from = str(peer_title.text)
else:
is_forward = False
else:
is_forward = False
name_from = namebox.find_element(By.CLASS_NAME, "peer-title").text
name = self.driver.find_element(By.CSS_SELECTOR, "div.user-title > span:nth-child(1)").text
try:
map=day.find_element(By.CLASS_NAME, "message")
except:
return "Error in find_message"
try:
doc = map.find_element(By.CLASS_NAME, "document-container")
except:
pass
else:
action = ActionChains(self.driver)
action.context_click(on_element = map)
sleep(1)
action.perform()
sleep(1)
try:
tigo_link = self.driver.find_element(By.CSS_SELECTOR, "div.tgico-link:nth-child(12)")
except:
link = ""
else:
tigo_link.click()
sleep(0.1)
link = str(pyperclip.paste())
doc2 = doc.find_element(By.CLASS_NAME, "document-wrapper")
audio_element = doc2.find_element(By.TAG_NAME, "audio-element")
btn_play = audio_element.find_element(By.CLASS_NAME, "audio-toggle")
audio_time = audio_element.find_element(By.CLASS_NAME, "audio-time").text
audio_time = str(audio_time).split(":")
audio_time = (int(audio_time[0])*60)+int(audio_time[1])
active_sessions = self.list_active_sessions()
self.mute_sessions(active_sessions)
btn_play.click()
self.save_audio(audio_time)
self.unmute_sessions(active_sessions)
audio = True
try:
attachment = day.find_element(By.CLASS_NAME, "attachment")
except:
media = False
else:
try:
attachment.click()
except:
media = attachment.find_element(By.CLASS_NAME, "media-photo")
media = media.get_attribute("src")
else:
sleep(0.5)
media = self.driver.find_element(By.CSS_SELECTOR, ".media-viewer-aspecter > img:nth-child(1)")
media = media.get_attribute("src")
btn = self.driver.find_element(By.CLASS_NAME, "media-viewer-buttons")
btn.find_element(By.CLASS_NAME, "tgico-close").click()
try:
video_time = attachment.find_element(By.CLASS_NAME, "video-time")
except:
is_video = False
else:
is_video = True
video_time = str(video_time.text)
try:
reply = day.find_element(By.CLASS_NAME, "reply")
except:
reply = False
else:
reply = reply.find_element(By.CLASS_NAME, "reply-content")
reply = reply.get_attribute("innerHTML")
time_tgico = map.find_element(By.TAG_NAME, "span")
time_inner = time_tgico.find_element(By.CLASS_NAME, "i18n").text
if str(time_inner) == "":
is_from_me = False
else:
is_from_me = True
time = time_tgico.get_attribute("title")
try:
view_message = time_tgico.find_element(By.CLASS_NAME, "post-views").text
except:
view_message = False
text = str(map.text)
text = text.split("\n"+str(time_inner))[0]
response2 = {
"result"+str(x):{
"message_id":str(message_id),
"link":str(link),
"chat":{
"id":str(chatid),
"title":str(name),
"username":str(self.getPeerUsername(chatid)),
"type":str(self.getDialogType(chatid))
},
}
}
if audio:
new_data = {
"audio":{
"output_file":"output.mp3",
"audio_time":int(audio_time)
}
}
response2["result"+str(x)].update(new_data)
if media:
new_data = {
"media":{
"media-src": str(media)
}
}
response2["result"+str(x)].update(new_data)
if is_video:
new_data = {
"video":{
"video-time":str(video_time)
}
}
response2["result"+str(x)]["media"].update(new_data)
if is_from:
new_data = {
"from":{
"is_forward":is_forward,
"id":str(chatid_from),
"name":str(name_from),
"username":str(self.getPeerUsername(chatid_from)),
"type":str(self.getDialogType(chatid_from))
}
}
response2["result"+str(x)].update(new_data)
if reply:
new_data = {
"reply":{
"reply-content": str(reply)
}
}
response2["result"+str(x)].update(new_data)
if view_message:
new_data = {
"date":str(time),
"text":str(text),
"view":str(view_message)
}
else:
new_data = {
"date":str(time),
"text":str(text),
"is_from_me":is_from_me
}
response2["result"+str(x)].update(new_data)
response.update(response2)
return response, map
def on_all_message(self, chat):
try:
chat = self.driver.find_element(By.CSS_SELECTOR, "li.chatlist-chat:nth-child("+str(chat)+")")
except:
return "not found chat"
cp = chat.find_element(By.CLASS_NAME, "user-caption")
sub = cp.find_element(By.CLASS_NAME, "dialog-subtitle")
try:
bubble = sub.find_element(By.CLASS_NAME, "dialog-subtitle-badge")
bubbletext = str(bubble.get_attribute('innerHTML'))
except:
pass
chatid = str(chat.get_attribute('data-peer-id'))
chat.click()
sleep(7.5)
chat.click()
sleep(1)
response = {}
y = 0
while True:
y += 1
x = int(str("-"+str(y)))
try:
bubble = self.driver.find_elements(By.CLASS_NAME, "bubble")[int(x)]
except:
self.go_chat(chatid)
sleep(10)
try:
bubble = self.driver.find_elements(By.CLASS_NAME, "bubble")[int(x)]
except:
break
message_id = bubble.get_attribute("data-mid")
try:
chatbox = bubble.find_element(By.CLASS_NAME, "bubble-content-wrapper")
except:
continue
day = chatbox.find_element(By.CLASS_NAME, "bubble-content")
try:
namebox = day.find_element(By.CLASS_NAME, "name")
except:
is_from = False
else:
is_from = True
try:
chatid_from = str(namebox.get_attribute('data-peer-id'))
except:
j = namebox.find_element(By.CLASS_NAME, "i18n").text
if "هدایت شده از " in str(j):
is_forward = True
peer_title = namebox.find_element(By.CLASS_NAME, "peer-title")
chatid_from = str(peer_title.get_attribute("data-peer-id"))
name_from = str(peer_title.text)
else:
is_forward = False
else:
is_forward = False
name_from = namebox.find_element(By.CLASS_NAME, "peer-title").text
name = self.driver.find_element(By.CSS_SELECTOR, "div.user-title > span:nth-child(1)").text
try:
map=day.find_element(By.CLASS_NAME, "message")
except:
return "Error in find_message"
try:
doc = map.find_element(By.CLASS_NAME, "document-container")
except:
pass
else:
action = ActionChains(self.driver)
action.context_click(on_element = map)
sleep(1)
action.perform()
sleep(1)
try:
tigo_link = self.driver.find_element(By.CSS_SELECTOR, "div.tgico-link:nth-child(12)")
except:
link = ""
else:
tigo_link.click()
sleep(0.1)
link = str(pyperclip.paste())
doc2 = doc.find_element(By.CLASS_NAME, "document-wrapper")
audio_element = doc2.find_element(By.TAG_NAME, "audio-element")
btn_play = audio_element.find_element(By.CLASS_NAME, "audio-toggle")
audio_time = audio_element.find_element(By.CLASS_NAME, "audio-time").text
audio_time = str(audio_time).split(":")
audio_time = (int(audio_time[0])*60)+int(audio_time[1])
active_sessions = self.list_active_sessions()
self.mute_sessions(active_sessions)
btn_play.click()
self.save_audio(audio_time)
self.unmute_sessions(active_sessions)
audio = True
try:
attachment = day.find_element(By.CLASS_NAME, "attachment")
except:
media = False
else:
media = attachment.find_element(By.CLASS_NAME, "media-photo")
media = media.get_attribute("src")
try:
video_time = attachment.find_element(By.CLASS_NAME, "video-time")
except:
is_video = False
else:
is_video = True
video_time = str(video_time.text)
try:
reply = day.find_element(By.CLASS_NAME, "reply")
except:
reply = False
else:
reply = reply.find_element(By.CLASS_NAME, "reply-content")
reply = reply.get_attribute("innerHTML")
time_tgico = map.find_element(By.TAG_NAME, "span")
time_inner = time_tgico.find_element(By.CLASS_NAME, "inner").get_attribute("innerHTML").split("</span>")[1]
if str(time_inner) == "":
is_from_me = False
else:
is_from_me = True
time = time_tgico.get_attribute("title")
try:
view_message = time_tgico.find_element(By.CLASS_NAME, "post-views").text
except:
view_message = False
text = str(map.text)
response2 = {
"result"+str(x):{
"message_id":str(message_id),
"link":str(link),
"chat":{
"id":str(chatid),
"title":str(name),
"username":str(self.getPeerUsername(chatid)),
"type":str(self.getDialogType(chatid))
},
}
}
if audio:
new_data = {
"audio":{
"output_file":"output.mp3",
"audio_time":int(audio_time)
}
}
response2["result"+str(x)].update(new_data)
if media:
new_data = {
"media":{
"media-src": str(media)
}
}
response2["result"+str(x)].update(new_data)
if is_video:
new_data = {
"video":{
"video-time":str(video_time)
}
}
response2["result"+str(x)]["media"].update(new_data)
if is_from:
new_data = {
"from":{
"is_forward":is_forward,
"is_from_me":is_from_me,
"id":str(chatid_from),
"name":str(name_from),
"username":str(self.getPeerUsername(chatid_from)),
"type":str(self.getDialogType(chatid_from))
}
}
response2["result"+str(x)].update(new_data)
if reply:
new_data = {
"reply":{
"reply-content": str(reply)
}
}
response2["result"+str(x)].update(new_data)
if view_message:
new_data = {
"date":str(time),
"text":str(text),
"view":str(view_message)
}
else:
new_data = {
"date":str(time),
"text":str(text),
"is_from_me":is_from_me
}
if bubbletext:
if int(bubbletext) >= int(y):
new_data = {
"unread":True,
}
else:
new_data = {
"unread":False,
}
response2["result"+str(x)].update(new_data)
response.update(response2)
return response, map
def get_info(self, chat_id):
s = self.driver.find_element(By.CSS_SELECTOR, "div.sidebar-header:nth-child(2)")
s.click()
sleep(1)
try:
username = self.driver.find_element(By.CSS_SELECTOR, ".tgico-username")
except:
username = False
else:
username = str(username.text)
try:
bio = self.driver.find_element(By.CSS_SELECTOR, ".tgico-info")
except:
bio = False
else:
bio = str(bio.text)
name = self.driver.find_element(By.CSS_SELECTOR, ".profile-name > span:nth-child(1)")
name = str(name.text)
status = self.driver.find_element(By.CSS_SELECTOR, ".profile-subtitle > span:nth-child(1)")
status = str(status.text)
try:
phone = self.driver.find_element(By.CSS_SELECTOR, ".tgico-phone")
except:
phone = False
else:
phone = str(phone.text)
result = {
'name':str(name),
'status':str(status),
'bio':str(bio),
'phone':str(phone),
'username':str(username),
'chat_id':str(chat_id),
}
return str(result)
def sendChatActionThread(self):
global message_sent
message_sent = False
try:
map = self.driver.find_element(By.CSS_SELECTOR, 'div.input-message-input:nth-child(1)')
except:
return "Error in find_message_box"
while not message_sent:
map.send_keys("s")
map.send_keys(Keys.CONTROL + 'a')
map.send_keys(Keys.BACKSPACE)
def sendChatAction(self, chatid):
self.go_chat(chatid)
thread = threading.Thread(target=self.sendChatActionThread)
thread.start()
def getMe(api):
req = get("https://eitaayar.ir/api/"+api+"/getMe")
return str(req.text)
def create_channel(self, name, bio):
menu = self.driver.find_element(By.CSS_SELECTOR, "#new-menu")
menu.click()
newchannel = self.driver.find_element(By.CSS_SELECTOR, ".tgico-newchannel")
newchannel.click()
sleep(1)
name_channel = self.driver.find_element(By.CSS_SELECTOR, "div.input-wrapper:nth-child(2) > div:nth-child(1) > div:nth-child(1)")
name_channel.send_keys(name)
bio_channel = self.driver.find_element(By.CSS_SELECTOR, "div.input-wrapper:nth-child(2) > div:nth-child(2) > div:nth-child(1)")
bio_channel.send_keys(bio)
next = self.driver.find_element(By.CSS_SELECTOR, ".tgico-arrow_next")
next.click()
sleep(1)
next2 = self.driver.find_element(By.CSS_SELECTOR, "button.btn-circle:nth-child(1)")
sleep(1)
next2.click()
self.send_message(self.driver, ".")
chat_id = chat_id(False, True, self.driver)
result = {
'name':str(name),
'bio':str(bio),
'chat_id':str(chat_id),
}
return str(result)
def folders_tabs(self, x):
s = self.driver.find_element(By.CSS_SELECTOR, "#folders-tabs > div:nth-child("+str(x)+")")
s.click()
sleep(3.5)
def contactMessage(self, map):
try:
c = map.find_element(By.CLASS_NAME, "contact")
except:
return False
chat_id = c.get_attribute("data-peer-id")
d = c.find_element(By.CLASS_NAME, "contact-details")
name = d.find_element(By.CLASS_NAME, "contact-name")
number = d.find_element(By.CLASS_NAME, "contact-number")
return str(name.text), str(number.text), str(chat_id)
def send_album(self, filepath, caption, Send_compressed):
global message_sent
message_sent = True
n = 3
for i in filepath:
n += 1
image = Image.open(i)
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
self.send_to_clipboard(win32clipboard.CF_DIB, data)
map = self.driver.find_element(By.CSS_SELECTOR, 'div.input-message-input:nth-child(1)')
map.send_keys(Keys.CONTROL + 'v')
sleep(1)
btn = self.driver.find_element(By.XPATH, "//span[contains(., 'ارسال به صورت فشرده')]")
title = self.driver.find_element(By.CSS_SELECTOR, ".popup-title > span:nth-child(1)")
if Send_compressed:
if str(title.text) == "ارسال عکس":
pass
else:
if str(title.text) == "ارسال عکس":
btn.click()
try:
caption2 = self.driver.find_element(By.CSS_SELECTOR, "div.input-field-input")
caption2.click()
except:
caption2 = self.driver.find_element(By.CSS_SELECTOR, "div.input-field:nth-child("+str(n)+") > div:nth-child(1)")
caption2.click()
caption2.send_keys(caption)
send = self.driver.find_element(By.CSS_SELECTOR, "button.btn-primary:nth-child(3)")
send.click()
def send_other(self, path, caption, Send_compressed):
global message_sent
message_sent = True
n = 3
for i in path:
self.copy_to_clipboard(str(i))
map = self.driver.find_element(By.CSS_SELECTOR, 'div.input-message-input:nth-child(1)')
map.send_keys(Keys.CONTROL + 'v')
n += 1
sleep(1)
btn = self.driver.find_element(By.XPATH, "//span[contains(., 'ارسال به صورت فشرده')]")
title = self.driver.find_element(By.CSS_SELECTOR, ".popup-title > span:nth-child(1)")
if Send_compressed:
if str(title.text) == "ارسال عکس":
pass
else:
if str(title.text) == "ارسال عکس":
btn.click()
try:
caption2 = self.driver.find_element(By.CSS_SELECTOR, "div.input-field-input")
caption2.click()
except:
caption2 = self.driver.find_element(By.CSS_SELECTOR, "div.input-field:nth-child("+str(n)+") > div:nth-child(1)")
caption2.click()
caption2.send_keys(caption)
send = self.driver.find_element(By.CSS_SELECTOR, "button.btn-primary:nth-child(3)")
send.click()
def onchatupdate(self):
chatid = self.chat_id()
try:
bubble = self.driver.find_elements(By.CLASS_NAME, "bubble")[-1]
except:
self.go_chat(chatid)
sleep(10)
try:
bubble = self.driver.find_elements(By.CLASS_NAME, "bubble")[-1]
except:
return None
message_id = bubble.get_attribute("data-mid")
chatbox = bubble.find_element(By.CLASS_NAME, "bubble-content-wrapper")
day = chatbox.find_element(By.CLASS_NAME, "bubble-content")
try:
namebox = day.find_element(By.CLASS_NAME, "name")