Initial commit
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user