This repository was archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
300 lines (247 loc) · 8.1 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
import datetime
import getpass
import os
import tkinter.messagebox as msgbox
import webbrowser
from configparser import ConfigParser
from tkinter import *
from tkinter.ttk import *
import psutil
import pypresence
from ttkthemes import ThemedTk
conf = ConfigParser()
conf.read("settings.ini")
client_id = "785146044056076328" # Discord Application Client ID. Replace with your own, if you dont use the official build.
settings = ["autostart_presence", "selected_char", "show_location"]
for x in settings:
if not conf.has_option("GENERAL", x):
conf["GENERAL"] = {
"autostart_presence": False,
"show_location": True,
"selected_char": "",
}
with open("settings.ini", "w") as config:
conf.write(config)
break
def get_characters():
"""
returns a list of all characters
"""
chars = []
logs = [
x for x in os.scandir(log_location)
] # Making it a list instead of a custom iterator
for log in logs:
with open(log.path, "r") as f:
try:
lines = [x.replace("\n", "") for x in f.readlines()]
chars.append(lines[2].split("Listener: ")[1])
except:
pass
chars = list(dict.fromkeys(chars))
return chars
def log_lines():
"""
returns list of lines in latest log
"""
logs = [
x for x in os.scandir(log_location)
] # Making it a list instead of a custom iterator
logs.reverse()
char = selected_char.get()
for log in logs:
with open(log.path, "r") as f: # open latest log
lines = [
x.replace("\n", "") for x in f.readlines()
] # Lines without new line char
try:
if lines[2].split("Listener: ")[1] == char:
return lines
except IndexError:
pass
def details(lines=None):
"""
sets current activity and status
"""
if lines == None:
lines = log_lines()
char = lines[2].split("Listener: ")[1]
start_time = datetime.datetime.strptime(
lines[3].split("Session Started: ")[1], "%Y.%m.%d %H:%M:%S"
).replace(
tzinfo=datetime.timezone.utc
) # get time from log file (i know, super complicated)
start_time = (
start_time.astimezone().timestamp()
) # convert it to your actual timezone
details = {
"char": char,
"start_time": start_time,
"location": "Unknown",
"autopilot": False,
"docked": False,
"station": False,
} # set default values
for line in lines:
if "Undocking from" in line:
details["location"] = line.split(" to ")[1].replace(" solar system.", "")
details["docked"] = False
elif "Jumping from" in line:
details["location"] = line.split(" to ")[1]
details["docked"] = False
elif "Autopilot engaged" in line:
details["autopilot"] = True
elif "Autopilot disabled" in line:
details["autopilot"] = False
elif "Requested to dock at" in line:
details["station"] = line.split("Requested to dock at ")[1].replace(
" station", ""
)
elif "Your docking request has been accepted." in line:
details["docked"] = True
return details
def loop():
global run_loop
if run_loop:
d = details()
unknown_location = False
if not d["docked"] and d["location"] == "Unknown":
d["docked"] = ""
d["location"] = "Location Unknown"
if d["autopilot"] == False:
small_img = "manual"
small_txt = "Manual flight"
else:
small_img = "auto"
small_txt = "Autopilot"
unknown_location = True
# here comes the logic part
if not unknown_location:
if d["autopilot"] == False:
if d["docked"]:
small_img = "docked"
small_txt = "Docked"
else:
small_img = "manual"
small_txt = "Manual flight"
else:
small_img = "auto"
small_txt = "Autopilot"
if not show_location.get():
d["location"] = ""
if d["docked"]:
d["docked"] = "Docked"
else:
d["docked"] = "In Space"
else:
if d["docked"]:
d["docked"] = "Docked at "
if d["station"]:
d["location"] = d["station"]
else:
d["docked"] = "In Space of "
presence.update(
state=d["docked"] + d["location"],
details=d["char"],
large_image="eve-logo",
large_text="EVE Discord Presence --> Get it on Github: https://toasteruwu.com/EVE-Presence",
small_image=small_img,
small_text=small_txt,
start=int(d["start_time"]),
)
status_message.config(
text=f"{d['char']}\n{d['docked']+d['location']}\n{small_txt}"
)
tk.after(5000, loop) # repeat after 5 seconds
def change_loop():
global run_loop, loop_button
if run_loop == False:
try:
presence.connect()
except pypresence.exceptions.InvalidPipe:
msgbox.showerror(
"An Error occoured",
"Discord doesnt seem to run. Please start Discord and then try again.",
)
exit()
if "eve_crashmon.exe" not in (p.name() for p in psutil.process_iter()):
return msgbox.showwarning(
"EVE Online is not running",
"To use this program, you need to have EVE Online started.",
)
run_loop = True
loop_button["text"] = "Stop Presence"
else:
run_loop = False
loop_button["text"] = "Start Presence"
presence.clear()
presence.close()
status_message.config(text="Presence offline")
def set_conf(*args):
conf["GENERAL"] = {
"show_location": show_location.get(),
"autostart_presence": autostart_presence.get(),
"selected_char": selected_char.get(),
}
with open("settings.ini", "w") as config:
conf.write(config)
presence = pypresence.Presence(client_id)
tk = ThemedTk("EVE Online - Discord Presence", theme="arc")
tk.title("EVE Online - Discord Presence")
try:
tk.iconbitmap("icon.ico")
except:
pass
tk.geometry("350x150")
frame = Frame(tk)
run_loop = False
user = getpass.getuser()
log_location = f"C:\\Users\\{user}\\Documents\\EVE\\logs\\Gamelogs"
show_location = BooleanVar(frame, value=conf.getboolean("GENERAL", "show_location"))
autostart_presence = BooleanVar(
frame, value=conf.getboolean("GENERAL", "autostart_presence")
)
selected_char = StringVar(frame, value=conf.get("GENERAL", "selected_char"))
loop_button = Button(frame, text="Start Presence", command=change_loop)
loop_button.grid(column=1, row=1)
location_box = Checkbutton(
frame,
text="Show Location",
variable=show_location,
onvalue=True,
offvalue=False,
command=set_conf,
)
location_box.grid(column=0, row=0)
if autostart_presence.get():
change_loop()
autostart_box = Checkbutton(
frame,
text="Autostart Presence",
variable=autostart_presence,
onvalue=True,
offvalue=False,
command=set_conf,
)
autostart_box.grid(column=0, row=1)
chars = get_characters()
if selected_char.get() == "":
selected_char.set(chars[0])
set_conf()
character_dropdown = OptionMenu(
frame, selected_char, selected_char.get(), *chars, command=set_conf
)
character_dropdown.grid(column=1, row=0)
status_message = Message(frame, width=300)
status_message.grid(column=0, columnspan=2, row=2)
credit_button = Button(
frame,
text="Github Page",
command=lambda: webbrowser.open(
"https://github.com/ToasterUwU/EVE-Discord-Presence"
),
)
credit_button.grid(column=0, columnspan=2, row=3)
frame.place(relx=0.5, rely=0.5, anchor="c")
tk.after(0, loop)
tk.mainloop()