Skip to content

alifeee/steam_collage_api

Repository files navigation

Steam Collage API

An API to generate a collage of games from a Steam profile. Python is used to generate the image, which is served via an HTTP server using Flask. Hosted using Kamatera.

Give it a go! --> https://alifeee.co.uk/steam_mosaic/

Blog post --> https://blog.alifeee.co.uk/steam-collage-api/

Collage image

URLs

Service URL
Docker Hub alifeee/steamcollageapi
GitHub alifeee/steam_collage
Kamatera Console alifeee
Kamatera Server http://server.alifeee.co.uk

Environment

A .env file is used to store environment variables. The following variables are used:

Name Description
API_KEY Steam API key - see below.
DO_CACHE Whether to cache the game thumbnails. Note that this increases the memory usage of the server.
# .env
API_KEY=8917981789178
DO_CACHE=True

API Key

A steam API key must be placed in the .env file in the root directory, or exposed to the environment via the terminal. Keys can be obtained from Steam.

Commands

Use python virtual environment

python -m venv env

Install modules from requirements.txt

pip install -r requirements.txt

Save modules to requirements.txt

pip freeze > requirements.txt

Run tests

ptw -- --cov=api

Run server locally

python ./api/api.py debug

Build and run docker image

docker build -t alifeee/steamcollageapi .
docker run -d --restart unless-stopped -p 5000:5000 -e API_KEY=8917981789178 alifeee/steamcollageapi

Push docker image to Docker Hub

docker push alifeee/steamcollageapi

Thunder Client

Thunder client is a VS Code extension which allows you to make HTTP requests. It is useful for testing the API.

Testing

Some tests are included in the tests collection. These can be run by using the Run All button in the top right of collection.

Deploying to Kamatera (remote)

Connect to Kamatera

ssh root@server.alifeee.co.uk
> enter password

Pull the latest image from Docker Hub

docker pull alifeee/steamcollageapi

Remove existing container

docker ps -a
> get container id
docker rm <container id>

Run image

docker create -d --restart unless-stopped -p 5000:5000 -e API_KEY=8917981789178 --name steamcollageapi alifeee/steamcollageapi
> get container id
docker start <container id>

GitHub actions

python-app.yml: Python testing

This workflow:

  1. Checks out the repository
  2. Sets up Python 3.10
  3. Installs dependencies
  4. Runs linting
  5. Runs tests

Secrets

None.

docker-image.yml: Docker

This workflow:

  1. Checks out the repository
  2. Builds the Docker image
  3. Pushes the Docker image to Docker Hub
  4. Deploys the Docker image to Kamatera

Secrets used for Docker and Kamatera

Name Description
API_KEY Steam API key - see above.
DOCKERHUB_USERNAME Docker Hub username.
DOCKERHUB_TOKEN Docker Hub access token.
HOST IP address of the Kamatera server
USERNAME Username for the Kamatera server
KEY RSA key* for the Kamatera server
*RSA key

See rsa_ssh_key_setup.md for details on setting up the RSA key.

API

All APIs are served on the port 5000.

GET /steamcollage/games

Returns a list of games for a given steam ID.

Query Parameters {1}

Name Type Description
id string Steam ID or vanity URL of the user, e.g., alifeee
cols int Number of columns in the collage
rows int Number of rows in the collage
sort name, recent, or playtime Sort order of the games
wget http://localhost:5000/steamcollage/games?id=alifeee&cols=5&rows=5&sort=recent

Example image which would be generated by the above API

GET /steamcollage/verifyuser

Returns a boolean indicating whether a given steam account exists, and whether it is private.

Query Parameters {#2}

Name Type Description
id string Steam ID or vanity URL of the user, e.g., alifeee
wget http://localhost:5000/steamcollage/verifyuser?id=alifeee
{
  "exists": true,
  "private": false
}

GET /steamcollage/alive

Returns "Alive" if the server is running.

wget http://localhost:5000/steamcollage/alive
"Alive"

GET /steamcollage/alive_img

Returns an image if the server is running.

wget http://localhost:5000/steamcollage/alive_img

Lovely picture of a sheep

Alive check

To check the API is alive, I use Testfully to poll the /steamcollage/alive endpoint every hour.