Initial commit

This commit is contained in:
Nate Bowman
2026-10-07 17:28:39 -04:00
commit 9d5cc75426
8 changed files with 248 additions and 0 deletions

25
website/config.py Normal file
View File

@@ -0,0 +1,25 @@
from pathlib import Path
from types import SimpleNamespace
import yaml
config_file = '/home/nate/projects/games-spreadsheet/config.yaml'
with open(config_file, 'r') as f:
c = yaml.safe_load(f)
root_path = Path(c['storage']['root'])
copied_keys = [
'users',
'key',
'share',
'user_games_refresh_delay_seconds',
'max_players_refresh_delay_seconds'
]
config = SimpleNamespace({k: c[k] for k in copied_keys})
config.max_player_spreadsheet_path = (root_path /
c['storage']['resources'] /
'max-players.ods')
config.resources = root_path / c['storage']['resources']

82
website/download.py Normal file
View File

@@ -0,0 +1,82 @@
import json
import logging
import pandas as pd
import subprocess
from website import helpers
from website.config import config
logger = logging.getLogger(__name__)
def _download_max_players() -> None:
# be sure to use single quotes within the command to prevent shell escaping
cmd = (
f"curl -s"
f" -u 'anonymous:{config.share['password']}'"
f" -o '{config.max_player_spreadsheet_path}'"
f" '{config.share['uri']}'"
)
# TODO error check
subprocess.run(cmd, shell=True)
def _download_games(user: str) -> None:
# TODO check for injection attack
STEAM_API_URI = 'https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/'
STEAM_OPTIONS = '&include_played_free_games=1&include_appinfo=1&format=json'
CURL_OPTIONS = f'-s -o {helpers.get_user_fname(user)}'
# be sure to use single quotes within the command to prevent shell escaping
cmd = (
f"curl {CURL_OPTIONS} '{STEAM_API_URI}"
f"?key={config.key}"
f"{STEAM_OPTIONS}"
f"&steamid={config.users[user]}'"
)
# TODO error check
subprocess.run(cmd, shell=True)
def _refresh_games_if_needed(users: list(str), force=False) -> None:
for u in users:
path = helpers.get_user_fname(u)
if force or helpers.get_file_age(path) > config.user_games_refresh_delay_seconds:
logger.info(f'Downloading {path}')
_download_games(u)
else:
logger.info(f'Using cached file {path}')
def get_games_from_steam(users: list(str), force=False) -> pd.DataFrame:
_refresh_games_if_needed(users, force)
# Generate game info dataframe from cached data obtained via Steam API
games = {}
df = pd.DataFrame()
for u in users:
with open(helpers.get_user_fname(u), 'r') as f:
d = json.load(f)
small_games = sorted([g['name'] for g in d['response']['games']])
games[u] = small_games
small_df = pd.DataFrame(columns=[u], index=games[u], data=1)
df = df.join(small_df, how='outer')
df = df.fillna(0)
return df
def get_max_players_data(force=False) -> pd.DataFrame:
# freshen up max players list if needed
path = config.max_player_spreadsheet_path
if force or helpers.get_file_age(path) > config.max_players_refresh_delay_seconds:
logger.info(f'Downloading {path}')
_download_max_players()
else:
logger.info(f'Using cached file {path}')
df = pd.read_excel(path, engine='odf', index_col=0)
return df

47
website/helpers.py Normal file
View File

@@ -0,0 +1,47 @@
import logging
import os
from markupsafe import escape
from pathlib import Path
import time
from website.config import config
logger = logging.getLogger(__name__)
def get_user_fname(user: str) -> str:
return config.resources / f'{user}.json'
def get_file_age(fname: Path) -> int:
return time.time() - os.path.getmtime(fname)
def check_max_players_list() -> None:
people = list(config.users.keys())
dfg = get_games_from_steam(people)
dfp = get_max_players_data()
oops = dfp.index[~dfp.index.isin(dfg.index)]
oops = '\n'.join(sorted(oops.to_list()))
logger.info(
'The following titles are in the max-players spreadsheet but'
' are not in any user lists:\n'
f'{oops}\n'
'This may mean that they are misspelled in the spreadsheet.'
'It is also possible that they are not Steam games or that'
' they are Steam games that nobody owns.'
)
def safely_get_people(unsafe_people: str) -> list(str):
safe_users = [escape(u) for u in unsafe_people.split(',')]
for u in safe_users:
if u not in config.users.keys():
logger.warn(f'Requested user {u} is not in the configuration file')
return None
return safe_users

21
website/spreadsheet.py Normal file
View File

@@ -0,0 +1,21 @@
import pandas as pd
from website.download import get_games_from_steam, get_max_players_data
def get_games_for_people(people: list) -> (list, list):
dfg = get_games_from_steam(people)
dfp = get_max_players_data()
all_have = dfg.index[(dfg[people] > 0).all(axis=1)]
enough_slots = dfp.index[dfp['Players'] >= len(people)]
candidates = all_have[all_have.isin(enough_slots)]
candidates = sorted(candidates.to_list())
unknown = all_have[~all_have.isin(dfp.index)]
unknown = sorted(unknown.to_list())
return candidates, unknown