-
Notifications
You must be signed in to change notification settings - Fork 0
/
KVUE_allergy_report.py
executable file
·68 lines (56 loc) · 1.98 KB
/
KVUE_allergy_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
from datetime import datetime, timedelta
from re import split
from unicodedata import normalize
from bs4 import BeautifulSoup
from rich import box
from rich.console import Console
from rich.table import Table
from selenium import webdriver
# iframe from https://www.kvue.com/allergy
base_url = "https://www.keepandshare.com/calendar/show_month.php?i=1940971&vw=day&date=" # noqa:
def screenscrap(url):
logLocation = "/dev/null"
# For debugging
# logLocation = "./geckodriver.log"
opts = webdriver.FirefoxOptions()
opts.headless = True
browser = webdriver.Firefox(options=opts, service_log_path=logLocation)
browser.get(url)
soup = BeautifulSoup(browser.page_source, "html.parser")
browser.close()
return soup
date = datetime.now()
# starting with today, check if there is a report, if not find the most recent
count = 1
while True:
if count == 5:
print("ERROR, can't connect or get a report after 5 tries")
exit(1)
html = screenscrap(base_url + date.strftime("%Y-%m-%d"))
allergyReport = html.find("div", {"class": "calendar_one_line_text"})
if not allergyReport:
date = date - timedelta(days=1)
count += 1
else:
break
# clean up results
# sometime the report ends with a ".", lets remove it
allergies = allergyReport.text.strip(".")
# fix encoding like \xa0 https://stackoverflow.com/a/34669482
allergies = normalize("NFKD", allergies)
# spilt for each allergen and remove leading spaces
allergies = [x.lstrip() for x in split(", |; ", allergies)]
table = Table(
border_style="dim green",
box=box.SIMPLE_HEAD,
header_style="white",
title="KVUE allergy report from {}".format(date.strftime("%B %d, %Y")),
)
table.add_column("allergen", style="dim")
table.add_column("severity", style="dim")
for item in sorted(allergies):
# change formatting from 'Trees 13 gr/m3 Low' to ['Trees', '13 gr/m3 Low']
i = item.split(" ", 1)
table.add_row(i[0], i[1])
Console().print(table)