Skip to content

Commit ee9fdc0

Browse files
committed
fixed memory leak
fixed an issue where the script may error out indefinitely, printing an error message every 15 seconds - at one point this app was spending about 2 gigabytes of ram lmao
1 parent 8a11af2 commit ee9fdc0

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

main.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ def try_fetching_icon(gameName, steamGridAppID):
141141

142142

143143
if res == 16:
144-
print(f"could not find icon for {gameName} either ignore this or manually add one to icons.txt")
144+
with open(f'{dirname(__file__)}/icons.txt', 'a') as icons:
145+
if gameName not in icons:
146+
icons.write(f"{gameName}=\"\"\n")
147+
print(f"could not find icon for {gameName} either ignore this or manually add one to icons.txt")
148+
icons.close()
145149

146150

147151
# fetches the icon from steamgrid
@@ -155,7 +159,7 @@ def get_steam_grid_icon(gameName):
155159
if gameName == game:
156160
URL = i.split("=")[1]
157161
return URL
158-
162+
159163
print(f"fetching icon for {gameName}")
160164

161165
results = sgdb.search_game(gameName)
@@ -246,20 +250,20 @@ def set_game(
246250
large_image = game_icon, large_text = large_text,
247251
small_image = custom_icon_url, small_text = custom_icon_text
248252
)
249-
250253
print("reconnected to discord")
251254

252255
except:
253256
return None
254257

255258

256-
def program():
259+
def program(cycles):
257260
global RPC
258261
global sgdb
259262
global DEFAULT_APP_ID
260263

261264
# get data from the config file
262-
print("fetching config data...")
265+
if cycles <= 20:
266+
print("fetching config data...")
263267
config = get_config()
264268
if config["STEAM_API_KEY"] == "KEY":
265269
print(f"ERROR: [{datetime.now().strftime('%d-%b-%Y %H:%M:%S')}] Please set your Steam API key in the config file.")
@@ -293,7 +297,8 @@ def program():
293297

294298
# initialize the steam grid database object
295299
if GRID_ENABLED:
296-
print("intializing the SteamGrid database...")
300+
if cycles <= 20:
301+
print("intializing the SteamGrid database...")
297302
sgdb = SteamGridDB(GRID_KEY)
298303

299304

@@ -320,12 +325,14 @@ def program():
320325
app_id = get_game_id(gameName)
321326

322327
# initialize the discord rich presence object
323-
print("intializing the rich presence...")
328+
if cycles <= 20:
329+
print("intializing the rich presence...")
324330
RPC = Presence(client_id=app_id)
325331
RPC.connect()
326332

327333
# everything ready!
328-
print("everything is ready!")
334+
if cycles <= 20:
335+
print("everything is ready!")
329336

330337

331338
while True:
@@ -355,7 +362,6 @@ def program():
355362
gameName = web_scrape_steam_presence(USER_ID)
356363
scraped = True
357364

358-
359365

360366
if gameName is None:
361367
# note, this completely hides your current rich presence
@@ -375,10 +381,12 @@ def program():
375381
else:
376382
set_game(do_game_title, None, coverImage, startTime, do_custom_state, custom_state, do_custom_icon, custom_icon_url, custom_icon_text)
377383

384+
378385
# if the game has changed, restart the rich presence client with that new app ID
379386
if (oldGameName != gameName and gameName != None) or (app_id == DEFAULT_APP_ID and scraped == True):
380387
startTime = round(time())
381-
print(f"game changed to \"{gameName}\"")
388+
if cycles <= 20:
389+
print(f"game changed to \"{gameName}\"")
382390

383391
app_id = get_game_id(gameName)
384392
if app_id == DEFAULT_APP_ID and scraped == True:
@@ -400,13 +408,16 @@ def program():
400408
sleep(45)
401409

402410

403-
def try_running():
411+
def try_running(cycles = 0):
404412
try:
405-
program()
413+
program(cycles)
406414
except Exception as e:
407-
print(f"could not connect to discord: {e}")
415+
if cycles <= 20:
416+
print(f"could not connect to discord: {e}")
417+
if cycles == 20:
418+
print("failed to connect 20 times, the script will now stop logging errors - as if it continues to do so this script will end up taking gigabytes worth of memory in a couple hours")
408419
sleep(15)
409-
try_running()
420+
try_running(cycles+1)
410421

411422
if __name__ == "__main__":
412423
try_running()

0 commit comments

Comments
 (0)