def weathermethod(cityparam): try: city=cityparam url="https://openweathermap.org/data/2.5/weather?q={}&appid=439d4b804bc8187953eb36d2a8c26a02".format(city) #get the url res=requests.get(url) #jason format output=res.json() #ouput contains a dictionary which holds the weather information so we extract data from dictionary in output weather_status=output['weather'][0]['description'] temperature=output['main']['temp'] humidity=output['main']['humidity'] wind_speed=output['wind']['speed'] weather=" The weather status in "+city+" is "+ output['weather'][0]['description'] +"and temperature is"+str(output['main']['temp'])+" degree swith humidity "+str(output['main']['humidity'])+"the wind speed is "+str(output['wind']['speed']) print("weather_status",output['weather'][0]['description']) print("temperature",output['main']['temp']) print("humidity",output['main']['humidity']) print("wind_speed",output['wind']['speed']) # print(weather) #call speak method return weather #speakclass.speakmethod(weather) except: notification.notify(title="Virtual assistant", message="Something went wrong,do check the guide ", timeout=5) speakclass.speakmethod("location not found")
def wikisearch(text):#searchkeylis=[tell, me, something ,about ,india] start_time = time.time() key=['what','who','is','tell','me','something','describe','give','some','details','about','when','was','wiki','wikipedia','the','according','to'] searchkeylist=text.split() for x in searchkeylist: #print(x) if x in key: continue else: searchkey=x break print(searchkey) #try: wiki_wiki = wikipediaapi.Wikipedia('en') page_py = wiki_wiki.page(searchkey) webbrowser.open(page_py.fullurl) x = (time.time() - start_time) notification.notify(title="Virtual assistant", message=" we found this in " + str(x) + "seconds", timeout=5) result = wikipedia.summary(searchkey, sentences=2) #print(result) #notification.notify(title="Virtual as # sistant", message=" we found this", timeout=5) writeresults=wikipedia.page(searchkey).content handle=open('scratch.txt',mode='w',encoding='utf-8') handle.write(writeresults) handle.close() root=Tk() buttoncopy=Button(root,text='copy results',command=lambda :os.startfile('scratch.txt')) speakclass.speakmethod(result) buttoncopy.pack() root.mainloop()
def googlesearch(text): print("HERE ARE RESULTS FOR ", text) st = "https://www.google.com/search?q={}".format(text) webbrowser.open(st) notification.notify(title="Virtual assistant", message=" we found some results", timeout=5) speakclass.speakmethod("according to google")
def browsermethod(text): x = text.lower() handle = open('browsermap.txt', mode='r', encoding="utf-8") for line in handle: if line.startswith(x): l = line.split(':') speakclass.speakmethod("OPENING " + l[0]) webbrowser.open(l[1])
def loadhistory(a): conn = sqlite3.connect('historydb.sqlite') cur = conn.cursor() if a == 0: speakclass.speakmethod("deleting history") cur.execute("delete from history") notification.notify(title="Virtual assistant", message="history cleared", timeout=5) cur.close() conn.commit() else: cur.execute("SELECT d from history") row = cur.fetchall() cur.close() conn.commit() new_lst = row[::-1] return new_lst
def changeMonth(a): con = psycopg2.connect( host='localhost', database='Auth', user='******', password='******') cur = con.cursor() if a == 0: speakclass.speakmethod("deleting history") cur.execute("delete from history") notification.notify(title="Virtual assistant", message="history cleared", timeout=5) cur.close() con.commit() else: cur.execute("select d from history") row = cur.fetchall() cur.close() con.commit() new_lst = row[::-1] return new_lst
def audiototext(searchentry): searchentry.delete(0,END) speakclass.speakmethod("What can i do for you ") print("LISTENING :") r=sr.Recognizer() mic=sr.Microphone(device_index=1) with mic as source: #store audio from mic in var audio audio=r.listen(source) try: #call method for text recogniton in speech search_text=r.recognize_google(audio) searchentry.insert(0,search_text) print(search_text) except: speakclass.speakmethod("sorry i didnt get you") print("error") speakclass.speakmethod("hold on I'm getting the results") searchclass.searchmethod(search_text)
def searchmethod(search_text): inserthistory(search_text)#log the commands to store history if('weather' in search_text): l=search_text.split() city=l[-1] result=weatherinfoclass.weathermethod(city)#weather API st = "https://www.google.com/search?q={}".format(search_text) webbrowser.open(st) notification.notify(title="Virtual assistant", message=" we found some results", timeout=5) speakclass.speakmethod(result) #searchclass.displayres(result) elif('camera' in search_text): speakclass.speakmethod('opening camera') subprocess.run('start microsoft.windows.camera:', shell=True) notification.notify(title="Virtual assistant", message="Camera opened", timeout=5) elif('file' in search_text): l = search_text.split() file_name = l[-1] print(file_name) s="searching for the file"+file_name speakclass.speakmethod(s) print(file_name) try: result="path is "+ searchfiles(file_name) except: notification.notify(title="Virtual assistant", message="SORRY FILE NOT FOUND ", timeout=5) speakclass.speakmethod('file not found') notification.notify(title="Virtual assistant", message="file opened succesfully " +result, timeout=5) speakclass.speakmethod(file_name+"opened successfully") elif("bye" in search_text): speakclass.speakmethod(" Bye take care") sys.exit() elif ('open' in search_text): l=search_text.split() p=l[1].lower() browserclass.browsermethod(p) notification.notify(title="Virtual assistant", message=p+" opened succesfully",timeout=5) elif("entertain" in search_text): webbrowser.open('www.youtube.com/feed/trending') #elif('songs' in search_text or 'music' in search_text): #webbrowser.open() elif ('google'in search_text or 'Google' in search_text): res = search_text.split(' ', 1)[1] searchnet.googlesearch(res) else:#uses wikipedia searchnet.wikisearch(search_text)
def searhfromtext(searchentry): search_text=searchentry.get() print(search_text) speakclass.speakmethod("hold on I'm getting the results") searchclass.searchmethod(search_text)