aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2020-10-31 12:57:45 -0400
committerGalen Guyer <galen@galenguyer.com>2020-10-31 12:57:45 -0400
commit084cc91e2a8fa16bb34c608312ee2dc744ed0575 (patch)
tree336ab5d54dc24961d6cdeaf5d9b2a67e282f0571
parent6d59a868a6cfa849a9db4082b2258e71d7e4f482 (diff)
get and return latest data
-rw-r--r--poller/__init__.py76
1 files changed, 65 insertions, 11 deletions
diff --git a/poller/__init__.py b/poller/__init__.py
index 2d52545..fd1a55d 100644
--- a/poller/__init__.py
+++ b/poller/__init__.py
@@ -4,11 +4,16 @@ import os
import threading
import sqlite3
import atexit
+import datetime
from flask import Flask, jsonify
+import requests
+from bs4 import BeautifulSoup
POOL_TIME = 5 * 60 # Seconds
DASHBOARD_URL = 'https://rit.edu/ready/dashboard'
+LATEST_DATA = None
+data_thread = threading.Thread()
db_lock = threading.Lock()
if not os.path.exists('./data'):
@@ -16,7 +21,8 @@ if not os.path.exists('./data'):
def interrupt():
global ping_thread
- ping_thread.cancel()
+ global data_thread
+ data_thread.cancel()
def create_tables():
with db_lock:
@@ -32,8 +38,6 @@ def create_tables():
c.execute(sql)
sql = f'CREATE TABLE IF NOT EXISTS `isolation` (time DATETIME PRIMARY KEY NOT NULL, isolation_on_campus INT NOT NULL, isolation_off_campus INT NOT NULL);'
c.execute(sql)
- sql = f'CREATE TABLE IF NOT EXISTS `isolation` (time DATETIME PRIMARY KEY NOT NULL, isolation_on_campus INT NOT NULL, isolation_off_campus INT NOT NULL);'
- c.execute(sql)
sql = f'CREATE TABLE IF NOT EXISTS `beds` (time DATETIME PRIMARY KEY NOT NULL, beds_available INT NOT NULL);'
c.execute(sql)
sql = f'CREATE TABLE IF NOT EXISTS `tests` (time DATETIME PRIMARY KEY NOT NULL, tests_administered INT NOT NULL);'
@@ -41,13 +45,63 @@ def create_tables():
db_conn.commit()
db_conn.close()
-def ping_sites():
- global ping_thread
- ping_thread = threading.Timer(POOL_TIME, ping_sites, ())
- ping_thread.start()
+def data_is_same(current_data):
+ global LATEST_DATA
+ if LATEST_DATA is None or current_data is None:
+ return False
+ for key in list(LATEST_DATA.keys()):
+ if current_data[key] != LATEST_DATA[key] and key != 'last_updated':
+ return False
+ return True
+
+def get_data():
+ global data_thread
+ data_thread = threading.Timer(POOL_TIME, get_data, ())
+ data_thread.start()
create_tables()
-
-ping_sites()
+ page = requests.get(DASHBOARD_URL, headers={'Cache-Control': 'no-cache'})
+ soup = BeautifulSoup(page.content, 'html.parser')
+ total_students = int(soup.find('div', attrs={'class': 'statistic-12481'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ total_staff = int(soup.find('div', attrs={'class': 'statistic-12484'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ new_students = int(soup.find('div', attrs={'class': 'statistic-12202'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ new_staff = int(soup.find('div', attrs={'class': 'statistic-12205'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ quarantine_on_campus = int(soup.find('div', attrs={'class': 'statistic-12190'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ quarantine_off_campus = int(soup.find('div', attrs={'class': 'statistic-12193'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ isolation_on_campus = int(soup.find('div', attrs={'class': 'statistic-12226'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ isolation_off_campus = int(soup.find('div', attrs={'class': 'statistic-12229'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip())
+ beds_available = int(soup.find('div', attrs={'class': 'statistic-12214'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip().strip('%'))
+ tests_administered = int(soup.find('div', attrs={'class': 'statistic-12829'}).find_all("p", attrs={'class': 'card-header'})[0].text.strip().replace("*", " "))
+ container = soup.find('div', attrs={'id': 'pandemic-message-container'})
+ alert_level = container.find("a").text
+ color = ""
+ if "Green" in alert_level:
+ color = 'green'
+ elif "Yellow" in alert_level:
+ color = 'yellow'
+ elif "Orange" in alert_level:
+ color = 'orange'
+ elif "Red" in alert_level:
+ color = 'red'
+ global LATEST_DATA
+ current_data = {
+ 'alert_level': color,
+ 'total_students': total_students,
+ 'total_staff': total_staff,
+ 'new_students': new_students,
+ 'new_staff': new_staff,
+ 'quarantine_on_campus': quarantine_on_campus,
+ 'quarantine_off_campus': quarantine_off_campus,
+ 'isolation_on_campus': isolation_on_campus,
+ 'isolation_off_campus': isolation_off_campus,
+ 'beds_available': beds_available,
+ 'tests_administered': tests_administered,
+ 'last_updated': datetime.datetime.now()
+ }
+ if not data_is_same(current_data):
+ LATEST_DATA = current_data
+
+
+get_data()
# When you kill Flask (SIGTERM), clear the trigger for the next thread
atexit.register(interrupt)
@@ -62,6 +116,6 @@ else:
APP.secret_key = APP.config['SECRET_KEY']
-@APP.route('/')
+@APP.route('/api/v1/latest')
def _index():
- return jsonify(status=200, response="OK")
+ return jsonify(LATEST_DATA)