/ MagTag_Covid_Vaccination / code.py
code.py
1 # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 from adafruit_magtag.magtag import MagTag 6 from adafruit_progressbar.progressbar import ProgressBar 7 8 # Set up where we'll be fetching data from 9 DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv" # pylint: disable=line-too-long 10 # Find data for other countries/states here: 11 # https://github.com/owid/covid-19-data/tree/master/public/data/vaccinations 12 13 magtag = MagTag(url=DATA_SOURCE) 14 magtag.network.connect() 15 16 magtag.add_text( 17 text_font="/fonts/ncenR14.pcf", 18 text_position=( 19 (magtag.graphics.display.width // 2) - 1, 20 8, 21 ), 22 text_anchor_point=(0.5, 0.5), 23 is_data=False, 24 ) # Title 25 26 magtag.add_text( 27 text_font="/fonts/ncenR14.pcf", 28 text_position=( 29 (magtag.graphics.display.width // 2) - 1, 30 23, 31 ), 32 text_anchor_point=(0.5, 0.5), 33 is_data=False, 34 ) # Date 35 36 magtag.add_text( 37 text_font="/fonts/ncenR14.pcf", 38 text_position=( 39 (magtag.graphics.display.width // 2) - 1, 40 40, 41 ), 42 text_anchor_point=(0.5, 0.5), 43 is_data=False, 44 ) # Vaccinated text 45 46 magtag.add_text( 47 text_font="/fonts/ncenR14.pcf", 48 text_position=( 49 (magtag.graphics.display.width // 2) - 1, 50 85, 51 ), 52 text_anchor_point=(0.5, 0.5), 53 is_data=False, 54 ) # Fully vaccinated text 55 56 BAR_WIDTH = magtag.graphics.display.width - 80 57 BAR_HEIGHT = 25 58 BAR_X = magtag.graphics.display.width // 2 - BAR_WIDTH // 2 59 60 progress_bar = ProgressBar( 61 BAR_X, 50, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000 62 ) 63 64 progress_bar_1 = ProgressBar( 65 BAR_X, 95, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000 66 ) 67 68 magtag.graphics.splash.append(progress_bar) 69 magtag.graphics.splash.append(progress_bar_1) 70 magtag.graphics.set_background("/bmps/background.bmp") 71 72 73 def l_split(line): 74 line_list = [] 75 print(line) 76 while "," in line: 77 if line[0] == '"': 78 temp = line.split('"', 2)[1] 79 line_list.append(temp) 80 line = line.split('"', 2)[2][1:] 81 else: 82 temp, line = line.split(",", 1) 83 line_list.append(temp) 84 line_list.append(line) 85 return line_list 86 87 88 try: 89 table = magtag.fetch().split("\n") 90 columns = l_split(table[0]) 91 latest = l_split(table[-2]) 92 print(columns) 93 print(latest) 94 value = dict(zip(columns, latest)) 95 print("Response is", value) 96 print(value) 97 98 vaccinated = int(value["people_vaccinated"]) / 331984513 99 fully_vaccinated = int(value["people_fully_vaccinated"]) / 331984513 100 101 magtag.set_text(f"{value['location']} Vaccination Rates", 0, False) 102 magtag.set_text(value["date"], 1, False) 103 magtag.set_text("Vaccinated: {:.2f}%".format(vaccinated * 100), 2, False) 104 magtag.set_text( 105 "Fully Vaccinated: {:.2f}%".format(fully_vaccinated * 100), 3, False 106 ) 107 108 progress_bar.progress = vaccinated 109 progress_bar_1.progress = fully_vaccinated 110 111 magtag.refresh() 112 113 SECONDS_TO_SLEEP = 24 * 60 * 60 # Sleep for one day 114 115 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 116 print("Some error occured, retrying in one hour! -", e) 117 seconds_to_sleep = 60 * 60 # Sleep for one hour 118 119 print(f"Sleeping for {SECONDS_TO_SLEEP} seconds") 120 magtag.exit_and_deep_sleep(SECONDS_TO_SLEEP)