-
Notifications
You must be signed in to change notification settings - Fork 1
/
CovPy.py
241 lines (205 loc) · 6.85 KB
/
CovPy.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# tested on android (termux, python)
import os
import time
import requests
from covid import Covid
from termcolor import colored
from prettytable import PrettyTable
covid = Covid()
# these colors might cause errors on windows
class fontcolor:
def green(string):
return colored(string, "green", attrs=["bold"])
def white(string):
return colored(string, "white", attrs=["bold"])
def grey(string):
return colored(string, "grey", attrs=["bold"])
def yellow(string):
return colored(string, "yellow", attrs=["bold"])
def cyan(string):
return colored(string, "cyan", attrs=["bold"])
def red(string):
return colored(string, "red", attrs=["bold"])
def blue(string):
return colored(string, "blue", attrs=["bold"])
class symbol:
WARN = fontcolor.red(" [-] ")
DONE = fontcolor.green(" [+] ")
INPUT = fontcolor.cyan(" [»] ")
INFO = fontcolor.yellow(" [!] ")
ARROW = fontcolor.cyan(" > ")
NUM_1 = fontcolor.yellow(" [1] ")
NUM_2 = fontcolor.yellow(" [2] ")
NUM_3 = fontcolor.yellow(" [3] ")
NUM_4 = fontcolor.yellow(" [4] ")
NUM_5 = fontcolor.yellow(" [5] ")
def print_banner():
banr = """
o-o o-o o o o--o o o
/ o o | | | | \ /
O | | o o O--o O
\ o o \ / | |
o-o o-o o o o
CovPy - Python Covid Tracker
GitHub - @sumit-buddy
"""
print(fontcolor.cyan(banr))
# Main Option Menu
def display_options():
print(symbol.INPUT + fontcolor.cyan("AVAILABLE OPTIONS ↓"))
print(symbol.NUM_1 + fontcolor.white("Global Statistics"))
print(symbol.NUM_2 + fontcolor.white("Country & ID Table"))
print(symbol.NUM_3 + fontcolor.white("Search By Name"))
print(symbol.NUM_4 + fontcolor.white("Search By ID"))
print(symbol.NUM_5 + fontcolor.white("Quit"))
# Check Internet Connection
def check_internet(url="http://www.google.com/", timeout=5):
try:
_ = requests.head(url, timeout=timeout)
print(symbol.DONE + fontcolor.green("Connected Successfully."))
return True
except requests.ConnectionError:
print(symbol.WARN + fontcolor.red("No Internet Connection.") + "\n")
quit()
return False
# Fancy Box Message
def box_msg(msg):
row = len(msg)
m = "".join([" +"] + ["-" * row] + ["+"])
h = fontcolor.cyan(m)
result = (
h
+ "\n"
+ fontcolor.cyan(" |")
+ fontcolor.white(msg)
+ fontcolor.cyan("|")
+ "\n"
+ h
)
print("\n" + result)
# Clear Screen
def clear_screen():
if os.name == "posix":
_ = os.system("clear")
else:
_ = os.system("cls")
# "Press Enter To Continue" Message
def continue_msg():
input("\n" + symbol.INPUT + fontcolor.cyan("Press Enter To Continue : "))
clear_screen()
# Main Menu
def display_menu():
print_banner()
check_internet()
box_msg(" Global Covid-19 Live Information Tracker ")
display_options()
# [1] Global Covid Statistics
def global_stats():
box_msg(" Global Coronavirus Disease Statistics ")
try:
print(
symbol.INFO + fontcolor.white(" Active Cases :"),
"{:,}".format(covid.get_total_active_cases()),
)
print(
symbol.INFO + fontcolor.white("Confirmed Cases :"),
"{:,}".format(covid.get_total_confirmed_cases()),
)
print(
symbol.INFO + fontcolor.white("Recovered Cases :"),
"{:,}".format(covid.get_total_recovered()),
)
print(
symbol.INFO + fontcolor.white(" Death Cases :"),
"{:,}".format(covid.get_total_deaths()),
)
except Exception as e:
print(e)
print("\n" + symbol.WARN + fontcolor.red("No Internet Connection!"))
finally:
continue_msg()
# [2] Print Country & ID Table
def display_name_id():
box_msg(" Countries And Assigned Id Table ")
country_id_table = PrettyTable([fontcolor.cyan("Id"), fontcolor.cyan("Country")])
try:
countries = covid.list_countries()
for country in countries:
country_id_table.add_row(list(country.values()))
country_id_table.add_row(
[fontcolor.grey("-") * 5, fontcolor.grey("-") * 32]
)
print(country_id_table)
except Exception:
print("\n" + symbol.WARN + fontcolor.red("No Internet Connection!"))
finally:
continue_msg()
# [3] Status With Country Name
def status_with_name():
box_msg(" Search Covid-19 Statistics By Country Name ")
country_name_data_table = PrettyTable(
[fontcolor.cyan("Parameter"), fontcolor.cyan("Value")]
)
try:
country_name = input(
symbol.INPUT + fontcolor.cyan("Enter Country Name : ")
).strip()
country_name_stats = covid.get_status_by_country_name(country_name)
for k, v in country_name_stats.items():
country_name_data_table.add_row([fontcolor.white(k), fontcolor.white(v)])
country_name_data_table.add_row(
[fontcolor.grey("-") * 15, fontcolor.grey("-") * 15]
)
print("\n")
print(country_name_data_table)
except:
print(
symbol.WARN + fontcolor.red("Wrong Country Name Or No Internet Connection!")
)
finally:
continue_msg()
# [4] Status With Country ID
def status_with_id():
box_msg(" Search Covid-19 Statistics By Country ID ")
country_id_data_table = PrettyTable(
[fontcolor.cyan("Parameter"), fontcolor.cyan("Value")]
)
try:
country_id = int(input(symbol.INPUT + fontcolor.cyan("Enter Country Id : ")))
country_id_stats = covid.get_status_by_country_id(country_id)
for k, v in country_id_stats.items():
country_id_data_table.add_row(
[fontcolor.white(k).capitalize(), fontcolor.white(v)]
)
country_id_data_table.add_row(
[fontcolor.grey("-") * 15, fontcolor.grey("-") * 15]
)
print("\n")
print(country_id_data_table)
except:
print(
symbol.WARN + fontcolor.red("Wrong Country ID Or No Internet Connection!")
)
finally:
continue_msg()
# Looping All Things
while True:
display_menu()
choice = input(
"\n" + symbol.INPUT + fontcolor.cyan("Enter Your Choice [1,2,3,4,5] : ")
)
if choice == "1":
global_stats()
elif choice == "2":
display_name_id()
elif choice == "3":
status_with_name()
elif choice == "4":
status_with_id()
elif choice == "5":
print(symbol.WARN + fontcolor.red("Quitting...") + "\n")
time.sleep(1)
quit()
else:
print(symbol.WARN + fontcolor.red("Sorry! Enter A Valid Input !"))
break