Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicconike committed Jun 5, 2024
1 parent f0b11e8 commit 03a998f
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 115 deletions.
9 changes: 5 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#Re
STEAM_API_KEY=<YOUR_STEAM_API_KEY>
STEAM_ID=<YOUR_STEAM_ID> #17 digit SteamID(steamID64 [Decimals])
STEAM_CUSTOM_ID=<YOUR_STEAM_CUSTOM_ID>
# Mandatory Envioronment Variables for Steam Stats
INPUT_GH_TOKEN=<YOUR_GITHUB_TOKEN_KEY>
INPUT_STEAM_API_KEY=<YOUR_STEAM_API_KEY>
INPUT_STEAM_ID=<YOUR_STEAM_ID> #17 digit SteamID(steamID64 [Decimals])
INPUT_STEAM_CUSTOM_ID=<YOUR_STEAM_CUSTOM_ID>
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ on:
- master

jobs:
quality-check:
build:
name: Quality Check
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/steam-stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get update
sudo apt install google-chrome-stable
sudo apt-get install chromium-chromedriver
sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
python -m pip install --upgrade pip
sudo apt-get install -y libxtst6 libxss1 libgconf-2-4 libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxdamage1 libxrandr2 libgbm1 libasound2
pyppeteer-install
python -m pip install pyppeteer
pip install -r requirements.txt
shell: python

- name: Update Steam Stats in README
env:
STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}
STEAM_ID: ${{ secrets.STEAM_ID }}
STEAM_CUSTOM_ID: ${{ secrets.STEAM_CUSTOM_ID }}
WORKSHOP_STATS: true
LOG_SCALE: true
run: python api/main.py
env:
INPUT_STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}
INPUT_STEAM_ID: ${{ secrets.STEAM_ID }}
INPUT_STEAM_CUSTOM_ID: ${{ secrets.STEAM_CUSTOM_ID }}
INPUT_WORKSHOP_STATS: True
INPUT_LOG_SCALE: True
PYPPETEER_HOME: /home/runner/work/Steam-Stats/Steam-Stats/pyppeteer_home

- name: Commit and push if changes
- name: Commit and Push
run: |
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"
Expand Down
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ representative at an online or offline event.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
nicconike6@gmail.com.
*nicconike6@gmail.com*.
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
![pylint](https://img.shields.io/badge/PyLint-10.00-brightgreen?logo=python&logoColor=white)
[![Visitor Badge](https://badges.pufler.dev/visits/nicconike/steam-stats)](https://badges.pufler.dev)

> ### From one Passionate Gamer and Developer to Another 🍻
## Prerequisites
1. **Steam Web API Key:** API key is important to fetch your account details and for that you will require a key which you can create for your account [here](https://steamcommunity.com/dev).
2. **Github API Token:**
1. Goto your Github profile -> Settings -> Developer Settings -> Personal Access Tokens -> Tokens(Classic) or just click [here](https://github.com/settings/tokens)
2. Generate new token -> Generate new token(classic)
3. Select scopes -> repo and workflow only
4. Set a reasonable expiration date
3. **Steam ID:** You can get your 64-bit Steam id (SteamID64 - 17 digit number) by clicking on your profile name in the top right corner in steam desktop client, select "Account Details" and your Steam ID will be displayed directly under your account name
4. **Steam Custom ID:** Open the Steam desktop application, click on your profile name in the top right corner. Select "View Profile" and your custom URL will be displayed in the URL bar. From this url you will know your Steam Custom ID

### Samples/Features
1. Steam Player Summary
2. Recently Played Games from Steam in the Last 2 Weeks
3. Steam Workshop Stats (If Available)

<!-- Steam-Stats start -->
![Steam Summary](https://github.com/Nicconike/Steam-Stats/blob/master/assets/steam_summary.png)
![Steam Summary](https://github.com/Nicconike/Steam-Stats/blob/master/assets/recently_played_games.png)
Expand Down
22 changes: 13 additions & 9 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
name: "Steam Stats"
description: "Update your GitHub profile with your latest Steam stats"
author: "Your Name <your.email@example.com>"
description: "Update your GitHub Profile with your latest Steam Stats"
author: "Nicconike"
inputs:
GH_TOKEN:
description: "GitHub access token with Repo scope"
required: true
default: ${{ github.token }}
STEAM_API_KEY:
description: "Steam API Key"
description: "Steam Web API Key"
required: true
STEAM_ID:
description: "64bit SteamID of the user"
Expand All @@ -12,13 +16,13 @@ inputs:
description: "Steam Custom ID"
required: true
WORKSHOP_STATS:
description: "Include Steam Workshop Stats"
description: "Include Steam Workshop Stats from your Steam Account"
required: false
default: "false"
default: "False"
LOG_SCALE:
description: "Use logarithmic scale for recently played games"
description: "Use logarithmic scale for plotting recently played games in steam for the last 2 weeks"
required: false
default: "normal"
default: "False"
runs:
using: composite
steps:
Expand All @@ -43,8 +47,8 @@ runs:
STEAM_API_KEY: ${{ inputs.steam_api_key }}
STEAM_ID: ${{ inputs.steam_id }}
STEAM_CUSTOM_ID: ${{ inputs.steam_custom_id }}
INPUT_WORKSHOP: ${{ inputs.workshop }}
INPUT_PLOT_SCALE: ${{ inputs.plot_scale }}
WORKSHOP_STATS: ${{ inputs.workshop_stats }}
LOG_SCALE: ${{ inputs.log_scale }}
branding:
icon: bar-chart-2
color: blue
54 changes: 34 additions & 20 deletions api/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ def generate_card_for_player_summary(player_data):
<style>
.card {{
width: 100%;
max-width: 500px;
max-width: 400px;
margin: auto;
border: 2px solid #000;
padding: 20px;
padding: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border-radius: 10px;
background-color: #fff;
}}
.avatar {{
width: 100px;
height: 100px;
width: 80px;
height: 80px;
border-radius: 50%;
margin: auto;
}}
Expand All @@ -160,32 +160,46 @@ def generate_card_for_player_summary(player_data):
height: 24px;
vertical-align: middle;
}}
.info-container {{
display: flex;
justify-content: space-between;
margin-top: 10px;
}}
.info-left, .info-right {{
width: 48%;
}}
</style>
</head>
<body>
<div class="card">
<div class="content">
<img id="avatar" class="avatar" src="{avatarfull}" alt="Avatar">
<h2 id="name">Name: {personaname}</h2>
<p id="status">Status: {personastate_value}</p>
<p id="country">Country: <span id="country-code">{loccountrycode}</span>
<img id="flag" class="flag"
src="https://flagcdn.com/w320/{loccountrycode.lower()}.png" alt="Flag">
</p>
<p id="lastlogoff">Last Logoff: {lastlogoff_str}</p>
<p id="lastlogoff">Gaming Since: {timecreated_str}</p>
<div class="info-container">
<div class="info-left">
<p id="status">Status: {personastate_value}</p>
<p id="country">Country: <span id="country-code">{loccountrycode}</span>
<img id="flag" class="flag"
src="https://flagcdn.com/w320/{loccountrycode.lower()}.png" alt="Flag">
</p>
</div>
<div class="info-right">
<p id="lastlogoff">Last Logoff: {lastlogoff_str}</p>
<p id="timecreated">Gaming Since: {timecreated_str}</p>
</div>
</div>
{"<p id='game'>Currently Playing: <span id='game-info'>" +
gameextrainfo + "</span></p>" if gameextrainfo else ""}
</div>
</div>
</body>
</html>
"""
with open("assets/steam_summary.html", "w", encoding="utf-8") as file:
with open("../assets/steam_summary.html", "w", encoding="utf-8") as file:
file.write(html_content)

convert_html_to_png("assets/steam_summary.html",
"assets/steam_summary.png", ".card")
convert_html_to_png("../assets/steam_summary.html",
"../assets/steam_summary.png", ".card")

return (
"![Steam Summary]"
Expand Down Expand Up @@ -244,11 +258,11 @@ def generate_card_for_played_games(games_data):
</html>
"""

with open("assets/recently_played_games.html", "w", encoding="utf-8") as file:
with open("../assets/recently_played_games.html", "w", encoding="utf-8") as file:
file.write(html_content)

convert_html_to_png("assets/recently_played_games.html",
"assets/recently_played_games.png", ".card")
convert_html_to_png("../assets/recently_played_games.html",
"../assets/recently_played_games.png", ".card")

return (
"![Steam Summary]"
Expand Down Expand Up @@ -332,11 +346,11 @@ def generate_card_for_steam_workshop(workshop_stats):
</body>
</html>
"""
with open("assets/steam_workshop_stats.html", "w", encoding="utf-8") as file:
with open("../assets/steam_workshop_stats.html", "w", encoding="utf-8") as file:
file.write(html_content)

convert_html_to_png("assets/steam_workshop_stats.html",
"assets/steam_workshop_stats.png", ".card")
convert_html_to_png("../assets/steam_workshop_stats.html",
"../assets/steam_workshop_stats.png", ".card")

return (
"![Steam Summary]"
Expand Down
43 changes: 19 additions & 24 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@

load_dotenv()

# Secrets Configuration
STEAM_API_KEY = os.getenv("STEAM_API_KEY")
STEAM_CUSTOM_ID = os.getenv("STEAM_CUSTOM_ID")
# Required Secrets Configuration
STEAM_ID = os.environ["INPUT_STEAM_ID"]
STEAM_API_KEY = os.environ["INPUT_STEAM_API_KEY"]
STEAM_CUSTOM_ID = os.environ["INPUT_STEAM_CUSTOM_ID"]

workshop_stats = os.getenv("WORKSHOP_STATS", "false").lower() == "true"
plot_scale = os.getenv("PLOT_SCALE", "normal").lower()
# Optional Feature Flags
WORKSHOP_STATS = os.getenv("WORKSHOP_STATS", "false").lower() == "true"
LOG_SCALE = os.getenv("LOG_SCALE", "false").lower() == "true"

# Verify that the environment variables are loaded correctly
if not STEAM_CUSTOM_ID:
raise ValueError(
"Missing STEAM_ID in environment variables")


def update_readme(markdown_data, start_marker, end_marker, readme_path="README.md"):
def update_readme(markdown_data, start_marker, end_marker, readme_path="../README.md"):
"""Updates the README.md file with the provided Markdown content within specified markers."""
# Read the current README content
with open(readme_path, "r", encoding="utf-8") as file:
Expand Down Expand Up @@ -58,7 +55,6 @@ def update_readme(markdown_data, start_marker, end_marker, readme_path="README.m

player_summary = get_player_summaries()
recently_played_games = get_recently_played_games()
links = fetch_workshop_item_links(STEAM_CUSTOM_ID, STEAM_API_KEY)

USER_MARKDOWN_CONTENT = ""
if player_summary and recently_played_games:
Expand All @@ -76,26 +72,25 @@ def update_readme(markdown_data, start_marker, end_marker, readme_path="README.m
else:
print("Failed to fetch Steam Summary & Games Data")

if workshop_stats is True:
if USER_MARKDOWN_CONTENT:
update_readme(USER_MARKDOWN_CONTENT,
"<!-- Steam-Stats start -->", "<!-- Steam-Stats end -->")
print("README.md has been successfully updated with Steam User Stats")

if WORKSHOP_STATS is True:
WORKSHOP_MARKDOWN_CONTENT = ""
links = fetch_workshop_item_links(STEAM_CUSTOM_ID, STEAM_API_KEY)
if links:
workshop_data = fetch_all_workshop_stats(links)
WORKSHOP_MARKDOWN_CONTENT += generate_card_for_steam_workshop(
workshop_data)
print("Retrieved all Workshop Stats")
update_readme(WORKSHOP_MARKDOWN_CONTENT,
"<!-- Steam-Workshop start -->", "<!-- Steam-Workshop end -->")
print("README.md has been successfully updated with Steam Workshop Stats")
else:
print("No workshop content was found")

if USER_MARKDOWN_CONTENT and WORKSHOP_MARKDOWN_CONTENT:
update_readme(USER_MARKDOWN_CONTENT,
"<!-- Steam-Stats start -->", "<!-- Steam-Stats end -->")
update_readme(WORKSHOP_MARKDOWN_CONTENT,
"<!-- Steam-Workshop start -->", "<!-- Steam-Workshop end -->")
print("README.md has been successfully updated")
else:
print("Failed to fetch or process data")

end_time = time.time() # End the timer
total_time = end_time-start_time
total_time = round(total_time, 3) # Total time
total_time = round(end_time-start_time, 3) # Total time
print(f"Total Execution Time: {total_time} seconds")
11 changes: 3 additions & 8 deletions api/steam_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

load_dotenv()

# Secrets Configuration
STEAM_API_KEY = os.getenv("STEAM_API_KEY")
STEAM_ID = os.getenv("STEAM_ID")

# Verify that the environment variables are loaded correctly
if not STEAM_API_KEY or not STEAM_ID:
raise ValueError(
"Missing STEAM_API_KEY or STEAM_ID in environment variables")
# Required Secrets Configuration
STEAM_ID = os.environ["INPUT_STEAM_ID"]
STEAM_API_KEY = os.environ["INPUT_STEAM_API_KEY"]

# A reasonable timeout for the request (connection and read timeout)
REQUEST_TIMEOUT = (10, 15)
Expand Down
25 changes: 4 additions & 21 deletions api/steam_workshop.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Scrape Steam Workshop Data"""
import json
import os
import requests
from bs4 import BeautifulSoup, Tag
from dotenv import load_dotenv

load_dotenv()

# Secrets Configuration
STEAM_API_KEY = os.getenv("STEAM_API_KEY")
STEAM_CUSTOM_ID = os.getenv("STEAM_CUSTOM_ID")
# Required Secrets Configuration
STEAM_API_KEY = os.environ["INPUT_STEAM_API_KEY"]
STEAM_CUSTOM_ID = os.environ["INPUT_STEAM_CUSTOM_ID"]


# A reasonable timeout for the request (connection and read timeout)
REQUEST_TIMEOUT = (10, 15)
Expand Down Expand Up @@ -199,20 +199,3 @@ def fetch_all_workshop_stats(item_links):
"total_current_favorites": total_current_favorites,
"individual_stats": all_stats
}


def save_to_file(data, filename):
"""Save fetched data to a file in JSON format"""
if data is not None:
with open(filename, 'w', encoding='utf-8') as file:
# Use json.dump to write the JSON data to the file
json.dump(data, file, indent=4)
print(f"Data saved to {filename}")
else:
print("No data to save")


if __name__ == "__main__":
itemlinks = fetch_workshop_item_links(STEAM_CUSTOM_ID, STEAM_API_KEY)
workshop_data = fetch_all_workshop_stats(itemlinks)
save_to_file(workshop_data, "workshop_data.json")
Loading

1 comment on commit 03a998f

@vercel
Copy link

@vercel vercel bot commented on 03a998f Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.