|
| 1 | +import speech_recognition as sr |
| 2 | +import datetime |
| 3 | +import webbrowser as wb |
| 4 | +import os |
| 5 | +import pyttsx3 |
| 6 | +import wikipedia |
| 7 | +import pywhatkit |
| 8 | +import pyjokes as pj |
| 9 | +import smtplib |
| 10 | +import requests |
| 11 | +from bs4 import BeautifulSoup |
| 12 | +import winsound |
| 13 | +import operator |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +engine = pyttsx3.init() |
| 18 | + |
| 19 | + |
| 20 | +def search(): |
| 21 | + speech("enter the topic you want to search on wikipedia ") |
| 22 | + query= takeCommand().lower() |
| 23 | + wresult = str(wikipedia.search(query, results = 1)) |
| 24 | + results = wikipedia.summary(wresult, sentences=2) |
| 25 | + speech("According to Wikipedia") |
| 26 | + print(results) |
| 27 | + speech(results) |
| 28 | + |
| 29 | +def wishMe(): |
| 30 | + hour = int(datetime.datetime.now().hour) |
| 31 | + |
| 32 | + if(hour >= 0 and hour <12 ): |
| 33 | + wish = 'good morning' |
| 34 | + #speech('good morning') |
| 35 | + #print('good morning') |
| 36 | + elif(hour >=12 and hour<= 18): |
| 37 | + wish = 'good afternoon' |
| 38 | + #speech('good afternoon') |
| 39 | + #print('good afternoon') |
| 40 | + else: |
| 41 | + wish = 'good evening' |
| 42 | + #speech('good evening') |
| 43 | + # print('good evening') |
| 44 | + |
| 45 | + return wish |
| 46 | + |
| 47 | +def Whatsapp(): |
| 48 | + speech('opening whatsApp') |
| 49 | + os.startfile('C:\\Users\\avyay\\AppData\\Local\\WhatsApp\\WhatsApp.exe') |
| 50 | + |
| 51 | +def chrome(): |
| 52 | + speech('opening chrome') |
| 53 | + os.startfile('C:\Program Files\Google\Chrome\Application\chrome.exe') |
| 54 | + |
| 55 | +def speech(audio): |
| 56 | + engine.setProperty('rate', 200) |
| 57 | + voices = engine.getProperty('voices') #getting details of current voice |
| 58 | + #engine.setProperty('voice', voices[1].id) #changing index, changes voices. o for male |
| 59 | + engine.setProperty('voice', voices[0].id) |
| 60 | + engine.say(audio) |
| 61 | + engine.runAndWait() |
| 62 | + |
| 63 | +def dateAndTime(): |
| 64 | + time = datetime.datetime.now().strftime("%Y\n %B\n %A\n %I %M %S %Z") |
| 65 | + print(time) |
| 66 | + speech(time) |
| 67 | + |
| 68 | +def google(): |
| 69 | + wb.open('google.com') |
| 70 | + speech("opening google") |
| 71 | + |
| 72 | +def takeCommand(): |
| 73 | + #It takes microphone input from the user and returns string output |
| 74 | + |
| 75 | + r = sr.Recognizer() |
| 76 | + with sr.Microphone() as source: |
| 77 | + r.adjust_for_ambient_noise(source,duration=1) |
| 78 | + print() |
| 79 | + print("Listening...") |
| 80 | + print() |
| 81 | + #r.pause_threshold = 1 |
| 82 | + audio = r.listen(source) |
| 83 | + |
| 84 | + try: |
| 85 | + print("Recognizing...") |
| 86 | + print() |
| 87 | + query = r.recognize_google(audio, language='en-in') |
| 88 | + print(f"you said: {query}\n") |
| 89 | + |
| 90 | + except Exception as e: |
| 91 | + print("Say that again please...") |
| 92 | + print() |
| 93 | + return "None" |
| 94 | + return query |
| 95 | + |
| 96 | +def playmusic(): |
| 97 | + speech("what would you like to play ") |
| 98 | + song = takeCommand() |
| 99 | + speech('playing '+song) |
| 100 | + pywhatkit.playonyt(song) |
| 101 | + |
| 102 | +def joke(): |
| 103 | + speech('telling a joke') |
| 104 | + a = pj.get_joke(language='en',category='neutral') |
| 105 | + speech(a) |
| 106 | + print(a) |
| 107 | + |
| 108 | +def searchOnGoogle(): |
| 109 | + speech('what would you like to search on google') |
| 110 | + print('what would you like to search on google') |
| 111 | + a= takeCommand() |
| 112 | + pywhatkit.search(a) |
| 113 | + |
| 114 | +def weather(): |
| 115 | + speech("please tell city name ") |
| 116 | + |
| 117 | + city = takeCommand().lower() |
| 118 | + print(city) |
| 119 | + |
| 120 | + url = "https://google.com/search?q="+"weather"+city |
| 121 | + html = requests.get(url).content |
| 122 | + |
| 123 | + soup = BeautifulSoup(html,'html.parser') |
| 124 | + |
| 125 | + temp = soup.find('div',attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text |
| 126 | + time_skyDescription = soup.find('div',attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text |
| 127 | + |
| 128 | + data = time_skyDescription.split('\n') |
| 129 | + time = data[0] |
| 130 | + sky = data[1] |
| 131 | + |
| 132 | + listdiv = soup.findAll('div',attrs={'class': 'BNeawe s3v9rd AP7Wnd'}) |
| 133 | + |
| 134 | + |
| 135 | + strd = listdiv[5].text |
| 136 | + |
| 137 | + pos = strd.find('wind') |
| 138 | + otherData= strd[pos:] |
| 139 | + |
| 140 | + print("Temperature is ",temp) |
| 141 | + speech("Temperature is "+temp) |
| 142 | + print("time is ",time) |
| 143 | + speech("time is "+time) |
| 144 | + print("sky description: ",sky) |
| 145 | + speech("sky description: "+sky) |
| 146 | + print(otherData) |
| 147 | + |
| 148 | +def alarm(Timing): |
| 149 | + |
| 150 | + altime = str(datetime.datetime.now().strptime(Timing,"%I:%M %p")) |
| 151 | + |
| 152 | + |
| 153 | + altime = altime[11:-3] |
| 154 | + print(altime) |
| 155 | + Horeal = altime[:2] |
| 156 | + Horeal = int(Horeal) |
| 157 | + Mireal = altime[3:5] |
| 158 | + Mireal = int(Mireal) |
| 159 | + |
| 160 | + print(f"Done, alarm is set for {Timing}") |
| 161 | + |
| 162 | + while True: |
| 163 | + if Horeal == datetime.datetime.now().hour and Mireal == datetime.datetime.now().minute: |
| 164 | + print("alarm is running") |
| 165 | + winsound.PlaySound('abc',winsound.SND_LOOP) |
| 166 | + |
| 167 | + elif Mireal<datetime.datetime.now().minute: |
| 168 | + break |
| 169 | + |
| 170 | +def calculate(): |
| 171 | + r = sr.Recognizer() |
| 172 | + with sr.Microphone( )as source: |
| 173 | + speech("say what you want to calculate, example 3 plus 3") |
| 174 | + print("Listening...") |
| 175 | + r.adjust_for_ambient_noise(source) |
| 176 | + audio = r.listen(source) |
| 177 | + my_string = r.recognize_google(audio) |
| 178 | + print(my_string) |
| 179 | + def get_operator_fn(op): |
| 180 | + return{ |
| 181 | + '+':operator.add, |
| 182 | + '-':operator.sub, |
| 183 | + '*':operator.mul, |
| 184 | + 'divided':operator.__truediv__, |
| 185 | + }[op] |
| 186 | + def evel_binary(op1,oper,op2): |
| 187 | + op1,op2 = int(op1),int(op2) |
| 188 | + return get_operator_fn(oper)(op1,op2) |
| 189 | + speech("your result is ") |
| 190 | + speech(evel_binary(*(my_string.split()))) |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +wish = wishMe() |
| 196 | +print(wish) |
| 197 | +speech(f"{wish} sir, i am jarvis what would you like to do") |
| 198 | + |
| 199 | +while True: |
| 200 | + speechinput= takeCommand().lower() |
| 201 | + #print(speechinput) |
| 202 | + |
| 203 | + |
| 204 | + if 'time' in speechinput : |
| 205 | + dateAndTime() |
| 206 | + |
| 207 | + elif 'google' in speechinput: |
| 208 | + google() |
| 209 | + |
| 210 | + |
| 211 | + elif 'play music' in speechinput: |
| 212 | + playmusic() |
| 213 | + |
| 214 | + elif 'open whatsapp' in speechinput: |
| 215 | + Whatsapp() |
| 216 | + |
| 217 | + elif 'open chrome' in speechinput: |
| 218 | + chrome() |
| 219 | + |
| 220 | + elif 'search' in speechinput:#wikipedia |
| 221 | + search() |
| 222 | + |
| 223 | + elif 'tell me a joke' in speechinput: |
| 224 | + joke() |
| 225 | + |
| 226 | + |
| 227 | + elif 'exit' in speechinput: |
| 228 | + speech("goodbye sir going off duty") |
| 229 | + print("goodbye sir going off duty") |
| 230 | + exit() |
| 231 | + |
| 232 | + elif 'search on web' in speechinput: |
| 233 | + searchOnGoogle() |
| 234 | + |
| 235 | + elif 'what is the weather' in speechinput: |
| 236 | + weather() |
| 237 | + |
| 238 | + elif 'what is your name' in speechinput: |
| 239 | + speech('my name is jarvis your personal voice assistant') |
| 240 | + |
| 241 | + elif'who made you' in speechinput: |
| 242 | + speech('i was made by avyay jain') |
| 243 | + |
| 244 | + elif 'alarm' in speechinput: |
| 245 | + speech("say set alarm for 5:30 am ") |
| 246 | + print("say set alarm for 5:30 am") |
| 247 | + tt = takeCommand() |
| 248 | + tt = tt.replace("set alarm to ","") |
| 249 | + tt = tt.replace(".","") |
| 250 | + tt = tt.upper() |
| 251 | + alarm(tt) |
| 252 | + |
| 253 | + elif'calculate' in speechinput: |
| 254 | + calculate() |
| 255 | + |
| 256 | + |
0 commit comments