This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
/
app.py
119 lines (94 loc) 路 3.62 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import rumps
import requests
import datetime
import webbrowser
class CoronaBar(object):
base_api_url = "https://coronavirus-19-api.herokuapp.com/countries"
default_country = "USA"
update_interval = 900 # seconds
about_url = "https://github.com/duarteocarmo/coronabar"
# APP
def __init__(self):
self.app = rumps.App("Corona Bar", "馃")
self.countries = rumps.MenuItem(title="Select Country")
self.about = rumps.MenuItem(title="About", callback=self.open_page)
self.country = self.default_country
self.app.menu = [self.countries, self.about]
country_list = self.get_country_list()
self.setup(country_list, self.country)
def setup(self, country_list, default_country):
for country in country_list:
self.countries.add(
rumps.MenuItem(
title=f"{country}", callback=self.update_country_listing
)
)
self.create_country_listing(
rumps.MenuItem(title=f"{self.default_country}")
)
def open_page(self, sender):
try:
webbrowser.open(self.about_url)
except Exception as e:
print(str(e))
return False
def create_country_listing(self, country):
# print("Creating")
data = self.get_country_data(country.title)
for k, v in data.items():
self.app.menu.add(rumps.MenuItem(self.string_mapper(k, v)))
current_time = datetime.datetime.now().strftime("%H:%M")
self.app.menu.add(rumps.MenuItem(title=f"Updated at {current_time}"))
# print("Created")
self.timer = rumps.Timer(self.on_update, self.update_interval)
self.timer.start()
def update_country_listing(self, country):
# print("Update")
self.country = country.title
for k, v in self.app.menu.items():
if k not in ["Select Country", "Quit", "About"]:
del self.app.menu[k]
data = self.get_country_data(country.title)
for k, v in data.items():
self.app.menu.insert_before(
"Quit", rumps.MenuItem(self.string_mapper(k, v))
)
current_time = datetime.datetime.now().strftime("%H:%M")
self.app.menu.insert_before(
"Quit", rumps.MenuItem(title=f"Updated at {current_time}")
)
# print(f"Updated,country is {self.country}")
def on_update(self, sender):
# print("Timer is running.")
self.update_country_listing(rumps.MenuItem(title=self.country))
def run(self):
self.app.run()
# DATA
def get_country_list(self):
response = requests.request("GET", self.base_api_url)
data = response.json()
country_list = [e["country"] for e in data]
return sorted(country_list)
def get_country_data(self, country):
response = requests.request("GET", f"{self.base_api_url}/{country}")
data = response.json()
return data
# STRING MAPPER
def string_mapper(self, key, value):
if not self.is_camel_case(key):
return f"{key.title()}: {value}"
elif key == "todayCases":
return f"Cases Today: {value}"
elif key == "todayDeaths":
return f"Deaths Today: {value}"
elif key == "casesPerOneMillion":
return f"Per Million: {value}"
else:
return f"{key}: {value}"
@staticmethod
def is_camel_case(s):
# https://stackoverflow.com/a/10182901 thanks :)
return s != s.lower() and s != s.upper() and "_" not in s
if __name__ == "__main__":
app = CoronaBar()
app.run()