From d3da3019773548e86c233190e1fb1ecbb98938b9 Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Sun, 27 Nov 2016 11:08:16 -0500
Subject: [PATCH] Count checked-in fencers

---
 scrape.py            | 26 +++++++++++++++++++++-----
 templates/index.html |  9 ++++++++-
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/scrape.py b/scrape.py
index aee9774..bcad8d8 100644
--- a/scrape.py
+++ b/scrape.py
@@ -2,6 +2,7 @@ from lxml import html
 import requests
 import re
 from urllib.parse import urlparse, urljoin
+from itertools import repeat
 
 
 def scrape():
@@ -29,38 +30,53 @@ def scrape():
         event_time = event_details[1]
         if event_tree.xpath('//a[text()="Final Results"]'):
             fencers = event_tree.xpath('//div[@id="finalResults"]/table/tr/td[2]/text()')
+            fencers = dict(zip(fencers, repeat("Checked In")))
             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()')
+            fencers = dict(zip(fencers, repeat("Checked In")))
             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_checked_in = event_tree.xpath('//div[@id="checkIn"]/table/tr/td[1]/text()')
             fencers = event_tree.xpath('//div[@id="checkIn"]/table/tr/td[2]/text()')
+            fencers = dict(zip(fencers, fencers_checked_in))
         try:
             del this_event
         except:
-            pass
+            pass # not yet set, oh well
         this_event = {
             'name': event_name,
             'time': event_time,
             'status': event_status,
             'fencers': [],
+            'fencers_checked_in': [],
+            'new_fencers_not_checked_in': [],
             'previously_fenced': {},
             'previous_total': 0
         }
-        for fencer in fencers:
-            this_event['fencers'].append(fencer.strip())
+        for fencer, is_checked_in in fencers.items():
+            is_checked_in = bool(is_checked_in.strip())
+            fencer = fencer.strip()
+            this_event['fencers'].append(fencer)
+            if is_checked_in:
+                this_event['fencers_checked_in'].append(fencer)
+            else:
+                this_event['new_fencers_not_checked_in'].append(fencer)
             for e in events:
                 if e['name'] == event_details:
                     continue
-                if fencer.strip() in e['fencers']:
+                if fencer 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
+                    try:
+                        this_event['new_fencers_not_checked_in'].remove(fencer)
+                    except ValueError:
+                        pass # already removed; ignore
                     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
index e34ef73..4a6c6be 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -6,10 +6,11 @@
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <style>
         body {
-            font: 14px sans-serif;
+            font: 18px sans-serif;
         }
         section {
             border-bottom: 1px dashed #aaa;
+            margin: 1em 0;
         }
         h1, h2, h3, h4, h5, h6 {
             margin: .35em 0;
@@ -27,6 +28,9 @@
             text-decoration: underline;
             color: #000;
         }
+        .hl {
+            background-color: #ee8;
+        }
     </style>
 </head>
 <body>
@@ -48,6 +52,9 @@
     {% endfor %}
     </ul>
     {% endif %}
+    {% if e['new_fencers_not_checked_in'] %}
+    <p class="hl">{{e['new_fencers_not_checked_in']|length}} new fencers not yet checked in</p>
+    {% endif %}
     </section>
     {% endfor %}
 </body>
-- 
GitLab