Skip to content

Commit d2174e5

Browse files
authored
Merge pull request #1819 from dherrada/vaxx-fix
Came up with better way to get values
2 parents e4d0a8a + cf5410a commit d2174e5

1 file changed

Lines changed: 46 additions & 14 deletions

File tree

MagTag_Covid_Vaccination/code.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from adafruit_progressbar.progressbar import ProgressBar
33

44
# Set up where we'll be fetching data from
5-
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv"
5+
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv" # pylint: disable=line-too-long
66
# Find data for other countries/states here:
77
# https://github.com/owid/covid-19-data/tree/master/public/data/vaccinations
88

@@ -11,28 +11,40 @@
1111

1212
magtag.add_text(
1313
text_font="/fonts/ncenR14.pcf",
14-
text_position=((magtag.graphics.display.width // 2) - 1, 8,),
14+
text_position=(
15+
(magtag.graphics.display.width // 2) - 1,
16+
8,
17+
),
1518
text_anchor_point=(0.5, 0.5),
1619
is_data=False,
1720
) # Title
1821

1922
magtag.add_text(
2023
text_font="/fonts/ncenR14.pcf",
21-
text_position=((magtag.graphics.display.width // 2) - 1, 23,),
24+
text_position=(
25+
(magtag.graphics.display.width // 2) - 1,
26+
23,
27+
),
2228
text_anchor_point=(0.5, 0.5),
2329
is_data=False,
2430
) # Date
2531

2632
magtag.add_text(
2733
text_font="/fonts/ncenR14.pcf",
28-
text_position=((magtag.graphics.display.width // 2) - 1, 40,),
34+
text_position=(
35+
(magtag.graphics.display.width // 2) - 1,
36+
40,
37+
),
2938
text_anchor_point=(0.5, 0.5),
3039
is_data=False,
3140
) # Vaccinated text
3241

3342
magtag.add_text(
3443
text_font="/fonts/ncenR14.pcf",
35-
text_position=((magtag.graphics.display.width // 2) - 1, 85,),
44+
text_position=(
45+
(magtag.graphics.display.width // 2) - 1,
46+
85,
47+
),
3648
text_anchor_point=(0.5, 0.5),
3749
is_data=False,
3850
) # Fully vaccinated text
@@ -54,15 +66,36 @@
5466
magtag.graphics.set_background("/bmps/background.bmp")
5567

5668

69+
def l_split(line):
70+
line_list = []
71+
print(line)
72+
while "," in line:
73+
if line[0] == '"':
74+
temp = line.split('"', 2)[1]
75+
line_list.append(temp)
76+
line = line.split('"', 2)[2][1:]
77+
else:
78+
temp, line = line.split(",", 1)
79+
line_list.append(temp)
80+
line_list.append(line)
81+
return line_list
82+
83+
5784
try:
58-
value = magtag.fetch().split("\n")[-2].split(",")
85+
table = magtag.fetch().split("\n")
86+
columns = l_split(table[0])
87+
latest = l_split(table[-2])
88+
print(columns)
89+
print(latest)
90+
value = dict(zip(columns, latest))
5991
print("Response is", value)
92+
print(value)
6093

61-
vaccinated = int(value[7]) / 331984513
62-
fully_vaccinated = int(value[8]) / 331984513
94+
vaccinated = int(value["people_vaccinated"]) / 331984513
95+
fully_vaccinated = int(value["people_fully_vaccinated"]) / 331984513
6396

64-
magtag.set_text(f"{value[0]} Vaccination Rates", 0, False)
65-
magtag.set_text(value[2], 1, False)
97+
magtag.set_text(f"{value['location']} Vaccination Rates", 0, False)
98+
magtag.set_text(value["date"], 1, False)
6699
magtag.set_text("Vaccinated: {:.2f}%".format(vaccinated * 100), 2, False)
67100
magtag.set_text(
68101
"Fully Vaccinated: {:.2f}%".format(fully_vaccinated * 100), 3, False
@@ -73,12 +106,11 @@
73106

74107
magtag.refresh()
75108

76-
seconds_to_sleep = 24 * 60 * 60 # Sleep for one day
109+
SECONDS_TO_SLEEP = 24 * 60 * 60 # Sleep for one day
77110

78111
except (ValueError, RuntimeError) as e:
79112
print("Some error occured, retrying in one hour! -", e)
80113
seconds_to_sleep = 60 * 60 # Sleep for one hour
81114

82-
print(f"Sleeping for {seconds_to_sleep} seconds")
83-
magtag.exit_and_deep_sleep(seconds_to_sleep)
84-
115+
print(f"Sleeping for {SECONDS_TO_SLEEP} seconds")
116+
magtag.exit_and_deep_sleep(SECONDS_TO_SLEEP)

0 commit comments

Comments
 (0)