Skip to content
Snippets Groups Projects
Commit ae2a0f05 authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Make the Live Results URL user-configurable.

parent 60151f54
No related branches found
No related tags found
No related merge requests found
from urllib.parse import urlparse, urljoin from urllib.parse import urlparse, urljoin
import re import re
from flask import Flask, render_template from flask import Flask, render_template, redirect, request, url_for
from flask_caching import Cache from flask_caching import Cache
from lxml import html from lxml import html
import requests import requests
...@@ -9,16 +9,28 @@ from scrape import scrape ...@@ -9,16 +9,28 @@ from scrape import scrape
app = Flask(__name__) app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'}) cache = Cache(app, config={'CACHE_TYPE': 'simple'})
def make_cache_key(*args, **kwargs):
path = request.path
args = str(hash(frozenset(request.args.items())))
return (path + args).encode('utf-8')
@app.after_request @app.after_request
def add_header(response): def add_header(response):
response.cache_control.max_age = 300 response.cache_control.max_age = 300
return response return response
@app.route("/") @app.route("/")
@cache.cached(timeout=300)
def index(): def index():
tournament_name, tournament_details, events = scrape() return render_template('index.html')
return render_template('index.html',
@app.route("/live")
@cache.cached(timeout=300, key_prefix=make_cache_key)
def live(results_url=None):
results_url = request.args.get('results_url')
if not results_url:
return redirect(url_for('index'))
tournament_name, tournament_details, events = scrape(results_url)
return render_template('live.html',
tournament_name = tournament_name, tournament_name = tournament_name,
tournament_details = tournament_details, tournament_details = tournament_details,
events = events) events = events)
......
...@@ -5,8 +5,7 @@ from urllib.parse import urlparse, urljoin ...@@ -5,8 +5,7 @@ from urllib.parse import urlparse, urljoin
from itertools import repeat from itertools import repeat
def scrape(): def scrape(results_url):
results_url = "https://fencingresults.ant.sr/cobra/"
results = requests.get(results_url) results = requests.get(results_url)
results_tree = html.fromstring(results.content) results_tree = html.fromstring(results.content)
try: try:
......
...@@ -4,7 +4,12 @@ body { ...@@ -4,7 +4,12 @@ body {
background-color: #000; background-color: #000;
max-width: 100%; max-width: 100%;
} }
main { body.page-live header span.back-to-home {
float: right;
font-size: .8em;
opacity: .6;
}
body.page-live main {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
align-items: flex-start; align-items: flex-start;
...@@ -12,6 +17,27 @@ main { ...@@ -12,6 +17,27 @@ main {
max-width: 100%; max-width: 100%;
box-sizing: border-box; box-sizing: border-box;
} }
body.page-live p, ul {
margin: .25em 0;
}
body.page-home form {
display: flex;
flex-wrap: wrap;
}
body.page-home form > * {
flex: auto 1 0;
}
body.page-home label {
display: flex;
flex-wrap: wrap;
}
body.page-home input,
body.page-home select {
flex: 15em 1 0;
}
body.page-home input[type=submit] {
flex: 6em 0 0;
}
h1 { h1 {
font-size: 1.2em; font-size: 1.2em;
} }
...@@ -41,9 +67,6 @@ section > :last-child { ...@@ -41,9 +67,6 @@ section > :last-child {
h1, h2, h3, h4, h5, h6 { h1, h2, h3, h4, h5, h6 {
margin: .35em 0; margin: .35em 0;
} }
p, ul {
margin: .25em 0;
}
a:link, a:link,
a:active, a:active,
a:visited { a:visited {
......
...@@ -4,38 +4,37 @@ ...@@ -4,38 +4,37 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>Armory Dashboard</title> <title>Armory Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="refresh" content="60; URL={{ url_for('index') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head> </head>
<body> <body class="page-home">
<header> <header>
<h1>{{ tournament_name }} - {{ events | length }} events</h1> <h1>Armory Dashboard</h1>
</header> </header>
<main> <main>
{% for e in events %} <p>
<section class="status-{{ e['status'] }}"> Welcome! Please select a Live Results URL, or enter your own.
<header> </p>
<div class="status">{{ e['status'] }}</div> <p>
<div class="name"> <form action="{{ url_for('live') }}" method="get">
<a name="{{ e['name'] }}" href="#{{ e['name'] }}">{{ e['name'] }}</a> <label>Live Results URL: &nbsp;
</div> <select name="results_url">
<div class="time">{{ e['time'] }}</div> <option value="" selected disabled>-- Select One --</option>
</header> <option value="http://escrimeresults.com/cobra/">Cobra</option>
<h4></h4> <option value="http://escrimeresults.com/thrust/">Thrust</option>
<p><span class="number">{{ e['fencers_checked_in'] | length }}</span> of <span class="number">{{ e['fencers'] | length }}</span> fencer(s) checked in.</p> <option value="http://njfencingresults.org/liveresults/">NJFencingResults.org</option>
{% if e['previously_fenced'] %} </select>
<p><span class="number">{{ e['previous_total'] }}</span> fenced in prior events:</p> </label>
<ul> <input type="submit" value="Go!">
{% for pe in e['previously_fenced'] %} </form>
<li>{{ pe }} - {{ e['previously_fenced'][pe] }}</li> </p>
{% endfor %} <p>
</ul> <form action="{{ url_for('live') }}" method="get">
{% endif %} <label>Other URL: &nbsp;
{% if e['new_fencers_not_checked_in'] %} <input name="results_url" placeholder="http://example.com/liveresults">
<p class="hl">{{ e['new_fencers_not_checked_in'] | length }} new and not checked in</p> </label>
{% endif %} <input type="submit" value="Go!">
</section> </form>
{% endfor %} </p>
</main> </main>
</body> </body>
</html> </html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Armory Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="refresh" content="60; URL={{ url_for('index') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body class="page-live">
<header>
<span class="back-to-home"><a href="{{ url_for('index') }}">Back to Home</a></span>
<h1>{{ tournament_name }} - {{ events | length }} events</h1>
</header>
<main>
{% for e in events %}
<section class="status-{{ e['status'] }}">
<header>
<div class="status">{{ e['status'] }}</div>
<div class="name">
<a name="{{ e['name'] }}" href="#{{ e['name'] }}">{{ e['name'] }}</a>
</div>
<div class="time">{{ e['time'] }}</div>
</header>
<h4></h4>
<p><span class="number">{{ e['fencers_checked_in'] | length }}</span> of <span class="number">{{ e['fencers'] | length }}</span> fencer(s) checked in.</p>
{% if e['previously_fenced'] %}
<p><span class="number">{{ e['previous_total'] }}</span> fenced in prior events:</p>
<ul>
{% for pe in e['previously_fenced'] %}
<li>{{ pe }} - {{ e['previously_fenced'][pe] }}</li>
{% endfor %}
</ul>
{% endif %}
{% if e['new_fencers_not_checked_in'] %}
<p class="hl">{{ e['new_fencers_not_checked_in'] | length }} new and not checked in</p>
{% endif %}
</section>
{% endfor %}
</main>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment