Initial commit
This commit is contained in:
8
README.md
Normal file
8
README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
Final setup:
|
||||
* draw list of owned games directly from Steam
|
||||
* re-download every day or so
|
||||
* hand-maintain list of max players for games
|
||||
|
||||
```
|
||||
flask --app server run
|
||||
```
|
||||
11
config.yaml
Normal file
11
config.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
users:
|
||||
nate: 123
|
||||
key: 456
|
||||
share:
|
||||
uri: https://nextcloud.site.tld/public.php/dav/files/abc
|
||||
password: secret
|
||||
storage:
|
||||
root: /home/nate/projects/games-spreadsheet
|
||||
resources: resources
|
||||
user_games_refresh_delay_seconds: 86400
|
||||
max_players_refresh_delay_seconds: 86400
|
||||
1
resources/.gitignore
vendored
Normal file
1
resources/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*
|
||||
53
server.py
Normal file
53
server.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from flask import Flask, request
|
||||
|
||||
from website.helpers import safely_get_people
|
||||
from website.spreadsheet import get_games_for_people
|
||||
from website.config import config
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
|
||||
def games_spreadsheet(unsafe_users):
|
||||
safe_users = safely_get_people(unsafe_users)
|
||||
if safe_users:
|
||||
return valid_users_requested(safe_users)
|
||||
else:
|
||||
return unknown_user_requested()
|
||||
|
||||
|
||||
@app.route('/games-spreadsheet')
|
||||
def games_spreadsheet():
|
||||
# parse url for people
|
||||
unsafe_users = request.args.get('users', None)
|
||||
if unsafe_users:
|
||||
safe_users = safely_get_people(unsafe_users)
|
||||
if safe_users:
|
||||
return valid_users_requested(safe_users)
|
||||
else:
|
||||
return unknown_users_requested()
|
||||
else:
|
||||
return standard_home()
|
||||
|
||||
|
||||
def standard_home():
|
||||
return "Hello!"
|
||||
|
||||
|
||||
def unknown_users_requested():
|
||||
return 'Unknown user in request'
|
||||
|
||||
|
||||
def valid_users_requested(people):
|
||||
candidates, unknown = get_games_for_people(people)
|
||||
userinfo = f'You requested the following users:\n{"\n".join(people)}'
|
||||
valid = (
|
||||
f'The following games are owned by all requested users and can'
|
||||
f' support {len(people)} players:\n{'\n'.join(candidates)}'
|
||||
)
|
||||
maybe = (
|
||||
f'The following games are owned by all users in the request,'
|
||||
f' but no max-player information is available for them:\n'
|
||||
f'{"\n".join(unknown)}'
|
||||
)
|
||||
return (userinfo + '\n\n' + valid + '\n\n' + maybe).replace('\n', '<br>')
|
||||
25
website/config.py
Normal file
25
website/config.py
Normal 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
82
website/download.py
Normal 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
47
website/helpers.py
Normal 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
21
website/spreadsheet.py
Normal 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
|
||||
Reference in New Issue
Block a user