From 3ab5fab7a9e13df935775bdb0b1396b8c31be26b Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Sun, 27 Nov 2016 09:19:59 -0500
Subject: [PATCH] Convert to web-app

---
 app.py               |  24 ++++++++++
 scrape.py            | 105 +++++++++++++++++++++++--------------------
 templates/index.html |  39 ++++++++++++++++
 3 files changed, 120 insertions(+), 48 deletions(-)
 create mode 100644 app.py
 create mode 100644 templates/index.html

diff --git a/app.py b/app.py
new file mode 100644
index 0000000..71d1863
--- /dev/null
+++ b/app.py
@@ -0,0 +1,24 @@
+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()
diff --git a/scrape.py b/scrape.py
index 3df9c17..f9c4f59 100644
--- a/scrape.py
+++ b/scrape.py
@@ -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)
diff --git a/templates/index.html b/templates/index.html
new file mode 100644
index 0000000..e5574e5
--- /dev/null
+++ b/templates/index.html
@@ -0,0 +1,39 @@
+<!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>
-- 
GitLab