-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (142 loc) · 4.82 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
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
from bs4 import BeautifulSoup
import requests
import re
import pandas
import pyttsx3
import speech_recognition as sr
from selenium import webdriver
def speak(text):
engine = pyttsx3.init()
engine.setProperty('rate',150)
engine.setProperty('voice','en+m7')
engine.say(text)
engine.runAndWait()
def get_audio():
r=sr.Recognizer()
print("Listening.....")
with sr.Microphone() as source:
audio = r.listen(source)
said=""
print("loading.....")
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: "+str(e))
return said.lower()
def load_data():
r = requests.get("https://news.google.com/covid19/map?hl=en-IN&gl=IN&ceid=IN%3Aen", headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content
soup = BeautifulSoup(c,"html.parser")
l=[]
d={}
id = 0
worldwide = soup.find("tr",{"class":"sgXwHf wdLSAe ROuVee"})
d['ID'] = id
d['Country'] = (worldwide.find("th",{"class":"l3HOY"}).text)
d['Confirmed'] =(worldwide.find_all("td",{"class":"l3HOY"})[0].text)
d['CPMP'] = (worldwide.find_all("td",{"class":"l3HOY"})[1].text)
d['Recovered'] = (worldwide.find_all("td",{"class":"l3HOY"})[2].text)
d['Death'] = (worldwide.find_all("td",{"class":"l3HOY"})[3].text)
l.append(d)
all = soup.find_all("tr",{"class":"sgXwHf wdLSAe YvL7re"})
id +=1
for item in all:
d={}
d['ID'] = id
d['Country'] = (item.find("div",{"class":"pcAJd"}).text)
d['Confirmed'] =(item.find_all("td",{"class":"l3HOY"})[0].text)
d['CPMP'] = (item.find_all("td",{"class":"l3HOY"})[1].text)
d['Recovered'] = (item.find_all("td",{"class":"l3HOY"})[2].text)
d['Death'] = (item.find_all("td",{"class":"l3HOY"})[3].text)
id +=1
l.append(d)
r = requests.get("https://news.google.com/covid19/map?hl=en-IN&gl=IN&ceid=IN%3Aen&mid=%2Fm%2F03rk0", headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content
soup = BeautifulSoup(c,"html.parser")
india = soup.find_all("tr",{"class":"sgXwHf wdLSAe YvL7re"})
for item in india:
d={}
d['ID'] = id
d['Country'] = (item.find("div",{"class":"pcAJd"}).text)
d['Confirmed'] =(item.find_all("td",{"class":"l3HOY"})[0].text)
d['CPMP'] = (item.find_all("td",{"class":"l3HOY"})[1].text)
d['Recovered'] = (item.find_all("td",{"class":"l3HOY"})[2].text)
d['Death'] = (item.find_all("td",{"class":"l3HOY"})[3].text)
id +=1
l.append(d)
return l
def get_data(text):
text = text.lower()
country = None
case = None
active = None
death = None
recover =None
for word in text.split():
for i in df['Country']:
if word == i.lower():
country= i
try:
if country == None:
country = df['Country'][0]
if text.count("cases") > 0 or text.count("case")>0 or text.count("confirm")>0 or text.count("confirmed")>0:
case = (df[df.Country == country].Confirmed).tolist()
result = case
if text.count("active") > 0:
case = (df[df.Country == country].Confirmed).tolist()
recover=(df[df.Country==country].Recovered).tolist()
case = "".join(case[0].split(","))
recover = "".join(recover[0].split(","))
result = []
result.append(str(int(case)-int(recover)))
if text.count("death") > 0:
death = (df[df.Country==country].Death).tolist()
print(death)
result =death
if text.count("recovered") > 0 or text.count("recover") > 0:
recover = (df[df.Country==country].Recovered).tolist()
result =recover
result = "".join(result[0].split(","))
print(result)
except:
result = None
if case != None:
speak("There are "+str(result)+" cases in "+ country)
elif death !=None:
speak("There are "+str(result)+" deaths in" +country)
elif recover!=None:
speak("There are "+str(result)+" recovered cases in"+ country)
else:
try:
search = text.replace(" ","+")
speak("This is what I found on web")
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.google.com/search?q="+search+"&oq="+search+"&aqs=chrome.0.0j69i57j0l3.9430j0j7&sourceid=chrome&ie=UTF-8")
except:
pass
speak("wait for few seconds, while it's loading")
print("Loading....")
l = load_data()
df= pandas.DataFrame(l)
df.set_index("ID",inplace = True)
df.to_csv("Output.csv")
speak("Hi, I am Corona virus voice assistant. Made by Harsh Gandhi. I can give you latest updates on corona virus.")
speak("say 'hello' to wake me up")
speak("say 'goodbye' to make me sleep")
WAKE = "hello"
GOODBYE = "goodbye"
while True:
text = get_audio()
if text.count(WAKE) > 0:
speak("I am ready")
text = get_audio()
if text.count(GOODBYE) > 0:
speak("Good bye!")
break
else:
get_data(text)
if text.count(GOODBYE) > 0:
speak("Good bye!")
break