-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
94 lines (81 loc) · 3.3 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from utils import (
get_summary,
generate_icon_path,
remove_unnecessary_info,
beautify_stats,
beautify_key,
)
class Covid_19(Extension):
def __init__(self):
super().__init__()
self.subscribe(KeywordQueryEvent, KeywordEventListener())
class KeywordEventListener(EventListener):
data = get_summary()
def on_event(self, event, extension):
query = event.get_argument()
results = []
if query is None:
req = self.data.get("Global")
results.append(
ExtensionResultItem(
name="World",
description="World Status",
on_enter=HideWindowAction(),
icon=f"images/emoji/World.png",
)
)
world_summary = remove_unnecessary_info(req)
for key, value in world_summary.items():
results.append(
ExtensionResultItem(
name=beautify_stats(value),
description=beautify_key(key),
on_enter=HideWindowAction(),
icon=generate_icon_path(key),
)
)
return RenderResultListAction(results)
elif query is not None and len(query) >= 2:
countries = self.data.get("Countries")
filtered_countries = list(
filter(
lambda country: country.get("CountryCode") == query.upper(),
countries,
)
)
if len(filtered_countries) == 1:
country = dict(filtered_countries[0])
flag_code = country.pop("CountryCode").lower()
country = remove_unnecessary_info(country)
for key, value in country.items():
if key == "Country":
genereated_icon = generate_icon_path(key, flag_code)
else:
genereated_icon = generate_icon_path(key)
results.append(
ExtensionResultItem(
name=beautify_stats(value),
description=beautify_key(key),
on_enter=HideWindowAction(),
icon=genereated_icon,
)
)
return RenderResultListAction(results)
else:
results.append(
ExtensionResultItem(
name="No data found",
description="No country or data found. Try again!",
on_enter=HideWindowAction(),
icon=f"images/emoji/NotFound.png",
)
)
return RenderResultListAction(results)
if __name__ == "__main__":
Covid_19().run()