-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrpc_client.py
912 lines (670 loc) · 31.9 KB
/
rpc_client.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
import argparse
import asyncio
import datetime
import json
import os
import pprint
import socket
import sys
import tempfile
import time
import traceback
from threading import Thread
from typing import Optional, Union
from urllib.parse import quote
import aiohttp
import emoji
import tornado.web
from PySimpleGUI import PySimpleGUI as sg
from discoIPC.ipc import DiscordIPC
from app_version import version
from config_loader import read_config, ActivityType
from langs import langs
from main_window import RPCGui
config = read_config()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("O app de rich_presence está em execução!")
a = tornado.web.Application([(r'/', MainHandler)])
try:
a.listen(config['app_port'])
except:
sg.popup_ok(F"A porta {config['app_port']} está em uso!\nO app já está em execução?")
sys.exit(0)
def time_format(milliseconds: Union[int, float]) -> str:
minutes, seconds = divmod(int(milliseconds / 1000), 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
strings = f"{minutes:02d}:{seconds:02d}"
if hours:
strings = f"{hours}:{strings}"
if days:
strings = (f"{days} dias" if days > 1 else f"{days} dia") + (f", {strings}" if strings != "00:00" else "")
return strings
track_source_replaces = {
"applemusic": "Apple Music",
"youtubemusic": "Youtube Music",
}
class MyDiscordIPC(DiscordIPC):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = ""
self.next = None
self.updating = False
self.last_data = {}
def _send(self, opcode, payload):
encoded_payload = self._encode(opcode, payload)
try:
if self.platform == 'windows':
self.socket.write(encoded_payload)
try:
self.socket.flush()
except OSError:
raise IPCError(f'Não foi possivel enviar dados ao discord via IPC.', client=self)
else:
self.socket.send(encoded_payload)
except Exception as e:
raise IPCError(f'Não foi possivel enviar dados ao discord via IPC | Erro: {repr(e)}.', client=self)
def update_activity(self, activity=None):
if activity:
self.last_data = activity
else:
self.last_data.clear()
try:
super().update_activity(activity=activity)
except Exception as e:
self.last_data.clear()
raise e
def test_ipc_path(self, path):
'''credits: pypresence https://github.com/qwertyquerty/pypresence/blob/master/pypresence/utils.py#L25
Tests an IPC pipe to ensure that it actually works'''
if sys.platform == 'win32' or sys.platform == 'win64':
with open(path):
return True
else:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(path)
return True
def _get_ipc_path(self, pipe=None):
# credits: pypresence https://github.com/qwertyquerty/pypresence/blob/master/pypresence/utils.py#L37
ipc = 'discord-ipc-'
if pipe is not None:
ipc = f"{ipc}{pipe}"
if sys.platform in ('linux', 'darwin'):
tempdir = os.environ.get('XDG_RUNTIME_DIR') or (
f"/run/user/{os.getuid()}" if os.path.exists(f"/run/user/{os.getuid()}") else tempfile.gettempdir())
paths = ['.', 'snap.discord', 'app/com.discordapp.Discord', 'app/com.discordapp.DiscordCanary']
elif sys.platform == 'win32':
tempdir = r'\\?\pipe'
paths = ['.']
else:
return
for path in paths:
full_path = os.path.abspath(os.path.join(tempdir, path))
if sys.platform == 'win32' or os.path.isdir(full_path):
for entry in os.scandir(full_path):
if entry.name.startswith(ipc) and os.path.exists(entry) and self.test_ipc_path(entry.path):
return entry.path
class IPCError(Exception):
def __init__(self, error, client: MyDiscordIPC):
self.error = error
self.client = client
def __repr__(self):
return self.error
user_clients = {}
valid_presence_fields = ("state", "details", "assets", "timestamps", "pid", "start", "end", "large_image", "large_text",
"small_image", "small_text", "party_id", "party_size", "join", "spectate", "match", "buttons",
"instance")
replaces = [
('"', '"'),
('&', '&'),
('(', '\u0028'),
(')', '\u0029'),
('[', '【'),
(']', '】'),
(" ", " "),
("*", '"'),
("_", ' '),
("{", "\u0028"),
("}", "\u0029"),
("`", "'")
]
def fix_characters(text: str, limit=30):
for r in replaces:
text = text.replace(r[0], r[1])
if len(text) > limit:
return f"{text[:limit - 3]}..."
return text
loop = asyncio.new_event_loop()
_t = Thread(target=loop.run_forever)
_t.daemon = True
_t.start()
class RpcClient:
def __init__(self, autostart: int = 0):
self.last_data = {}
self.tasks = []
self.main_task = None
self.config = config
self.langs = langs
self.session: Optional[aiohttp.ClientSession] = None
self.closing = False
self.activity_type = {a.name: a.value for a in ActivityType}
if os.path.isdir("./langs"):
for f in os.listdir("./langs"):
if not f.endswith(".json"):
continue
lang_data = self.load_json(f"./langs/{f}")
if not (lang := f[:-5]) in self.langs:
self.langs[lang] = lang_data
else:
self.langs[lang].update(lang_data)
self.users_rpc = {
# user -> {bot: presence}
}
self.users_socket = {
# url -> [ids]
}
self.bots_socket = {
# url -> [ids]
}
self.gui = RPCGui(self, autostart=autostart)
def load_json(self, json_file: str):
with open(json_file, encoding="utf-8") as f:
return json.load(f)
def save_json(self, json_file: str, data: dict):
with open(json_file, "w") as f:
f.write(json.dumps(data, indent=4))
def get_app_instances(self):
for i in range(10):
try:
rpc = MyDiscordIPC(str(config["dummy_app_id"]), pipe=i)
rpc.connect()
if not config["override_appid"]:
time.sleep(0.5)
rpc.disconnect()
except Exception:
continue
user_id_ = rpc.data['data']['user']['id']
user = f"{rpc.data['data']['user']['username']}"
user_clients[int(user_id_)] = {"pipe": i, "user": user}
rpc.user = user
self.gui.update_log(f"RPC conectado: {user} [{user_id_}] pipe: {i}")
if not self.config["load_all_instances"]:
break
if not user_clients:
raise Exception("Não foi detectado nenhuma instância do discord em execução.")
def close_app_instances(self):
for u_id, u_data in self.users_rpc.items():
for b_id, rpc in u_data.items():
try:
rpc.disconnect()
except:
traceback.print_exc()
self.users_rpc.clear()
user_clients.clear()
def get_bot_rpc(self, bot_id: int, pipe: int) -> MyDiscordIPC:
rpc = MyDiscordIPC(str(bot_id), pipe=pipe)
rpc.connect()
return rpc
def check_presence(self, user_id: int, bot_id: int):
if not (self.users_rpc.get(user_id)):
try:
rpc = self.get_bot_rpc(bot_id, user_clients[user_id]["pipe"])
self.users_rpc[user_id] = {bot_id: rpc}
return
except:
pass
try:
user_apps = self.users_rpc[user_id][bot_id]
if not user_apps.connected:
user_apps.connect()
except KeyError:
try:
rpc = self.get_bot_rpc(bot_id, user_clients[user_id]["pipe"])
self.users_rpc[user_id][bot_id] = rpc
except FileNotFoundError:
return
def exit(self):
for t in self.tasks:
loop.call_soon_threadsafe(t.cancel)
try:
loop.call_soon_threadsafe(self.main_task.cancel)
except AttributeError:
pass
def process_data(self, user_id: int, bot_id: int, data: dict, url: str = "", refresh_timestamp=True):
data = dict(data)
if data['op'] == "update":
self.update(user_id, bot_id, data, refresh_timestamp=refresh_timestamp)
elif data['op'] == "idle":
try:
self.last_data[user_id][bot_id] = dict(data)
except KeyError:
self.last_data[user_id] = {bot_id: dict(data)}
payload = self.get_idle_data(bot_id, data)
if self.config["override_appid"]:
payload["assets"]["large_image"] = self.config["assets"]["idle"]
self.update(user_id, bot_id, payload, refresh_timestamp=refresh_timestamp)
elif data['op'] == "close":
try:
self.users_socket[url].remove(user_id)
except:
pass
try:
del self.last_data[user_id][bot_id]
except:
pass
self.update(user_id, bot_id, {})
else:
self.gui.update_log(f"unknow op: {data}", tooltip=True, log_type="warning")
def update(self, user_id: int, bot_id: int, data: dict, refresh_timestamp=True):
current_data = dict(data)
try:
current_data.pop("op")
except:
pass
self.check_presence(user_id, bot_id)
if not current_data:
try:
self.users_rpc[user_id][bot_id].clear()
except:
pass
return
payload = {
"assets": {
"small_image": "https://i.ibb.co/qD5gvKR/cd.gif"
},
"timestamps": {},
"type": data.get("type", self.activity_type.get(self.config["activity_type"], ActivityType.playing.value))
}
track = current_data.pop("track", None)
thumb = current_data.pop("thumb", None)
guild = current_data.pop("guild", "")
start_time = current_data.pop("start_time", datetime.datetime.now(datetime.timezone.utc).timestamp())
listen_along_url = current_data.pop("listen_along_invite", None)
for d in dict(current_data):
if d not in valid_presence_fields:
del current_data[d]
payload.update(current_data)
if not payload["assets"].get("large_image"):
payload["assets"]["large_image"] = thumb
if track:
playlist_name = track.get("playlist_name")
clear_presence = False
blacklist = self.config["track_blacklist"].lower().split("||")
loop_queue_txt = ""
if blacklist:
for word in track["title"].lower().split():
for blacklistword in blacklist:
if word in blacklistword:
clear_presence = True
if not clear_presence:
blacklist = self.config["uploader_blacklist"].lower().split("||")
for word in track["author"].lower().split():
for blacklistword in blacklist:
if word in blacklistword:
clear_presence = True
if not clear_presence and playlist_name:
blacklist = self.config["playlist_blacklist"].lower().split("||")
for word in playlist_name.lower().split():
for blacklistword in blacklist:
if word in blacklistword:
clear_presence = True
if clear_presence:
self.users_rpc[user_id][bot_id].clear()
return
if self.config["show_thumbnail"] and track["thumb"]:
payload["assets"]["large_image"] = track["thumb"].replace("mqdefault", "default")
payload['details'] = track["title"]
show_platform_icon = False
if not track["paused"]:
if track["stream"]:
if track["source"] == "twitch" and self.config["show_platform_icon"]:
payload['assets']['small_image'] = self.config["assets"]["sources"][track["source"]]
payload['assets']['small_text'] = "Twitch: " + self.get_lang("stream")
else:
payload['assets']['small_image'] = self.config["assets"]["stream"]
payload['assets']['small_text'] = self.get_lang("stream")
payload['timestamps']['start'] = track['duration']
else:
endtime = datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(milliseconds=track["duration"] - track["position"])
payload['timestamps']['end'] = int(endtime.timestamp())
payload['timestamps']['start'] = int(start_time)
player_loop = track.get('loop')
if player_loop:
if player_loop == "queue":
loop_queue_txt = self.get_lang('loop_queue')
show_platform_icon = True
else:
if isinstance(player_loop, list):
loop_text = f"{self.get_lang('loop_text')}: {player_loop[0]}/{player_loop[1]}."
elif isinstance(player_loop, int):
loop_text = f"{self.get_lang('loop_remaining')}: {player_loop}"
else:
loop_text = self.get_lang("loop_text")
payload['assets']['small_image'] = self.config["assets"]["loop"]
payload['assets']['small_text'] = loop_text
else:
show_platform_icon = True
if show_platform_icon:
if self.config["show_platform_icon"]:
try:
payload['assets']['small_image'] = self.config["assets"]["sources"][track["source"]]
except KeyError:
pass
payload['assets']['small_text'] = track["source"]
else:
payload['assets']['small_image'] = self.config["assets"]["play"]
payload['assets']['small_text'] = self.get_lang("playing")
else:
payload['assets']['small_image'] = self.config["assets"]["pause"]
payload['assets']['small_text'] = self.get_lang("paused")
state = ""
button_dict = {}
if (url := track.get("url")) and self.config["show_listen_button"]:
if not self.config["playlist_refs"]:
url = url.split("&list=")[0]
if self.config["show_platform_icon"]:
listen_text = f'{self.get_lang("listen_on")} {track_source_replaces.get(track["source"], track["source"].capitalize())}' if (track["source"] and track['source'] not in ("http", "local")) else self.get_lang("listen_music")
else:
listen_text = f'{self.get_lang("listen_music")}'
button_dict[self.config["button_order"].index('listen_button')] = {"label": listen_text, "url": url.replace("www.", "")}
state += f'👤{self.get_lang("author")}: {track["author"]}'
playlist_url = track.get("playlist_url")
album_url = track.get("album_url")
album_name = track.get("album_name")
large_image_desc = []
if playlist_name and playlist_url:
playlist_translation = self.get_lang("playlist")
if self.config["show_playlist_button"]:
playlist_index = self.config["button_order"].index('playlist_button')
character_limit = self.config["button_character_limit"] if emoji.emoji_count(
playlist_name) < 1 else (self.config["button_character_limit"] - 7)
if not self.config["show_playlist_name_in_button"] or (playlist_name_size:=len(playlist_name)) > character_limit:
button_dict[playlist_index] = {
"label": self.get_lang("view_playlist"), "url": playlist_url.replace("www.", "")}
else:
if ((len(playlist_translation) + playlist_name_size + 2)) > character_limit:
button_dict[playlist_index] = {
"label": playlist_name, "url": playlist_url.replace("www.", "")}
else:
button_dict[playlist_index] = {
"label": f"{playlist_translation}: {playlist_name}", "url": playlist_url.replace("www.", "")}
elif self.config["show_playlist_text"]:
playlist_txt = f"{playlist_translation}: {playlist_name}"
large_image_desc.append(playlist_txt)
if album_url:
album_txt = f'{self.get_lang("album")}: {album_name}'
if len(album_txt) < self.config["button_character_limit"]:
album_button = {"label": album_txt, "url": album_url}
elif len(album_name) < self.config["button_character_limit"]:
album_button = {"label": album_name, "url": album_url}
else:
album_button = {"label": (album_name[:self.config["button_character_limit"]-3] + "..."), "url": album_url}
button_dict[self.config["button_order"].index('album_button')] = album_button
if payload["type"] != ActivityType.listening.value:
large_image_desc.append(f"📀{album_txt}")
if not track["stream"] and (track["source"] not in ("lastfm", "http", "local")):
if track["source"] == "youtube":
if track["author"].endswith(" - topic") and not track["author"].endswith(
"Release - topic") and not track["title"].startswith(track['author'][:-8]):
title = track["title"]
author = track["author"][:-8]
else:
try:
author, title = track["title"].split(" - ", maxsplit=1)
except ValueError:
title = track["title"]
author = track["author"]
else:
title = track["title"]
author = track["author"]
button_dict[self.config["button_order"].index('open_lastfm')] = {
"label": f"{self.get_lang('listen_on')} Last.FM",
"url": f"https://www.last.fm/music/{quote(author.split(',')[0])}/_/{quote(title)}"
}
try:
if track["queue"] and self.config["enable_queue_text"]:
large_image_desc.append(f'🎶{self.get_lang("queue").replace("{queue}", str(track["queue"]))}')
except KeyError:
pass
if guild and self.config['show_guild_name']:
large_image_desc.append(f"🌐{self.get_lang('server')}: {guild}")
try:
if track["247"]:
large_image_desc.append("⏰24/7")
except KeyError:
pass
try:
if track["autoplay"]:
state += f" | 👍{self.get_lang('recommended')}"
except KeyError:
pass
if large_image_desc:
payload['assets']['large_text'] = " | ".join(large_image_desc)
if self.config['show_listen_along_button'] and listen_along_url:
button_dict[self.config["button_order"].index('listen_along_button')] = {"label": self.get_lang("listen_along"), "url": listen_along_url}
if lastfm_user:=data.get("lastfm_user"):
button_dict[self.config["button_order"].index('lastfm_profile')] = {"label": f"Last.fm: {lastfm_user[:20]}", "url": f"https://www.last.fm/user/{lastfm_user}"}
if button_dict:
button_dict = {key: value for key, value in sorted(button_dict.items(), key=lambda k: k)[:2]}
payload["buttons"] = [v for v in button_dict.values()]
if loop_queue_txt:
state += f" | 🔄{loop_queue_txt}"
if not state:
payload['state'] = " "
else:
payload['state'] = state[:128]
try:
if track and not track.get('autoplay') and (self.config["block_other_users_track"] and track["requester_id"] != user_id):
self.users_rpc[user_id][bot_id].clear()
else:
self.users_rpc[user_id][bot_id].update_activity(payload)
except IPCError:
used_pipes = [i["pipe"] for u, i in user_clients.items() if u != user_id]
for i in range(12):
if i in used_pipes:
continue
try:
rpc = self.get_bot_rpc(bot_id, i)
rpc.connect()
self.users_rpc[user_id][bot_id] = rpc
user_clients[user_id]["pipe"] = i
self.update(user_id, bot_id, data, refresh_timestamp=refresh_timestamp)
self.gui.update_log(f"RPC reconectado ao discord: {user_clients[user_id]['user']} | pipe: {i}")
return
except Exception:
traceback.print_exc()
continue
del self.users_rpc[user_id]
del user_clients[user_id]
except Exception as e:
traceback.print_exc()
self.gui.update_log(repr(e), exception=e)
pprint.pprint(payload)
def get_idle_data(self, bot_id: int, data: dict):
data = dict(data)
text_idle = self.get_lang("idle")
payload = {
"thumb": data.pop("thumb", None),
"assets": {},
"details": text_idle[0],
"timestamps": {}
}
try:
payload["timestamps"] = {"end": data["idle_endtime"], "start": data["idle_starttime"]}
except KeyError:
pass
if len(text_idle) > 1:
payload['state'] = text_idle[1]
buttons = []
public = data.pop("public", True)
support_server = data.pop("support_server", None)
if public and self.config["bot_invite"]:
invite = f"https://discord.com/api/oauth2/authorize?client_id={bot_id}&" \
f"permissions={data.pop('invite_permissions', 8)}&scope=bot%20applications.commands"
buttons.append({"label": self.get_lang("invite"), "url": invite})
if support_server:
buttons.append({"label": self.get_lang("support_server"), "url": support_server})
if len(buttons) < 2 and (lastfm_user := data.get("lastfm_user")):
buttons.append({"label": f"Last.fm: {lastfm_user[:20]}", "url": f"https://www.last.fm/user/{lastfm_user}"})
if buttons:
payload["buttons"] = buttons
return payload
def get_lang(self, key: str) -> str:
try:
lang = self.langs[self.config["language"]]
txt: str = lang.get(key)
if not txt:
txt = self.langs["en-us"].get(key)
except KeyError:
txt = self.langs["en-us"].get(key)
return txt
def clear_users_presences(self, uri: str):
for bot_id in self.bots_socket[uri]:
for user_id in self.users_socket[uri]:
try:
self.users_rpc[user_id][bot_id].clear()
except:
continue
async def handle_socket(self, uri):
backoff = 7
while True:
try:
async with self.session.ws_connect(uri, heartbeat=self.config["heartbeat"], timeout=120) as ws:
try:
self.users_socket[uri].clear()
except:
pass
try:
self.bots_socket[uri].clear()
except:
pass
self.bots_socket[uri] = set()
self.users_socket[uri] = set()
self.gui.update_log(f"Websocket conectado: {uri}", tooltip=True)
await ws.send_str(
json.dumps(
{
"op": "rpc_update",
"user_ids": list(user_clients),
"token": self.config["token"].replace(" ", "").replace("\n", ""),
"version": version
}
)
)
async for msg in ws:
try:
if msg.type == aiohttp.WSMsgType.TEXT:
try:
data = json.loads(msg.data)
except Exception:
traceback.print_exc()
continue
try:
if not data['op']:
print(data)
continue
except:
traceback.print_exc()
continue
bot_id = data.pop("bot_id", None)
if self.config["override_appid"]:
bot_id = int(self.config["dummy_app_id"])
bot_name = data.pop("bot_name", None)
if data['op'] == "disconnect":
self.gui.update_log(f"op: {data['op']} | {uri} | reason: {data.get('reason')}",
log_type="error")
self.closing = True
await ws.close()
return
if bot_id:
try:
self.bots_socket[uri].add(bot_id)
except TypeError:
for i in bot_id:
self.bots_socket[uri].add(i)
user_ws = data.pop("user", None)
if not user_ws:
continue
self.users_socket[uri].add(user_ws)
try:
user = user_clients[user_ws]["user"]
except KeyError:
continue
if data['op'] == "exception":
self.gui.update_log(f"op: {data['op']} | {user} {user_ws} | "
f"bot: {(bot_name + ' ') if bot_name else ''}[{bot_id}] | "
f"\nerror: {data.get('message')}",
log_type="error")
continue
self.gui.update_log(f"op: {data['op']} | {user} {user_ws} | "
f"bot: {(bot_name + ' ') if bot_name else ''}[{bot_id}]",
log_type="info")
try:
self.last_data[user_ws][bot_id] = data
except KeyError:
self.last_data[user_ws] = {bot_id: data}
try:
del data["token"]
except KeyError:
pass
self.process_data(user_ws, bot_id, data, url=uri)
elif msg.type in (aiohttp.WSMsgType.CLOSED,
aiohttp.WSMsgType.CLOSING,
aiohttp.WSMsgType.CLOSE):
self.gui.update_log(f"Conexão finalizada com o servidor: {uri}")
return
elif msg.type == aiohttp.WSMsgType.ERROR:
self.clear_users_presences(uri)
if self.closing:
return
self.gui.update_log(
f"Conexão perdida com o servidor: {uri} | Reconectando em {time_format(backoff)} seg. {repr(ws.exception())}",
tooltip=True, log_type="error")
await asyncio.sleep(backoff)
backoff *= 1.3
else:
self.gui.update_log(
f"Unknow message type: {msg.type}",
log_type="warning"
)
except Exception as e:
traceback.print_exc()
self.gui.update_log(
f"Ocorreu um erro no link: {uri}\n{repr(e)}.",
log_type="error"
)
self.gui.update_log(
f"Desconectado: {uri} | Nova tentativa de conexão em "
f"{time_format(self.config['reconnect_timeout']*1000)}...",
log_type="warning"
)
self.clear_users_presences(uri)
await asyncio.sleep(self.config['reconnect_timeout'])
except (aiohttp.WSServerHandshakeError, aiohttp.ClientConnectorError):
tm = backoff * 5
self.gui.update_log(
f"Servidor indisponível: {uri} | Nova tentativa de conexão em {time_format(tm*1000)}.",
log_type="warning"
)
await asyncio.sleep(tm)
backoff *= 2
except Exception as e:
traceback.print_exc()
self.gui.update_log(f"Erro na conexão: {uri} | {repr(e)}", tooltip=True, log_type="error")
self.clear_users_presences(uri)
await asyncio.sleep(60)
async def create_session(self):
if not self.session:
self.session = aiohttp.ClientSession()
async def handler(self):
await self.create_session()
self.tasks = [loop.create_task(self.handle_socket(uri)) for uri in list(set(self.config["urls"]))]
await asyncio.wait(self.tasks)
def start_ws(self):
self.main_task = asyncio.run_coroutine_threadsafe(self.handler(), loop)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-autostart', type=int, help='Iniciar presence automaticamente (tempo mínimo: 15)', default=0)
args = parser.parse_args()
RpcClient(autostart=args.autostart)