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

Convert to web-app

parent f0a8b7c8
No related branches found
No related tags found
No related merge requests found
app.py 0 → 100644
from urllib.parse import urlparse, urljoin
import re
from flask import Flask, render_template
from lxml import html
import requests
from scrape import scrape
app = Flask(__name__)
@app.after_request
def add_header(response):
response.cache_control.max_age = 300
return response
@app.route("/")
def index():
tournament_name, tournament_details, events = scrape()
return render_template('index.html',
tournament_name = tournament_name,
tournament_details = tournament_details,
events = events)
if __name__ == "__main__":
app.run()
......@@ -3,52 +3,61 @@ import requests
import re
from urllib.parse import urlparse, urljoin
results_url = "http://www.escrimeresults.com/cobra/index.htm"
results = requests.get(results_url)
results_tree = html.fromstring(results.content)
try:
event_urls = results_tree.xpath(
'//div[@id="schedule"]/table/tr/td/a[text()="View"]/@href')
except IndexError:
print("No event schedule found")
exit()
tournament_name = results_tree.xpath(
'//span[@class="tournName"]/text()')[0]
tournament_details = results_tree.xpath(
'//span[@class="tournDetails"]/text()')[0]
print(tournament_name)
print(tournament_details)
print("{0} Events\n".format(len(event_urls)))
events = {}
for event_url in event_urls:
if not urlparse(event_url).netloc:
event_url = urljoin(results_url, event_url)
event = requests.get(event_url)
event_tree = html.fromstring(event.content)
event_details = event_tree.xpath(
def scrape():
results_url = "http://www.escrimeresults.com/cobra/index.htm"
results = requests.get(results_url)
results_tree = html.fromstring(results.content)
try:
event_urls = results_tree.xpath(
'//div[@id="schedule"]/table/tr/td/a[text()="View"]/@href')
except IndexError:
return "No event schedule found"
tournament_name = results_tree.xpath(
'//span[@class="tournName"]/text()')[0]
tournament_details = results_tree.xpath(
'//span[@class="tournDetails"]/text()')[0]
print("\n\n{0}".format(event_details))
if event_tree.xpath('//a[text()="Final Results"]'):
fencers = event_tree.xpath('//div[@id="finalResults"]/table/tr/td[2]/text()')
print("Event Closed ({0} fencers)".format(len(fencers)))
elif event_tree.xpath('//a[text()="Check-In Status"]'):
checkin_summary = event_tree.xpath(
'normalize-space(//div[@class="checkInSummary"]/text())')
print(checkin_summary)
fencers = event_tree.xpath('//div[@id="checkIn"]/table/tr/td[2]/text()')
events[event_details] = []
prior_events = {}
for fencer in fencers:
events[event_details].append(fencer.strip())
for e in events:
if e == event_details:
continue
if fencer.strip() in events[e]:
if e in prior_events:
prior_events[e] += 1
else:
prior_events[e] = 1
break
for e in prior_events:
print("{0} Fencers previously fenced in {1}".format(prior_events[e], e))
# TODO: Tally fencers who have checked in
events = []
for event_url in event_urls:
if not urlparse(event_url).netloc:
event_url = urljoin(results_url, event_url)
event = requests.get(event_url)
event_tree = html.fromstring(event.content)
event_details = event_tree.xpath(
'//span[@class="tournDetails"]/text()')[0]
if event_tree.xpath('//a[text()="Final Results"]'):
fencers = event_tree.xpath('//div[@id="finalResults"]/table/tr/td[2]/text()')
event_status = "Event Closed ({0} fencers)".format(len(fencers))
elif event_tree.xpath('//a[text()="Seeding"]'):
fencers = event_tree.xpath('//div[@id="Round1Seeding"]/table/tr/td[2]/text()')
event_status = "Event is Ongoing ({0} fencers)".format(len(fencers))
elif event_tree.xpath('//a[text()="Check-In Status"]'):
event_status = event_tree.xpath(
'normalize-space(//div[@class="checkInSummary"]/text())')
fencers = event_tree.xpath('//div[@id="checkIn"]/table/tr/td[2]/text()')
try:
del this_event
except:
pass
this_event = {
'name': event_details,
'status': event_status,
'fencers': [],
'previously_fenced': {},
'previous_total': 0
}
for fencer in fencers:
this_event['fencers'].append(fencer.strip())
for e in events:
if e['name'] == event_details:
continue
if fencer.strip() in e['fencers']:
if e['name'] in this_event['previously_fenced']:
this_event['previously_fenced'][e['name']] += 1
else:
this_event['previously_fenced'][e['name']] = 1
this_event['previous_total'] += 1
break
events.append(this_event)
# TODO: Tally fencers who have checked in
return (tournament_name, tournament_details, events)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Armory Dashboard</title>
<style>
body {
font: 14px sans-serif;
}
section {
border-bottom: 2px solid #aaa;
}
p {
margin: .25em 0;
}
</style>
</head>
<body>
<h1>{{tournament_name}}</h1>
<h2>{{tournament_details}}</h2>
<h2>{{events|length}} Events</h2>
<hr>
{% for e in events %}
<section>
<a name="{{e['name']}}">
<h3><a href="#{{e['name']}}">{{e['name']}}</a></h3>
<p>{{e['status']}}</p>
{% if e['previously_fenced'] %}
<p>{{e['previous_total']}} fencers have previously checked in:</p>
<ul>
{% for pe in e['previously_fenced'] %}
<li>{{pe}} - {{e['previously_fenced'][pe]}} fencers</li>
{% endfor %}
</ul>
{% endif %}
</section>
{% endfor %}
</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