Skip to content

Commit

Permalink
Update to v1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
JustTemmie committed Sep 10, 2023
2 parents 8af3f22 + fcc818e commit 1af0682
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ steam-cookies.txt
cookies.txt
Development/
data/
BrowserTest/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

a simple script to fetch a Steam user's current game and related info, and display it as a Discord rich presence

![GitHub last commit (by committer)](https://img.shields.io/github/last-commit/justtemmie/steam-presence)
![stars](https://img.shields.io/github/stars/justTemmie/steam-presence)
![visitors](https://visitor-badge.laobi.icu/badge?page_id=justtemmie.steam-presence)

Made with ❤ by myself and our contributors

If you liked this project, please consider starring it <3


### Showcase

![Playing BTD6 with the script running](readmeimages/example1.png)
Expand Down Expand Up @@ -396,29 +405,29 @@ if you're running either Windows or MacOS i cannot really give you any help with

(if you do know a way to run this on startup on any of the mentioned systems, *please* create a pull request with an updated readme)

## Steam Deck / Linux with Systemd
# Installation to Automatically Start on Bootupt

If you have a Steam Deck, it is possible to have steam-presence start automatically when your Steam Deck boots. This method may also work on other Linux distributions that use per-user Systemd instances. If you (as a regular user) can run the command `systemctl --user status` successfully, then this should work.
## Automatic Installer

### Automatic Installer
Steam presence currently only supports automatically starting up on `Linux` and `MacOS`, if you know how to make it start on boot within windows, please make a PR, thanks!

simply run the `installer.sh` file
to install steam presence, simply run the `installer.sh` file

open konsole/another terminal and run this command:
to do this, open konsole or another terminal and run this command:

```
./installer.sh
```

### Manual Installation
## Manual Installation

The file `steam-presence.service` has more information and instructions.
The file `steam-presence.service` has more information and instructions on how to install it on linux.

## Linux (not using Systemd)

for those of you not running systemd
for those of you not running systemd, you might have cron installed!

create a file named `startup.sh` and paste in the code below, changing the path so it finds the main.py file.
if you have cron setup, you can also install the `screen` application, and then create a file named `startup.sh` and paste in the code below, changing the path so it finds steam presence's main.py file.

```
screen -dmS steamPresence bash -c 'python3 /home/USER/steam-presence/main.py'
Expand All @@ -428,6 +437,4 @@ make this script executable using `chmod +x startup.sh`

then run `crontab -e` and add `@reboot /home/USER/startup.sh` to the end of the crontab file.

if you've done these steps the script should launch itself after your computer turns on.

(this ends up using about 30MB of ram)
if you've done these steps the script should launch itself after your computer turns on.
89 changes: 68 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ def log(log):
def error(error):
print(f" ERROR: [{datetime.now().strftime('%b %d %Y - %H:%M:%S')}] {error}")

# i've gotten the error `requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))` a lot;
# this just seems to sometimes happens if your network conection is a bit wack, this function is a replacement for requests.get() and basically just does error handling and stuff
def makeWebRequest(URL, loops=0):
try:
r = requests.get(URL)
return r
except Exception as e:
if loops > 10:
error(f"falling back... the script got caught in a loop while fetching data from `{URL}`")
return "error"
elif "104 'Connection reset by peer'" in str(e):
return makeWebRequest(URL, loops+1)
else:
# error(f"falling back... exception met whilst trying to fetch data from `{URL}`\nfull error: {e}")
return "error"

def getMetaFile():
if exists(f"{dirname(__file__)}/data/meta.json"):
Expand Down Expand Up @@ -166,7 +181,7 @@ def removeChars(inputString: str, ignoredChars: str) -> str:
return inputString


def getImageFromSGDB():
def getImageFromSGDB(loops=0):
global coverImage
global coverImageText

Expand Down Expand Up @@ -220,7 +235,9 @@ def getImageFromSGDB():
# makes sure image is not NSFW
if entry[1] == False:
URL = f"https://www.steamgriddb.com/icon/{entry[5]}"
page = requests.get(URL)
page = makeWebRequest(URL)
if page == "error":
return

if page.status_code != 200:
error(f"status code {page.status_code} recieved when trying to web scrape SGDB, ignoring")
Expand Down Expand Up @@ -249,7 +266,9 @@ def getImageFromSGDB():

def getGameSteamID():
# fetches a list of ALL games on steam
r = requests.get(f"https://api.steampowered.com/ISteamApps/GetAppList/v0002/?key={steamAPIKey}&format=json")
r = makeWebRequest(f"https://api.steampowered.com/ISteamApps/GetAppList/v0002/?key={steamAPIKey}&format=json")
if r == "error":
return

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)
Expand Down Expand Up @@ -293,7 +312,9 @@ def getImageFromStorepage():

log("getting icon from the steam store")
try:
r = requests.get(f"https://store.steampowered.com/api/appdetails?appids={gameSteamID}")
r = makeWebRequest(f"https://store.steampowered.com/api/appdetails?appids={gameSteamID}")
if r == "error":
return

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)
Expand All @@ -320,7 +341,9 @@ def getImageFromStorepage():

def getGameReviews():
# get the review data for the steam game
r = requests.get(f"https://store.steampowered.com/appreviews/{gameSteamID}?json=1")
r = makeWebRequest(f"https://store.steampowered.com/appreviews/{gameSteamID}?json=1")
if r == "error":
return

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)
Expand Down Expand Up @@ -391,7 +414,9 @@ def getGameImage():


def getGamePrice():
r = requests.get(f"https://store.steampowered.com/api/appdetails?appids={gameSteamID}&cc=us")
r = makeWebRequest(f"https://store.steampowered.com/api/appdetails?appids={gameSteamID}&cc=us")
if r == "error":
return

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)
Expand Down Expand Up @@ -449,10 +474,15 @@ def getWebScrapePresence():

# checks what game the user is currently playing
def getSteamPresence():
r = requests.get(f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={steamAPIKey}&format=json&steamids={userID}")
r = makeWebRequest(f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={steamAPIKey}&format=json&steamids={userID}")

# 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 it errors out, just return the already asigned gamename
if r == "error":
return gameName


if r.status_code == 403:
error("Forbidden, Access to the steam API has been denied, please verify your steam API key")
Expand Down Expand Up @@ -501,7 +531,9 @@ def getSteamPresence():
def getSteamRichPresence():
for i in userID.split(","):
# userID type 3. <id3> = <id64> - 76561197960265728
pageRequest = requests.get(f"https://steamcommunity.com/miniprofile/{int(i) - 76561197960265728}")
pageRequest = makeWebRequest(f"https://steamcommunity.com/miniprofile/{int(i) - 76561197960265728}")
if pageRequest == "error":
return

# sleep for 0.2 seconds, this is done after every steam request, to avoid getting perma banned (yes steam is scuffed)
sleep(0.2)
Expand Down Expand Up @@ -542,9 +574,12 @@ def getSteamRichPresence():

# requests a list of all games recognized internally by discord, if any of the names matches
# the detected game, save the discord game ID associated with said title to RAM, this is used to report to discord as that game
def getGameDiscordID():
def getGameDiscordID(loops=0):
log(f"fetching the Discord game ID for {gameName}")
r = requests.get("https://discordapp.com/api/v8/applications/detectable")
r = makeWebRequest("https://discordapp.com/api/v8/applications/detectable")
if r == "error":
return


if r.status_code != 200:
error(f"status code {r.status_code} returned whilst trying to find the game's ID from discord")
Expand Down Expand Up @@ -681,6 +716,7 @@ def getLocalPresence():

def setPresenceDetails():
global activeRichPresence
global startTime

details = None
state = None
Expand Down Expand Up @@ -716,13 +752,20 @@ def setPresenceDetails():
if len(label) > 32:
label = f"{gameName} - {price}"
if len(label) > 32:
label = f"on steam - {price}"
label = f"get it on steam! - {price}"
if len(label) > 32:
label = f"on steam! - {price}"

buttons = [{"label": label, "url": f"https://store.steampowered.com/app/{gameSteamID}"}]


log("pushing presence to Discord")

# sometimes startTime is 0 when it reaches this point, which results in a crash
# i do *NOT* know how or why it does this, adding these 2 lines of code seems to fix it
if startTime == 0:
startTime = round((time()))

RPC.update(
# state field currently unused
details = details, state = state,
Expand Down Expand Up @@ -786,7 +829,11 @@ def verifyProjectVersion():
# checks if the program has any updates
def checkForUpdate():
URL = f"https://api.github.com/repos/JustTemmie/steam-presence/releases/latest"
r = requests.get(URL)
try:
r = requests.get(URL)
except Exception as e:
error(f"failed to check if a newer version is available, falling back...\nfull error: {e}")
return

if r.status_code != 200:
error(f"status code {r.status_code} recieved when trying to find latest version of steam presence, ignoring")
Expand Down Expand Up @@ -825,7 +872,7 @@ def checkForUpdate():
def main():
global currentVersion
# this always has to match the newest release tag
currentVersion = "v1.11"
currentVersion = "v1.12"

# check if there's any updates for the program
checkForUpdate()
Expand Down Expand Up @@ -1056,11 +1103,11 @@ def main():

if __name__ == "__main__":
main()
try:
pass
except Exception as e:
error(f"{e}\nautomatically restarting script in 60 seconds\n")
sleep(60)
python = sys.executable
log("restarting...")
os.execl(python, python, *sys.argv)
# try:
# pass
# except Exception as e:
# error(f"{e}\nautomatically restarting script in 60 seconds\n")
# sleep(60)
# python = sys.executable
# log("restarting...")
# os.execl(python, python, *sys.argv)

0 comments on commit 1af0682

Please sign in to comment.