Compare commits
7 Commits
901c4812fe
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4723076147 | ||
|
|
5ef7a035dd | ||
|
|
ee794515de | ||
|
|
5c30b91cb9 | ||
|
|
f5b58cfe58 | ||
|
|
5e0a74f8ce | ||
|
|
17ccd0bab7 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
config.yaml
|
||||||
20
server.py
20
server.py
@@ -2,6 +2,7 @@ from flask import Flask, request
|
|||||||
|
|
||||||
from website.helpers import safely_get_people
|
from website.helpers import safely_get_people
|
||||||
from website.spreadsheet import get_games_for_people
|
from website.spreadsheet import get_games_for_people
|
||||||
|
from website.download import refresh_all_user_games, refresh_max_players
|
||||||
from website.config import config
|
from website.config import config
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -43,12 +44,22 @@ def password_form():
|
|||||||
# The HTML form asking for password
|
# The HTML form asking for password
|
||||||
return '''<form method="post" id="form">
|
return '''<form method="post" id="form">
|
||||||
<label for="names" form="form">Which users do you want?
|
<label for="names" form="form">Which users do you want?
|
||||||
(Hint: lowercase, comma-separated)
|
(comma-separated)
|
||||||
</label>
|
</label>
|
||||||
<br>
|
<br>
|
||||||
<input type="text" id="names" name="names" required>
|
<input type="text" id="names" name="names" required>
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<br>
|
||||||
|
<label for="reload_users" form="form">Force reload user info?</label>
|
||||||
|
<br>
|
||||||
|
<input type="checkbox" id="reload_users" name="reload_users">
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
<label for="reload_max_players" form="form">Force reload max-player info?</label>
|
||||||
|
<br>
|
||||||
|
<input type="checkbox" id="reload_max_players" name="reload_max_players">
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
<label for="password" form="form">Are you one of the cool kids?</label>
|
<label for="password" form="form">Are you one of the cool kids?</label>
|
||||||
<br>
|
<br>
|
||||||
<input type="password" id="password" name="password" required>
|
<input type="password" id="password" name="password" required>
|
||||||
@@ -65,7 +76,14 @@ def check_password(password):
|
|||||||
def serve_things():
|
def serve_things():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
if check_password(request.form['password']):
|
if check_password(request.form['password']):
|
||||||
|
if request.form.get('reload_users', 0):
|
||||||
|
refresh_all_user_games()
|
||||||
|
if request.form.get('reload_max_players', 0):
|
||||||
|
refresh_max_players()
|
||||||
|
if request.form['names']:
|
||||||
return games_spreadsheet(request.form['names'])
|
return games_spreadsheet(request.form['names'])
|
||||||
|
else:
|
||||||
|
return 'Sup'
|
||||||
else:
|
else:
|
||||||
return wrong_password()
|
return wrong_password()
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import subprocess
|
|
||||||
|
|
||||||
from website import helpers
|
from website import helpers
|
||||||
|
|
||||||
@@ -12,14 +11,13 @@ logger = logging.getLogger(__name__)
|
|||||||
def _download_max_players() -> None:
|
def _download_max_players() -> None:
|
||||||
# be sure to use single quotes within the command to prevent shell escaping
|
# be sure to use single quotes within the command to prevent shell escaping
|
||||||
cmd = (
|
cmd = (
|
||||||
f"curl -s"
|
f"/usr/bin/curl -s"
|
||||||
f" -u 'anonymous:{config.share['password']}'"
|
f" -u 'anonymous:{config.share['password']}'"
|
||||||
f" -o '{config.max_player_spreadsheet_path}'"
|
f" -o '{config.max_player_spreadsheet_path}'"
|
||||||
f" '{config.share['uri']}'"
|
f" '{config.share['uri']}'"
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO error check
|
helpers.run_and_log_cmd(cmd)
|
||||||
subprocess.run(cmd, shell=True)
|
|
||||||
|
|
||||||
|
|
||||||
def _download_games(user: str) -> None:
|
def _download_games(user: str) -> None:
|
||||||
@@ -30,25 +28,33 @@ def _download_games(user: str) -> None:
|
|||||||
|
|
||||||
# be sure to use single quotes within the command to prevent shell escaping
|
# be sure to use single quotes within the command to prevent shell escaping
|
||||||
cmd = (
|
cmd = (
|
||||||
f"curl {CURL_OPTIONS} '{STEAM_API_URI}"
|
f"/usr/bin/curl {CURL_OPTIONS} '{STEAM_API_URI}"
|
||||||
f"?key={config.key}"
|
f"?key={config.key}"
|
||||||
f"{STEAM_OPTIONS}"
|
f"{STEAM_OPTIONS}"
|
||||||
f"&steamid={config.users[user]}'"
|
f"&steamid={config.users[user]}'"
|
||||||
)
|
)
|
||||||
# TODO error check
|
|
||||||
subprocess.run(cmd, shell=True)
|
helpers.run_and_log_cmd(cmd)
|
||||||
|
|
||||||
|
|
||||||
def _refresh_games_if_needed(users: list(str), force=False) -> None:
|
def _refresh_games_if_needed(users: list(str), force=False) -> None:
|
||||||
for u in users:
|
for u in users:
|
||||||
path = helpers.get_user_fname(u)
|
path = helpers.get_user_fname(u)
|
||||||
if force or helpers.get_file_age(path) > config.user_games_refresh_delay_seconds:
|
age = helpers.get_file_age(path)
|
||||||
|
if force or not age or age > config.user_games_refresh_delay_seconds:
|
||||||
logger.info(f'Downloading {path}')
|
logger.info(f'Downloading {path}')
|
||||||
_download_games(u)
|
_download_games(u)
|
||||||
else:
|
else:
|
||||||
logger.info(f'Using cached file {path}')
|
logger.info(f'Using cached file {path}')
|
||||||
|
|
||||||
|
|
||||||
|
def refresh_all_user_games():
|
||||||
|
_refresh_games_if_needed(list(config.users.keys()), force=True)
|
||||||
|
|
||||||
|
|
||||||
|
def refresh_max_players():
|
||||||
|
_download_max_players()
|
||||||
|
|
||||||
|
|
||||||
def get_games_from_steam(users: list(str), force=False) -> pd.DataFrame:
|
def get_games_from_steam(users: list(str), force=False) -> pd.DataFrame:
|
||||||
|
|
||||||
@@ -72,7 +78,8 @@ def get_games_from_steam(users: list(str), force=False) -> pd.DataFrame:
|
|||||||
def get_max_players_data(force=False) -> pd.DataFrame:
|
def get_max_players_data(force=False) -> pd.DataFrame:
|
||||||
# freshen up max players list if needed
|
# freshen up max players list if needed
|
||||||
path = config.max_player_spreadsheet_path
|
path = config.max_player_spreadsheet_path
|
||||||
if force or helpers.get_file_age(path) > config.max_players_refresh_delay_seconds:
|
age = helpers.get_file_age(path)
|
||||||
|
if force or not age or age > config.max_players_refresh_delay_seconds:
|
||||||
logger.info(f'Downloading {path}')
|
logger.info(f'Downloading {path}')
|
||||||
_download_max_players()
|
_download_max_players()
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from markupsafe import escape
|
from markupsafe import escape
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from website.config import config
|
from website.config import config
|
||||||
@@ -14,7 +15,11 @@ def get_user_fname(user: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def get_file_age(fname: Path) -> int:
|
def get_file_age(fname: Path) -> int:
|
||||||
return time.time() - os.path.getmtime(fname)
|
try:
|
||||||
|
age = time.time() - os.path.getmtime(fname)
|
||||||
|
except FileNotFoundError:
|
||||||
|
age = None
|
||||||
|
return age
|
||||||
|
|
||||||
|
|
||||||
def check_max_players_list() -> None:
|
def check_max_players_list() -> None:
|
||||||
@@ -37,7 +42,7 @@ def check_max_players_list() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def safely_get_people(unsafe_people: str) -> list(str):
|
def safely_get_people(unsafe_people: str) -> list(str):
|
||||||
safe_users = [escape(u) for u in unsafe_people.split(',')]
|
safe_users = [escape(u.strip().lower()) for u in unsafe_people.split(',')]
|
||||||
|
|
||||||
for u in safe_users:
|
for u in safe_users:
|
||||||
if u not in config.users.keys():
|
if u not in config.users.keys():
|
||||||
@@ -45,3 +50,12 @@ def safely_get_people(unsafe_people: str) -> list(str):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
return safe_users
|
return safe_users
|
||||||
|
|
||||||
|
|
||||||
|
def run_and_log_cmd(cmd):
|
||||||
|
proc = subprocess.run(cmd,
|
||||||
|
shell=True,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT)
|
||||||
|
if proc.returncode != 0:
|
||||||
|
logger.error(proc.stdout)
|
||||||
|
|||||||
Reference in New Issue
Block a user