Display COVID-19 Vaccine Data on a Adafruit MagTag
# SPDX-FileCopyrightText: 2020 ladyada, written for Adafruit Industries | |
# | |
# SPDX-License-Identifier: Unlicense | |
import time | |
import alarm | |
import supervisor | |
import alarm | |
from adafruit_magtag.magtag import MagTag | |
# Change this to the hour you want to check the data at, for us its 8pm | |
# local time (eastern), which is 20:00 hrs | |
DAILY_UPDATE_HOUR = 20 | |
# Set up where we'll be fetching data from | |
DATA_SOURCE = "https://covid.cdc.gov/covid-data-tracker/COVIDData/getAjaxData?id=vaccination_data" | |
# 63 is USA | |
LOCATION_NUM = 63 | |
DATE_LOCATION = ["vaccination_data", LOCATION_NUM, 'Date'] | |
NAME_LOCATION = ["vaccination_data", LOCATION_NUM, 'LongName'] | |
CENSUS_LOACTION = ["vaccination_data", LOCATION_NUM, 'Census2019'] | |
ADMINISTER_1_LOCATION = ["vaccination_data", LOCATION_NUM, 'Administered_Dose1'] | |
ADMINISTER_2_LOCATION = ["vaccination_data", LOCATION_NUM, 'Administered_Dose2'] | |
magtag = MagTag( | |
url=DATA_SOURCE, | |
json_path=(DATE_LOCATION, NAME_LOCATION, | |
CENSUS_LOACTION, ADMINISTER_1_LOCATION, | |
ADMINISTER_2_LOCATION), | |
) | |
# Date stamp of info | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 15), | |
text_transform=lambda x: "Date: {}".format(x[0:10]), | |
) | |
# Location Country/State | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 35), | |
text_transform=lambda x: "Location: {}".format(x), | |
) | |
# Population | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 55), | |
text_transform=lambda x: "Population: {:,}".format(x), | |
) | |
# Vaccinated with 1 Dose | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 75), | |
text_transform=lambda x: "1 Dose : {:,}".format(x), | |
) | |
# Vaccinated with 2 Dose | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 95), | |
text_transform=lambda x: "2 Doses: {:,}".format(x), | |
) | |
# Percent Fully Vaccinated | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 115), | |
is_data=False | |
) | |
# updated time | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(245, 30), | |
line_spacing=0.75, | |
is_data=False | |
) | |
magtag.graphics.qrcode(b"https://covid.cdc.gov/covid-data-tracker/#vaccinations", | |
qr_size=2, x=240, y=70) | |
magtag.peripherals.neopixels.brightness = 0.1 | |
magtag.peripherals.neopixel_disable = False # turn on lights | |
magtag.peripherals.neopixels.fill(0x0F0000) # red! | |
magtag.get_local_time() | |
try: | |
now = time.localtime() | |
print("Now: ", now) | |
# display the current time since its the last-update | |
updated_at = "%d/%d\n%d:%02d" % now[1:5] | |
magtag.set_text(updated_at, 6, False) | |
# get data from the CDC | |
value = magtag.fetch() | |
print("Response is", value) | |
magtag.set_text(f"Fully Vaccinated: {(value[4]/value[2])*100:.2f}%", 5, True) | |
# OK we're done! | |
magtag.peripherals.neopixels.fill(0x000F00) # greten | |
except (ValueError, RuntimeError) as e: | |
print("Some error occured, trying again later -", e) | |
time.sleep(2) # let screen finish updating | |
# we only wanna wake up once a day, around the event update time: | |
event_time = time.struct_time((now[0], now[1], now[2], | |
DAILY_UPDATE_HOUR, 0, 0, | |
-1, -1, now[8])) | |
# how long is that from now? | |
remaining = time.mktime(event_time) - time.mktime(now) | |
if remaining < 0: # ah its aready happened today... | |
remaining += 24 * 60 * 60 # wrap around to the next day | |
remaining_hrs = remaining // 3660 | |
remaining_min = (remaining % 3600) // 60 | |
print("Gonna zzz for %d hours, %d minutes" % (remaining_hrs, remaining_min)) | |
# Turn it all off and go to bed till the next update time | |
magtag.exit_and_deep_sleep(remaining) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
For the QR position, I had to place it up and left to fit the entire thing on my MagTag screen.
qr_size=2, x=225, y=55