Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JustTemmie committed May 16, 2023
2 parents c2cd6d6 + da09157 commit 350e4f0
Showing 1 changed file with 94 additions and 53 deletions.
147 changes: 94 additions & 53 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
answer = input("looks like either requests, pypresence, steamgrid, psutil, or beautifulSoup is not installed, do you want to install them? (y/n) ")
if answer.lower() == "y":
from os import system
print("installing req packages...")
print("installing required packages...")
system(f"python3 -m pip install -r {dirname(__file__)}/requirements.txt")

from pypresence import Presence
Expand Down Expand Up @@ -277,6 +277,8 @@ def getGameImage():
global coverImage
global coverImageText

coverImage = ""

log(f"fetching icon for {gameName}")

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

log("no image found in cache")

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

URL = f"https://steamcommunity.com/profiles/{userID}/"
page = requests.post(URL, cookies=cj)

if page.status_code == 403:
error("Forbidden, Access to Steam has been denied, please verify that your cookies are up to date")
# split on ',' in case of multiple userIDs
for i in userID.split(","):
URL = f"https://steamcommunity.com/profiles/{i}/"

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)

page = requests.post(URL, cookies=cj)

if page.status_code == 403:
error("Forbidden, Access to Steam has been denied, please verify that your cookies are up to date")

elif page.status_code != 200:
error(f"error code {page.status_code} met when trying to fetch game thru webscraping, ignoring")
elif page.status_code != 200:
error(f"error code {page.status_code} met when trying to fetch game thru webscraping, ignoring")

else:
soup = BeautifulSoup(page.content, "html.parser")
else:
soup = BeautifulSoup(page.content, "html.parser")

for element in soup.find_all("div", class_="profile_in_game_name"):
result = element.text.strip()
for element in soup.find_all("div", class_="profile_in_game_name"):
result = element.text.strip()

# the "last online x min ago" field is the same div as the game name
if "Last Online" not in result:

global isPlayingLocalGame

isPlayingLocalGame = False
return result

return
# the "last online x min ago" field is the same div as the game name
if "Last Online" not in result:

global isPlayingLocalGame

isPlayingLocalGame = False
return result

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

global isPlayingLocalGame

response = r.json()

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

if "gameextrainfo" in response["response"]["players"][0]:
game_title = response["response"]["players"][0]["gameextrainfo"]
isPlayingLocalGame = False
return game_title

global isPlayingLocalGame

# sort the players based on position in the config file
sorted_response = []
for steam_id in userID.split(","):
for player in response["response"]["players"]:
if player["steamid"] == steam_id:
sorted_response.append(player)
break


# loop thru every user in the response, if they're playing a game, save it
for i in range(0, len(sorted_response)):
if "gameextrainfo" in sorted_response[i]:
game_title = sorted_response[i]["gameextrainfo"]
if game_title != gameName:
log(f"found game {game_title} played by {sorted_response[i]['personaname']}")
isPlayingLocalGame = False
return game_title

return ""

Expand All @@ -376,28 +399,42 @@ def getSteamPresence():
# why steam does this is beyond me but it's fine
# 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
def getSteamRichPresence():
# userID type 3. <id3> = <id64> - 76561197960265728
pageRequest = requests.get(f"https://steamcommunity.com/miniprofile/{int(userID) - 76561197960265728}")

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)

if pageRequest.status_code != 200:
error(f"status code {pageRequest.status_code} returned whilst trying to fetch the enhanced rich presence info from steam, ignoring")
return
for i in userID.split(","):
# userID type 3. <id3> = <id64> - 76561197960265728
pageRequest = requests.get(f"https://steamcommunity.com/miniprofile/{int(i) - 76561197960265728}")

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)

if pageRequest.status_code != 200:
error(f"status code {pageRequest.status_code} returned whilst trying to fetch the enhanced rich presence info for steam user ID {i}, ignoring function")
return

# turn the page into proper html formating
soup = BeautifulSoup(pageRequest.content, "html.parser")
# find the correct entry where the rich presence is located
rich_presence = soup.find("span", class_="rich_presence")

# error handling
if rich_presence == None:
return

# save rich presence
global gameRichPresence
gameRichPresence = rich_presence.contents[0]
# turn the page into proper html formating
soup = BeautifulSoup(pageRequest.content, "html.parser")

global gameRichPresence

# double check if it's the correct game, yea i know we're basically fetching the game twice
# 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
# doesn't really change it that much, might change things around later
miniGameName = soup.find("span", class_="miniprofile_game_name")
if miniGameName != None:
if gameName != miniGameName.contents[0]:
# print(f"{gameName} doesn't match", soup.find("span", class_="miniprofile_game_name").contents[0])
break


# find the correct entry where the rich presence is located
rich_presence = soup.find("span", class_="rich_presence")

# save rich presence if it exists
if rich_presence != None:
gameRichPresence = rich_presence.contents[0]

# set the "enhanced rich presence" information back to nothing
if rich_presence == None:
gameRichPresence = ""



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

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

gamesFile.write(f"{processName.lower()}={processName.title()}\n")
gamesFile.write(f"{processName}={processName.title()}\n")
gamesFile.close()

isPlayingLocalGame = True
Expand Down Expand Up @@ -627,6 +664,11 @@ def main():
userID = ""
if type(config["USER_IDS"]) == str:
userID = config["USER_IDS"]
elif type(config["USER_IDS"]) == list:
for i in config["USER_IDS"]:
userID += f"{i},"
# remove the last comma
userID = userID[:-1]
else:
error(
"type error whilst reading the USER_IDS field, please make sure the formating is correct\n",
Expand Down Expand Up @@ -696,7 +738,6 @@ def main():
gameName = getWebScrapePresence()

if doSteamRichPresence and not isPlayingLocalGame:
gameRichPresence = ""
getSteamRichPresence()


Expand Down Expand Up @@ -758,8 +799,8 @@ def main():
setPresenceDetails()
print("----------------------------------------------------------")

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


if __name__ == "__main__":
Expand Down

0 comments on commit 350e4f0

Please sign in to comment.