forked from yashagarwal971/voice-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corona.py
165 lines (112 loc) · 3.78 KB
/
corona.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
import requests
import json
import pyttsx3
import speech_recognition as sr
import re
import threading
import time
API_KEY = "t0vubbcLt8Oa"
PROJECT_TOKEN = "t-AMctfT2Ou1"
RUN_TOKEN = "tTLRCW_8KGiX"
class Data:
def __init__(self, api_key, project_token):
self.api_key = api_key
self.project_token = project_token
self.params = {
"api_key": self.api_key
}
self.get_data()
def get_data(self):
response = requests.get(f'https://www.parsehub.com/api/v2/projects/{PROJECT_TOKEN}/last_ready_run/data', params={"api_key": API_KEY})
self.data = json.loads(response.text)
def get_total_cases(self):
data = self.data['total']
for content in data:
if content['name'] == "Coronavirus Cases:":
return content['values']
def get_total_death(self):
data = self.data['total']
for content in data:
if content['name'] == "Death:":
return content['values']
return "0"
def get_country_data(self, country):
data = self.data["country"]
for content in data:
if content['name'].lower() == country.lower():
return content
return "0"
def get_list_of_countries(self):
countries = []
for country in self.data['country']:
countries.append(country['name'].lower())
return countries
def update_data(self):
response = requests.post(f'https://www.parsehub.com/api/v2/projects/{self.project_token}/run',params=self.params)
def poll():
time.sleep(0.1)
old_data = self.data
while True:
new_data = self.get_data()
if new_data != old_data:
self.data = new_data
print("Data updated")
break
time.sleep(5)
t = threading.Thread(target=poll)
t.start()
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
except Exception as e:
print("Exception:", str(e))
return said.lower()
def main():
print("Started Program")
data = Data(API_KEY, PROJECT_TOKEN)
END_PHRASE = "stop"
country_list = data.get_list_of_countries()
TOTAL_PATTERNS = {
re.compile("[\w\s]+ total [\w\s]+ cases"):data.get_total_cases,
re.compile("[\w\s]+ total cases"): data.get_total_cases,
re.compile("[\w\s]+ total [\w\s]+ death"): data.get_total_death,
re.compile("[\w\s]+ total death"): data.get_total_death
}
COUNTRY_PATTERNS = {
re.compile("[\w\s]+ cases [\w\s]+"): lambda country: data.get_country_data(country)['total_cases'],
re.compile("[\w\s]+ deaths [\w\s]+"): lambda country: data.get_country_data(country)['total_deaths'],
}
UPDATE_COMMAND = "update"
while True:
print("Listening...")
text = get_audio()
print(text)
result = None
for pattern, func in COUNTRY_PATTERNS.items():
if pattern.match(text):
words = set(text.split(" "))
for country in country_list:
if country in words:
result = func(country)
break
for pattern, func in TOTAL_PATTERNS.items():
if pattern.match(text):
result = func()
break
if text == UPDATE_COMMAND:
result = "Data is being updated. This may take a moment!"
data.update_data()
if result:
speak(result)
if text.find(END_PHRASE) != -1: # stop loop
print("Exit")
break
main()