Merge pull request #1819 from dherrada/vaxx-fix

Came up with better way to get values
This commit is contained in:
Jeff Epler 2021-09-02 15:26:52 -04:00 committed by GitHub
commit d2174e58fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@ from adafruit_magtag.magtag import MagTag
from adafruit_progressbar.progressbar import ProgressBar
# Set up where we'll be fetching data from
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv"
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv" # pylint: disable=line-too-long
# Find data for other countries/states here:
# https://github.com/owid/covid-19-data/tree/master/public/data/vaccinations
@ -11,28 +11,40 @@ magtag.network.connect()
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=((magtag.graphics.display.width // 2) - 1, 8,),
text_position=(
(magtag.graphics.display.width // 2) - 1,
8,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Title
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=((magtag.graphics.display.width // 2) - 1, 23,),
text_position=(
(magtag.graphics.display.width // 2) - 1,
23,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Date
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=((magtag.graphics.display.width // 2) - 1, 40,),
text_position=(
(magtag.graphics.display.width // 2) - 1,
40,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Vaccinated text
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=((magtag.graphics.display.width // 2) - 1, 85,),
text_position=(
(magtag.graphics.display.width // 2) - 1,
85,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Fully vaccinated text
@ -54,15 +66,36 @@ magtag.graphics.splash.append(progress_bar_1)
magtag.graphics.set_background("/bmps/background.bmp")
def l_split(line):
line_list = []
print(line)
while "," in line:
if line[0] == '"':
temp = line.split('"', 2)[1]
line_list.append(temp)
line = line.split('"', 2)[2][1:]
else:
temp, line = line.split(",", 1)
line_list.append(temp)
line_list.append(line)
return line_list
try:
value = magtag.fetch().split("\n")[-2].split(",")
table = magtag.fetch().split("\n")
columns = l_split(table[0])
latest = l_split(table[-2])
print(columns)
print(latest)
value = dict(zip(columns, latest))
print("Response is", value)
print(value)
vaccinated = int(value[7]) / 331984513
fully_vaccinated = int(value[8]) / 331984513
vaccinated = int(value["people_vaccinated"]) / 331984513
fully_vaccinated = int(value["people_fully_vaccinated"]) / 331984513
magtag.set_text(f"{value[0]} Vaccination Rates", 0, False)
magtag.set_text(value[2], 1, False)
magtag.set_text(f"{value['location']} Vaccination Rates", 0, False)
magtag.set_text(value["date"], 1, False)
magtag.set_text("Vaccinated: {:.2f}%".format(vaccinated * 100), 2, False)
magtag.set_text(
"Fully Vaccinated: {:.2f}%".format(fully_vaccinated * 100), 3, False
@ -73,12 +106,11 @@ try:
magtag.refresh()
seconds_to_sleep = 24 * 60 * 60 # Sleep for one day
SECONDS_TO_SLEEP = 24 * 60 * 60 # Sleep for one day
except (ValueError, RuntimeError) as e:
print("Some error occured, retrying in one hour! -", e)
seconds_to_sleep = 60 * 60 # Sleep for one hour
print(f"Sleeping for {seconds_to_sleep} seconds")
magtag.exit_and_deep_sleep(seconds_to_sleep)
print(f"Sleeping for {SECONDS_TO_SLEEP} seconds")
magtag.exit_and_deep_sleep(SECONDS_TO_SLEEP)