Skip to content

Commit 350e4f0

Browse files
committed
2 parents c2cd6d6 + da09157 commit 350e4f0

File tree

1 file changed

+94
-53
lines changed

1 file changed

+94
-53
lines changed

main.py

Lines changed: 94 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
answer = input("looks like either requests, pypresence, steamgrid, psutil, or beautifulSoup is not installed, do you want to install them? (y/n) ")
3636
if answer.lower() == "y":
3737
from os import system
38-
print("installing req packages...")
38+
print("installing required packages...")
3939
system(f"python3 -m pip install -r {dirname(__file__)}/requirements.txt")
4040

4141
from pypresence import Presence
@@ -277,6 +277,8 @@ def getGameImage():
277277
global coverImage
278278
global coverImageText
279279

280+
coverImage = ""
281+
280282
log(f"fetching icon for {gameName}")
281283

282284
# checks if there's already an existing icon saved to disk for the game
@@ -299,6 +301,7 @@ def getGameImage():
299301
log(f"found icon for {gameName} in cache")
300302
return
301303

304+
log("no image found in cache")
302305

303306
if gridEnabled and coverImage == "":
304307
getImageFromSGDB()
@@ -315,30 +318,34 @@ def getWebScrapePresence():
315318
cj = cookielib.MozillaCookieJar(f"{dirname(__file__)}/cookies.txt")
316319
cj.load()
317320

318-
URL = f"https://steamcommunity.com/profiles/{userID}/"
319-
page = requests.post(URL, cookies=cj)
320-
321-
if page.status_code == 403:
322-
error("Forbidden, Access to Steam has been denied, please verify that your cookies are up to date")
321+
# split on ',' in case of multiple userIDs
322+
for i in userID.split(","):
323+
URL = f"https://steamcommunity.com/profiles/{i}/"
324+
325+
# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
326+
sleep(0.2)
327+
328+
page = requests.post(URL, cookies=cj)
329+
330+
if page.status_code == 403:
331+
error("Forbidden, Access to Steam has been denied, please verify that your cookies are up to date")
323332

324-
elif page.status_code != 200:
325-
error(f"error code {page.status_code} met when trying to fetch game thru webscraping, ignoring")
333+
elif page.status_code != 200:
334+
error(f"error code {page.status_code} met when trying to fetch game thru webscraping, ignoring")
326335

327-
else:
328-
soup = BeautifulSoup(page.content, "html.parser")
336+
else:
337+
soup = BeautifulSoup(page.content, "html.parser")
329338

330-
for element in soup.find_all("div", class_="profile_in_game_name"):
331-
result = element.text.strip()
339+
for element in soup.find_all("div", class_="profile_in_game_name"):
340+
result = element.text.strip()
332341

333-
# the "last online x min ago" field is the same div as the game name
334-
if "Last Online" not in result:
335-
336-
global isPlayingLocalGame
337-
338-
isPlayingLocalGame = False
339-
return result
340-
341-
return
342+
# the "last online x min ago" field is the same div as the game name
343+
if "Last Online" not in result:
344+
345+
global isPlayingLocalGame
346+
347+
isPlayingLocalGame = False
348+
return result
342349

343350
# checks what game the user is currently playing
344351
def getSteamPresence():
@@ -355,18 +362,34 @@ def getSteamPresence():
355362
error(f"error code {r.status_code} met when trying to fetch game, ignoring")
356363
return ""
357364

358-
global isPlayingLocalGame
359365

360366
response = r.json()
361367

362-
if len(response["response"]["players"]) == 0:
368+
# counts how many users you're supposed to get back, and checks if you got that many back
369+
if len(response["response"]["players"]) != userID.count(",") + 1:
363370
error("No account found, please verify that your user ID is correct")
364371
exit()
365372

366-
if "gameextrainfo" in response["response"]["players"][0]:
367-
game_title = response["response"]["players"][0]["gameextrainfo"]
368-
isPlayingLocalGame = False
369-
return game_title
373+
374+
global isPlayingLocalGame
375+
376+
# sort the players based on position in the config file
377+
sorted_response = []
378+
for steam_id in userID.split(","):
379+
for player in response["response"]["players"]:
380+
if player["steamid"] == steam_id:
381+
sorted_response.append(player)
382+
break
383+
384+
385+
# loop thru every user in the response, if they're playing a game, save it
386+
for i in range(0, len(sorted_response)):
387+
if "gameextrainfo" in sorted_response[i]:
388+
game_title = sorted_response[i]["gameextrainfo"]
389+
if game_title != gameName:
390+
log(f"found game {game_title} played by {sorted_response[i]['personaname']}")
391+
isPlayingLocalGame = False
392+
return game_title
370393

371394
return ""
372395

@@ -376,28 +399,42 @@ def getSteamPresence():
376399
# why steam does this is beyond me but it's fine
377400
# thank you so much to `wuddih` in this post for being the reason i found out about this https://steamcommunity.com/discussions/forum/1/5940851794736009972/ lmao
378401
def getSteamRichPresence():
379-
# userID type 3. <id3> = <id64> - 76561197960265728
380-
pageRequest = requests.get(f"https://steamcommunity.com/miniprofile/{int(userID) - 76561197960265728}")
381-
382-
# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
383-
sleep(0.2)
384-
385-
if pageRequest.status_code != 200:
386-
error(f"status code {pageRequest.status_code} returned whilst trying to fetch the enhanced rich presence info from steam, ignoring")
387-
return
402+
for i in userID.split(","):
403+
# userID type 3. <id3> = <id64> - 76561197960265728
404+
pageRequest = requests.get(f"https://steamcommunity.com/miniprofile/{int(i) - 76561197960265728}")
405+
406+
# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
407+
sleep(0.2)
408+
409+
if pageRequest.status_code != 200:
410+
error(f"status code {pageRequest.status_code} returned whilst trying to fetch the enhanced rich presence info for steam user ID {i}, ignoring function")
411+
return
388412

389-
# turn the page into proper html formating
390-
soup = BeautifulSoup(pageRequest.content, "html.parser")
391-
# find the correct entry where the rich presence is located
392-
rich_presence = soup.find("span", class_="rich_presence")
393-
394-
# error handling
395-
if rich_presence == None:
396-
return
397-
398-
# save rich presence
399-
global gameRichPresence
400-
gameRichPresence = rich_presence.contents[0]
413+
# turn the page into proper html formating
414+
soup = BeautifulSoup(pageRequest.content, "html.parser")
415+
416+
global gameRichPresence
417+
418+
# double check if it's the correct game, yea i know we're basically fetching the game twice
419+
# once thru here, and once thru the API... BUT OH WELL - the api is used for other things so people would still need a steam api key
420+
# doesn't really change it that much, might change things around later
421+
miniGameName = soup.find("span", class_="miniprofile_game_name")
422+
if miniGameName != None:
423+
if gameName != miniGameName.contents[0]:
424+
# print(f"{gameName} doesn't match", soup.find("span", class_="miniprofile_game_name").contents[0])
425+
break
426+
427+
428+
# find the correct entry where the rich presence is located
429+
rich_presence = soup.find("span", class_="rich_presence")
430+
431+
# save rich presence if it exists
432+
if rich_presence != None:
433+
gameRichPresence = rich_presence.contents[0]
434+
435+
# set the "enhanced rich presence" information back to nothing
436+
if rich_presence == None:
437+
gameRichPresence = ""
401438

402439

403440

@@ -500,7 +537,7 @@ def getLocalPresence():
500537
game = game[0].split("=")
501538

502539
# if there's a match
503-
if game[0].lower() == processName:
540+
if game[0].lower() == processName.lower():
504541
gameName = game[1]
505542
startTime = processCreationTime
506543
isPlayingLocalGame = True
@@ -514,7 +551,7 @@ def getLocalPresence():
514551
# if there wasn't a local entry for the game
515552
log(f"could not find a name for {processName}, adding an entry to games.txt")
516553

517-
gamesFile.write(f"{processName.lower()}={processName.title()}\n")
554+
gamesFile.write(f"{processName}={processName.title()}\n")
518555
gamesFile.close()
519556

520557
isPlayingLocalGame = True
@@ -627,6 +664,11 @@ def main():
627664
userID = ""
628665
if type(config["USER_IDS"]) == str:
629666
userID = config["USER_IDS"]
667+
elif type(config["USER_IDS"]) == list:
668+
for i in config["USER_IDS"]:
669+
userID += f"{i},"
670+
# remove the last comma
671+
userID = userID[:-1]
630672
else:
631673
error(
632674
"type error whilst reading the USER_IDS field, please make sure the formating is correct\n",
@@ -696,7 +738,6 @@ def main():
696738
gameName = getWebScrapePresence()
697739

698740
if doSteamRichPresence and not isPlayingLocalGame:
699-
gameRichPresence = ""
700741
getSteamRichPresence()
701742

702743

@@ -758,8 +799,8 @@ def main():
758799
setPresenceDetails()
759800
print("----------------------------------------------------------")
760801

761-
# wait for a 20 seconds every time we query anything, to avoid getting banned from the steam API
762-
sleep(20)
802+
# sleep for a 20 seconds for every user we query, to avoid getting banned from the steam API
803+
sleep(20 * (userID.count(",") + 1))
763804

764805

765806
if __name__ == "__main__":

0 commit comments

Comments
 (0)