Skip to content

Sync Steam Web API

Sync Steam Web API #22

name: Sync Steam Web API
on:
schedule:
# every monday
- cron: "0 9 * * 1"
# watch:
# types: [started]
workflow_dispatch:
repository_dispatch:
types: [sync-steam]
jobs:
douban:
name: Sync Steam Web API
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
# 检查是否安装了 JQ
- name: Check JQ
run: |
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Installing..."
sudo apt-get update
sudo apt-get install -y jq
else
echo "jq is already installed."
fi
# 把当前目录保存到环境变量中
echo "WORK_DIR=$(pwd)" >> $GITHUB_ENV
# 获取本地现有文件中最新的 Count
- name: Get Local Current MD5
run: |
LOCAL_MD5=$(md5sum data/steam/steam_web_api.json | cut -d ' ' -f 1)
echo "LOCAL_MD5=$LOCAL_MD5" >> $GITHUB_ENV
rm data/steam/*.json
# 获取远程最新的 Count
- name: Get Steam Latest OwnedGames
run: |
curl -X 'GET' \
'https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key=${{ secrets.STEAM_WEB_API_KEY }}&steamid=76561197989210276&include_appinfo=true&include_played_free_games=true&format=json' \
-H 'accept: application/json' > data/steam/owned_games.json
- name: Fetch Steam Badges
run: |
curl -X 'GET' \
'https://api.steampowered.com/IPlayerService/GetBadges/v1/?key=${{ secrets.STEAM_WEB_API_KEY }}&steamid=76561197989210276&format=json' \
-H 'accept: application/json' > data/steam/badges.json
- name: Fetch Steam Player Summaries
run: |
curl -X 'GET' \
'https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=${{ secrets.STEAM_WEB_API_KEY }}&steamids=76561197989210276&format=json' \
-H 'accept: application/json' > data/steam/player.json
- name: Merge JSON Files
run: |
cd data/steam
jq -s 'reduce .[] as $item ({}; . * $item)' *.json > unsorted_steam_web_api.json
# 读取JSON文件并提取games数组
games=$(jq '.response.games' unsorted_steam_web_api.json)
# 对games数组进行排序
sorted_games=$(echo "$games" | jq 'sort_by(-.rtime_last_played, - .playtime_forever, .appid)')
# 更新排序后的games数组到原始JSON对象
sorted_json=$(jq '.response.games = $sorted_games' --argjson sorted_games "$sorted_games" unsorted_steam_web_api.json)
# 输出排序后的JSON
echo "$sorted_json" > steam_web_api.json
rm unsorted_steam_web_api.json
cd ../..
CURRENT_MD5=$(md5sum data/steam/steam_web_api.json | cut -d ' ' -f 1)
echo "CURRENT_MD5=$CURRENT_MD5" >> $GITHUB_ENV
- name: Download Steam Cover
run: |
# 检查 steam 目录是否存在,如果不存在则创建
if [ ! -d "steam" ]; then
mkdir steam
fi
# 读取本地的 steam_web_api.json 文件内容
json=$(cat data/steam/steam_web_api.json)
# 提取图片 URL
appIds=$(echo "$json" | jq -r '.response.games[].appid')
# 遍历 appid
for appId in $appIds; do
# 提取图片 URL
header_img="https://cdn.cloudflare.steamstatic.com/steam/apps/$appId/header.jpg"
# library_img="https://cdn.cloudflare.steamstatic.com/steam/apps/$appId/library_600x900.jpg"
# 遍历 Header 图片 URL 并下载图片
filename="$appId.jpg"
filepath="assets/images/steam/header/$filename"
# 检查文件是否已存在
if [ -f "$filepath" ]; then
echo "Skipping $filename - File already exists"
else
# 使用 curl 命令下载图片
curl -o "$filepath" "$header_img"
echo "Downloaded $filename"
fi
# 遍历 Library 图片 URL 并下载图片
# filename="$appId.jpg"
# filepath="assets/images/steam/library/$filename"
# # 检查文件是否已存在
# if [ -f "$filepath" ]; then
# echo "Skipping $filename - File already exists"
# else
# # 使用 curl 命令下载图片
# curl -o "$filepath" "$library_img"
# echo "Downloaded $filename"
# fi
done
# 对比 MD5
- name: MD5 Compare
run: |
if [ ${{ env.CURRENT_MD5 }} != ${{ env.LOCAL_MD5 }} ]; then
echo "Variables are not equal. Running the next steps."
else
echo "Variables are equal. Skipping the next steps."
exit 0
fi
# 把修改后的数据提交到 GitHub 仓库
- name: Git Add and Commit
if: ${{ env.CURRENT_MD5 != env.LOCAL_MD5 }}
uses: EndBug/add-and-commit@v9
with:
message: 'chore(data): update steam data'
committer_name: 'github-actions[bot]'
committer_email: 'github-actions[bot]@users.noreply.github.com'
add: |
'./data/steam'
# # 调用另外的 GitHub Actions 构建 Hugo
- name: Build Hugo and Deploy
if: ${{ env.CURRENT_MD5 != env.LOCAL_MD5 }}
uses: peter-evans/repository-dispatch@v2
with:
event-type: build-hugo