Skip to content

Commit

Permalink
fix: Update code to gracefully handle loccountrycode var
Browse files Browse the repository at this point in the history
docs: Update Readme
ci: update steam stats workflow
  • Loading branch information
Nicconike committed Jul 11, 2024
1 parent dc42d6b commit e50ca76
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 475 deletions.
29 changes: 14 additions & 15 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,15 @@ jobs:
restore-keys: |
${{ runner.os }}-pip-
- name: Run Pylint
- name: Run Pylint and Generate Badge
id: run-pylint
run: |
git add .
python -m pip install --upgrade pip
pip install pylint
pylint api tests > pylint-report.txt || true
pylint_output=$(pylint api tests || true)
echo "$pylint_output"
- name: Generate Pylint Badge
if: github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
score=$(grep -oP 'Your code has been rated at \K[0-9]+\.[0-9]+' pylint-report.txt)
if [ -z "$score" ]; then
score="0.0"
fi
score=$(echo "$pylint_output" | grep -oP 'Your code has been rated at \K[0-9]+\.[0-9]+' || echo "0.0")
color="red"
if (( $(echo "$score == 10" | bc -l) )); then
color="brightgreen"
Expand All @@ -103,10 +95,17 @@ jobs:
elif (( $(echo "$score >= 6" | bc -l) )); then
color="red"
fi
badge="![Pylint](https://img.shields.io/badge/Pylint-$score-$color?logo=python)"
sed -i 's|!\[Pylint\](.*)|'"$badge"'|' README.md
echo "PYLINT_BADGE=$badge" >> $GITHUB_OUTPUT
- name: Update README with Pylint Badge
if: github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
sed -i 's|!\[Pylint\](.*)|${{ steps.run-pylint.outputs.PYLINT_BADGE }}|' README.md
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git add README.md
git commit -m "chore: Update Pylint Badge"
git push origin HEAD:master
git diff --quiet && git diff --staged --quiet || (git commit -m "chore: Update Pylint Badge" && git push origin HEAD:master)
43 changes: 6 additions & 37 deletions .github/workflows/steam-stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,13 @@ jobs:
if: github.actor != 'dependabot[bot]' || github.actor != 'github-actions[bot]' || github.actor != 'protected-auto-commits[bot]' && github.base_ref == 'master'
name: Steam Stats
runs-on: ubuntu-latest
permissions:
contents: write
container:
image: nicconike/steam-stats:latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
token: ${{ secrets.GH_TOKEN }}

- name: Update Steam Stats
run: |
export PYTHONPATH=$(pwd)
python api/main.py
- name: Run Steam Stats
uses: docker://nicconike/steam-stats:latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}
INPUT_STEAM_ID: ${{ vars.STEAM_ID }}
INPUT_STEAM_CUSTOM_ID: ${{ vars.STEAM_CUSTOM_ID }}
INPUT_WORKSHOP_STATS: True
INPUT_LOG_SCALE: True
HOME: /root
STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}
STEAM_ID: ${{ vars.STEAM_ID }}
STEAM_CUSTOM_ID: ${{ vars.STEAM_CUSTOM_ID }}
WORKSHOP_STATS: True
GITHUB_REPOSITORY: ${{ github.repository }}

- name: Configure Git & Commit Changes
run: |
git config --global --add safe.directory /__w/Steam-Stats/Steam-Stats
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"
git add .
if ! git diff-index --quiet HEAD; then
git commit -m "chore: Update Steam Stats"
if [ "${{ github.ref }}" = "refs/heads/master" ]; then
git pull --rebase origin master
git push origin HEAD:master
else
echo "Not on master branch, skipping push"
fi
else
echo "No changes to commit"
fi
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Use the official Playwright image from the Docker Hub
# Use the official Playwright image from Microsoft
FROM mcr.microsoft.com/playwright/python:v1.44.0-jammy

# Set environment variables
Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ schedule:
```
> [!IMPORTANT]
> Please don't forget any of the steps mentioned in the prerequisites else the Github Action will not work.
> Also, make sure that you have set the country correctly in your Steam Account.
>
> You can refer the [Steam Stats Wiki](https://github.com/Nicconike/Steam-Stats/wiki) if you have any questions related to any of the steps mentioned in [Prerequisites](#Prerequisites).
***
Expand Down Expand Up @@ -89,27 +90,31 @@ If you are new to **Github Secrets** then you can checkout this official doc [he

**Sample Workflow File**

`steam-stats.yml`

```yml
name: Steam Stats
on:
push:
branches: master
schedule:
# Runs every Monday at 12AM IST (UTC+5:30)
- cron: "30 18 * * 0"
workflow_dispatch:
jobs:
update-readme:
steam-stats:
name: Steam Stats
runs-on: ubuntu-latest
steps:
- uses: nicconike/steam-stats@master
with:
STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}
STEAM_ID: ${{ vars.STEAM_ID }}
STEAM_CUSTOM_ID: ${{ vars.STEAM_CUSTOM_ID }}
WORKSHOP_STATS: True # Optional
STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }} # Replace with your created key saved in actions secrets
STEAM_ID: ${{ vars.STEAM_ID }} # Replace with your Steam ID env var
STEAM_CUSTOM_ID: ${{ vars.STEAM_CUSTOM_ID }} # Replace with your custom ID env var
LOG_SCALE: True # Optional
WORKSHOP_STATS: True # Optional
```

Checkout this real time usage example in a github repo from [here](https://github.com/Nicconike/Nicconike?tab=readme-ov-file#gaming-) and also the github actions [workflow file](https://github.com/Nicconike/Nicconike/blob/master/.github/workflows/steam-stats.yml).
Expand Down Expand Up @@ -142,11 +147,12 @@ If you are using this project and are really happy with it, then there are few w
- Starring and Sharing the project
- Donations through [GitHub Sponsers](https://github.com/sponsors/Nicconike) or whichever platform you like. So, that I can create more projects like these and play more games🎮🎧

**Thanks!🫡**
#### **Thanks!🫡**
Other Major Platforms Coming Soon:tm:😉

***
Created with 🐍 & ❤️ by [Nicco](https://github.com/Nicconike)

[^1]: Unfortunately, Steam Web API doesn't support Web Sockets so the profile status cannot be updated in real time as it gets updated in steam profile 🥲

[^2]: Modified `style.css` code according to project requirements
[^2]: Modified design code according to project requirements
33 changes: 19 additions & 14 deletions api/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
repo_owner, repo_name = os.environ["GITHUB_REPOSITORY"].split('/')
branch_name = os.environ["GITHUB_REF_NAME"]

# Personastate mapping for Steam Profile Status
personastate_map = {
0: "Offline", 1: "Online", 2: "Busy", 3: "Away",
4: "Snooze", 5: "Looking to trade", 6: "Looking to play"
}


async def get_element_bounding_box(html_file, selector):
"""Get the bounding box of the specified element using Playwright"""
Expand Down Expand Up @@ -114,7 +120,8 @@ def generate_card_for_player_summary(player_data):
personaname = summary_data["personaname"]
personastate = summary_data["personastate"]
avatarfull = summary_data["avatarfull"]
loccountrycode = summary_data["loccountrycode"]
loccountrycode = summary_data.get(
"loccountrycode", "")
lastlogoff = summary_data["lastlogoff"]
timecreated = summary_data["timecreated"]
gameextrainfo = summary_data.get("gameextrainfo", None)
Expand All @@ -123,17 +130,18 @@ def generate_card_for_player_summary(player_data):
lastlogoff_str = format_unix_time(lastlogoff)
timecreated_str = format_unix_time(timecreated)

personastate_map = {
0: "Offline",
1: "Online",
2: "Busy",
3: "Away",
4: "Snooze",
5: "Looking to trade",
6: "Looking to play"
}
personastate_value = personastate_map.get(personastate, "Unknown")

# Create country section only if loccountrycode exists
country_section = ""
if loccountrycode:
country_section = f'''
<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>
'''

html_content = f"""
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -185,10 +193,7 @@ def generate_card_for_player_summary(player_data):
<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>
{country_section}
</div>
<div class="info-right">
<p id="lastlogoff">Last Logoff: {lastlogoff_str}</p>
Expand Down
Loading

0 comments on commit e50ca76

Please sign in to comment.