This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
133 lines (99 loc) · 3.4 KB
/
core.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
# Modules
import os
import sys
import time
import pypresence
from ..utils.errors import errors
from ..utils.colors import colored
from .chromium import get_chromium_website
from ..utils.logging import crash, verbose, info
# Presence class
class CustomPresence(pypresence.Presence):
def __init__(self, config, pipe = 0):
super().__init__(config["app_id"], pipe = pipe)
self.config = config
self.prev_app = None
self.prev_time = None
def kill(self, ctrl = True):
verbose("Shutting down script...")
self.close() # Close the RPC connection to allow other statuses
# Remove RPC dump (for security purposes)
if "--keep-rpc" not in sys.argv:
try:
os.remove("rpc.json")
except (PermissionError, FileNotFoundError):
pass
except Exception as err:
verbose("Failed to remove RPC dump with error", err)
# Print & exit
if ctrl:
info(colored("Status changer stopped via CTRL+C.", "red"))
exit()
def establish(self, keys):
self.keys = keys
try:
self.connect()
except (ConnectionRefusedError, pypresence.exceptions.InvalidPipe):
crash(errors["FailedConnect"])
def previous_time(self, app):
verbose("Checking the validity of stored elapsed time...")
if not self.prev_app:
self.prev_app = app
if self.prev_time:
if self.prev_app != app:
self.prev_app = app
self.prev_time = self.time()
verbose(" revalidated time!")
else:
verbose(" time is still valid.")
return self.prev_time
self.prev_time = self.time()
return self.prev_time
def set_status(self, app):
# Chromium browsers
if app.endswith("|CSPRC_CHROMIUM"):
name, app = get_chromium_website(self.config, app)
if name is None or app is None:
return
else:
name = app
app = self.config["applications"][app]
# Lobby data
lobby = {}
if self.config["allowJoining"]:
lobby = {
"join": self.keys["join"],
"party_id": self.keys["party"],
"party_size": [1, self.config["maxJoinSize"]]
}
# Elapsed time
elapsed = {}
if self.config["showElapsed"]:
elapsed["start"] = self.previous_time(app)
# Update our presence
try:
self.dump = self.update(
# State information
details = app["name"],
state = app["text"],
# Images
large_image = name,
large_text = app["name"],
small_image = self.config["smallImage"],
small_text = self.config["hoverText"],
# Buttons
buttons = self.config["buttons"],
# Game and lobbies
**lobby,
# Time
**elapsed
)
except pypresence.exceptions.InvalidID:
crash("Discord was closed while the status was running.")
self.kill(ctrl = False)
except KeyboardInterrupt:
self.kill()
return self.dump
@staticmethod
def time():
return round(time.time())