def main(): """Shows basic usage of the Google Calendar API. Creates a Google Calendar API service object and outputs a list of the next 10 events on the user's calendar. """ credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('calendar', 'v3', http=http) now = datetime.datetime.utcnow().isoformat( ) + 'Z' # 'Z' indicates UTC time # print (colored('\nGetting the upcoming 10 events\n','green')) eventsResult = service.events().list(calendarId='primary', timeMin=now, maxResults=5, singleEvents=True, orderBy='startTime').execute() events = eventsResult.get('items', []) # if there are no upcoming events if not events: print(colored('\nNo upcoming events found.\n', 'red')) for event in events: # loop through events start = event['start'].get('dateTime', event['start'].get('date')) dateTime = event['start'].get('dateTime') if (dateTime == None): dateTime = event['start'].get('date') eventName = event['summary'] print(colored("\n" + dateTime + " :-: " + eventName + "\n", 'green')) ip.play(eventName) playDate(dateTime) # two seconds delay between playing every event time.sleep(2)
def bingRecog(audio): # recognize speech using Microsoft Bing Voice Recognition enterTime = time.time() outText = -1 outNumber = -1 try: json = r.recognize_bing(audio, key="", show_all=True) # check if success is returned or not checkSuccess = (json[u'header'])[u'status'] if (checkSuccess == 'success'): confidence = ((json[u'results'])[0])[u'confidence'] # case when confidence is low if (confidence < 0.75): # error message ip.play("I didn't get it, come again?") print colored("\nDidn't get it, try again!\n", 'red') # crosses only if confidence is high # print("Confidence = "+confidence) outText = ((json[u'results'])[0])[u'lexical'] # outNumber will contain a number in case if one is spoken(eg., one two three) outNumber = ((json[u'results'])[0])[u'name'] # print colored("\nI think you said --- "+outText+"\n",'green') # print colored("\nNumber : "+outNumber+"\n",'green') except: pass # print colored("\nBing Time : "+str((time.time())-enterTime)+"\n",'yellow') return (outText, outNumber) # return string on success otherwise -1
def searchNews(): url = "https://www.google.co.in/search?q=news&source=lnms&tbm=nws&sa=X" # print colored("\n"+url+"\n",'yellow') # web scraping google is forbidden, hence headers are needed as a workaround headers={'User-Agent':user_agent,} request=urllib2.Request(url,None,headers) response = urllib2.urlopen(request) # complete code is saved in "code" code = response.read() # save code file offline as "page_content.html" with open('page_content.html', 'wb') as fid: fid.write(code) # make a soup object from html code soup = BeautifulSoup(code,"html.parser") found = soup.findAll("h3", { "class" : "r" }) if(found==None): print colored("\nSomething went wrong, please try again.\n",'red') ip.play("Something went wrong, please try again.") else: # loop through all headlines, get_text and play text for t in found: text = t.get_text() print colored("\n"+text+"\n",'green') ip.play(text)
def set(hour, minutes, ampm, device): localtime = time.localtime(time.time()) # current time # e.g, 08:21 PM <- line below creates string in this format for alarm time time12hourformat = hour + ":" + minutes + " " + ampm.upper() # convert alarm time from 12hr format to 24hr t2 = (str(datetime.strptime(time12hourformat, '%I:%M %p')).split(" ")[1])[:5] # current time in 24hr format t1 = str(localtime.tm_hour) + ":" + str(localtime.tm_min) FMT = '%H:%M' # <- we just need hours and minutes # difference between both times, in number of days, hours and minutes tdelta = str(datetime.strptime(t2, FMT) - datetime.strptime(t1, FMT)) # below lines are for extracting hours and minutes if (len(tdelta.split(",")) == 2): tdelta = (tdelta.split(",")[1]).strip(" ") parts = tdelta.split(":") # calculate seconds from minutes and hours totalSeconds = (int(parts[0]) * 3600) + (int(parts[1]) * 60) # open a separate terminal which will play the mp3 file after n tdelta seconds # and return to main function if (device == 1): command = "'python /home/piyush/final/setAlarm.py " + str( totalSeconds) + " " + str(hour) + ":" + str(minutes) + str( ampm.upper()) + " " + str(device) + "'" os.system("gnome-terminal --command " + command) else: command = "'python /home/pi/final/setAlarm.py " + str( totalSeconds) + " " + str(hour) + ":" + str(minutes) + str( ampm.upper()) + " " + str(device) + "'" os.system("lxterminal --command " + command) print colored("\nAlarm set!\n", 'green') ip.play("Alarm set!")
def sphinxRecog(audio): # recognize speech using Sphinx enterTime = time.time() outText = -1 try: outText = r.recognize_sphinx(audio) # print colored("\nI think you said --- " + outText + "\n",'green') except sr.UnknownValueError: ip.play("I didn't get it, come again?") print colored("\nDidn't get it, try again!\n", 'red') except sr.RequestError as e: # print("Sphinx error; {0}".format(e)) ip.play("Please try again") print colored("\nDidn't get it, try again!\n", 'red') # print colored("\nSphinx Time : "+str((time.time())-enterTime)+"\n",'yellow') return outText
def shuffle(device): try: if(device==1): os.chdir('/home/piyush/ied/music') else: os.chdir('/home/pi/ied/music') # store file names in a python list with open("out.txt") as f: content = f.readlines() content = [x.strip() for x in content] check = [] # play random tracks from all tracks found using random set for i in range(len(content)): # print(i) while(1): r = random.randint(0,len(content)-1) if(r not in check): check.append(r) break # redirect console output to null file # command = "mplayer "+content[r]+" > /dev/null 2>&1" print colored("\nPlaying "+content[r]+"..\n",'green') if(device==1): command = "gnome-terminal --command='mplayer "+content[r]+"'" else: command = "lxterminal --command='mplayer "+content[r]+"'" os.system(command) # shift control to background music controls if(musicShuffleControlFunc(device)==False): break return True except: # in case shuffle encounters some errors print colored("\nShuffle failed. Please try again!\n",'red') ip.play("Shuffle failed. Please try again!") return False
def playDate(s): # if time is present in event if (len(s) > 10): # play month d = list(s[0:10].split("-")) play = d[2] + "th " + returnMonth(d[1]) ip.play(play) # play time time = s[11:16] play = time + " hours" ip.play(play) # if only date is present else: d = list(s.split("-")) play = d[2] + "th " + returnMonth(d[1]) ip.play(play)
# phrases often recognized when saying victoria triggerList = [ "victoria", "victorious", "pictorial", "victoriya", "call victoria", "pretoria", "vicktoria" ] while (True): print colored("\nSay 'Victoria' to wake me up!\n", 'yellow') audio = ip.recordAudio(device) if (cr.isConnected()): if (device == 1): outText = cr.googleRecog(audio) else: outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if (outText == -1): # if text is not recognized, continue listening continue elif (outText in ["exit", "Exit", "exact", "Exact"]): # exit the whole script print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") break else: # if Victoria is said, proceed to phraseRecog function if (outText.lower() in triggerList): pr.phraseRecog(device) elif ("victoria" in outText.lower()): pr.phraseRecog(device)
def youtube(textToSearch,flag,device): # flag=0 if link is available, flag=1 if text is to be searched if(flag==0): play(textToSearch,device) elif(flag==1): playLink(textToSearch,device) # videoPlaying is a flag to keep track if the video is playing videoPlaying = True # countyt keeps track of browser PIDs running in background # countyt=1 when the browser is not running countyt = 0 while(countyt!=1): if(countyt==0): # <- runs only first time # wait for 5 seconds, so that video starts playing time.sleep(15) # then press F to enter full-screen mode ip.keypress(fullScreen_sequence) countyt = 0 # read how many lines are there with "browser" in background running # around 7-8 when browser is running if(device==1): p = os.popen("ps ux | grep chrome","r") else: p = os.popen("ps ux | grep chromium","r") while(True): line = (p.readline()).strip() if not line: break countyt+=1 # at this point, video is playing in browser in fullscreen mode # program is listening in background print colored("\nSay 'Victoria' access video controls!\n",'green') audio = ip.recordAudio(device); if(cr.isConnected()): if(device==1): outText = cr.googleRecog(audio) else: outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) # if text is not understood, continue listening if(outText==-1): continue # if text is understood else: # check if victoria is said if((outText.lower() in triggerList)or("victoria" in outText.lower())): # enters if text was victoria if(videoPlaying==True): # pause the video ip.keypress(pause_sequence) videoPlaying = False ip.play("Which control sequence?") while(1): print colored("\nOptions available :",'yellow') print colored(videoControls,'yellow') print("\n") # record which video control has to be accessed audio = ip.recordAudio(device) if(cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) # if above text is not recognized, ask again if(outText==-1): print colored("\nNot a control, try again!\n",'red') ip.play("Not a control, try again!") elif(outText.lower() in videoControls): # enter if text is a videoControl # convert text to lower for easier comparison text = outText.lower() if(text=="pause"): break # close the browser window as stop sequence elif(text=="stop"): ip.keypress(stop_sequence) time.sleep(1) return True elif(text=="play"): if(videoPlaying!=True): ip.keypress(pause_sequence) videoPlaying = True break else: print colored("\nVideo is already playing!\n",'red') break # for volume controls, press the concerned key 3 times # each with 1 second pause elif(text=="volume up"): for i in range(3): ip.keypress(volume_up_sequence) time.sleep(1) ip.keypress(pause_sequence) videoPlaying = True break elif(text=="volume down"): for i in range(3): ip.keypress(volume_down_sequence) time.sleep(1) ip.keypress(pause_sequence) videoPlaying = True break # if text is "exit", play video and stop listening elif(outText in ["exit","Exit","exact","Exact"]): print colored("\nExiting..\n",'red') ip.play("Exiting") ip.keypress(pause_sequence) videoPlaying = True break # if text is recognized but it is not victoria, continue listening else: continue return
def mail(receiver_email, device): smtpObj = smtplib.SMTP('smtp.gmail.com', 587) # ping the smtp server smtpObj.ehlo() # start a ssl connection smtpObj.starttls() # sender email and password sender_email = '' account_password = '' # try to login, a frequent error occurs here try: smtpObj.login(sender_email, account_password) except: print colored("\nLogin Error!\n", 'red') ip.play("Login error!") return while (True): print colored("\nRecording subject of the mail..\n", 'yellow') ip.play("Recording subject of the mail..") audio = ip.recordAudio(device) if (cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if (outText == -1): # if text is not recognized, continue listening continue elif (outText in ["exit", "Exit", "exact", "Exact"]): # exit the whole script print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") smtpObj.quit() return else: print colored("\nSubject is, " + outText + ". Is this correct?\n", 'green') ip.play("Subject is, " + outText + ". Is this correct?") audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) if (consent.lower() in ["yes", "yah", "ya", "yup", "yes please"]): SUBJECT = outText break else: continue while (True): print colored("\nRecording body of the mail..\n", 'yellow') ip.play("Recording body of the mail..") audio = ip.recordAudio(device) if (cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if (outText == -1): # if text is not recognized, continue listening continue elif (outText in ["exit", "Exit", "exact", "Exact"]): # exit the whole script print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") smtpObj.quit() return else: print colored("\nBody is, " + outText + ". Is this correct?\n", 'green') ip.play("Body is, " + outText + ". Is this correct?") audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) if (consent.lower() in ["yes", "yah", "ya", "yup", "yes please"]): TEXT = outText break else: continue # create message object message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT) try: # send mail smtpObj.sendmail(sender_email, receiver_email, message) print colored("\nSuccessfully sent email\n", 'green') except: print colored("\nEmail not sent\n", 'red') # object needs to be closed smtpObj.quit() return
def mailDriver(device): while (True): print colored("\nTell me receiver's name..\n", 'yellow') ip.play("Tell me receiver's name..") audio = ip.recordAudio(device) if (cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if (outText == -1): # if text is not recognized, continue listening continue elif (outText in ["exit", "Exit", "exact", "Exact"]): # exit the whole script print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return else: print colored("\nShould I serch for " + outText + ". Yes or No?\n", 'green') ip.play("Should I search for " + outText + ". Yes or No?") consent = -1 while (consent == -1): audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) if (consent == -1): print colored("Didn't get it, recording again!", 'red') ip.play("Didn't get it, recording again!") if (consent.lower() in ["yes", "yah", "ya", "yup", "yes please"]): name = outText.lower() if (device == 1): os.chdir('/home/piyush/final') else: os.chdir('/home/pi/final') # open contacts list f = open("contacts.txt", "r") nameFound = False while (True): line = (f.readline()).strip() if not line: break # match every name with recorded name if (line.startswith(name)): # contacts is a txt file with csvs : name,email contactList = line.split(",") print colored( "\nShould I send it to " + contactList[0] + "?\n", 'yellow') ip.play("Should I send it to " + contactList[0] + "?") # record consent for name consent = -1 while (consent == -1): audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) if (consent == -1): print colored( "Didn't get it, recording again!", 'red') ip.play("Didn't get it, recording again!") # if name is matched if (consent.lower() in ["yes", "yah", "ya", "yup", "yes please"]): nameFound = True # extract email email = line.split(",")[1] break else: continue if (nameFound == True): mail(email, device) return else: print colored("\nNo one with such name found!\n", 'red') ip.play("No one with such name found!") continue else: continue
def search(inp,device): # below lines generate a google search url from search text pieces = inp.split(" ") query = "" for k in range(len(pieces)): pieces[k] = pieces[k].lower() if(k!=len(pieces)-1): query += pieces[k]+"+" else: query += pieces[k] url = "https://www.google.co.in/search?site=&source=hp&q=" url += query # print colored("\n"+url+"\n",'yellow') # web scraping google is forbidden, hence headers are needed as a workaround headers={'User-Agent':user_agent,} request=urllib2.Request(url,None,headers) response = urllib2.urlopen(request) # complete code is saved in "code" code = response.read() # save code file offline as "page_content.html" with open('page_content.html', 'wb') as fid: fid.write(code) # make a soup object from html code soup = BeautifulSoup(code,"html.parser") # found will be None if the specified tags are not found in code found = soup.find("div", { "class" : "_tXc" }) if(found==None): found = soup.find("table", { "style" : "font-size:14px;width:100%" }) if(found!=None): # delete the prefix noun/pronoun/verb/adjective toDelete = soup.find("div", { "style" : "color:#666;padding:5px 0" }).get_text() output = found.get_text() output = output[len(toDelete):] print colored("\n"+output+"\n",'yellow') ip.play(output) else: found = soup.find("div", { "class" : "_o0d" }) if(found!=None): output = found.get_text() # if the top result is a youtube video, play it using youtube function if(output.find("https://www.youtube.com/watch?v=")!=-1): link = output[output.find("https"):] ip.play("Playing youtube video..") print colored("\nPlaying youtube video..\n",'green') you.youtube(link,1,device) else: print colored("\n"+output+"\n",'yellow') ip.play(output) else: # no straightaway answer from google, only webpages available print colored("\nWill mail the link to you.\n",'green') ip.play("Will mail the link to you.") else: output = found.get_text() print colored("\n"+output+"\n",'yellow') ip.play(output) # remove the html code once function finishes os.system("rm page_content.html")
def phraseRecog(device): print colored("\nTell me the phrase word!\n", 'green') ip.play("Tell me the phrase word!") while (True): print colored("\nPhraseList : ", 'yellow') print colored(phrases, 'yellow') print("\n") sleep(2) # listen to the phrase word audio = ip.recordAudio(device) if (cr.isConnected()): if (device == 1): outText = cr.googleRecog(audio) else: outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) # if voice not recognized if (outText == -1): print colored("\nDidn't get it, please try again\n", 'red') ip.play("Didn't get it, please try again") # if user says exit, return from the main function elif (outText in ["exit", "Exit"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # if voice is recognized and it is not "exit" else: # convert to lower letters for easier comparison outText = outText.lower() # if word is a phrase word if (outText in phraseList): # if movie is said if (outText in ["movie", "movies"]): print colored("\nWhich movie to play?\n", 'green') ip.play("Which movie to play?") while (True): # print all movie names by printing out.txt print colored("Possible Options :", 'green') if (device == 1): os.chdir('/home/piyush/ied/movies') else: os.chdir('/home/pi/ied/movies') os.system('cat out.txt') print("\n") # record movie name audio = ip.recordAudio(device) if (cr.isConnected()): movieName = cr.bingRecog(audio)[0] else: movieName = cr.sphinxRecog(audio) # if user says exit, exit main function if (movieName in ["exit", "Exit", "exact", "Exact"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # po.playMovie(movieName) returns false if movie doesn't exist # plays the movie and returns true if movie exists if (po.playMovie(movieName, device)): return else: # if po.playMovie(movieName) returns false, give error message print colored( "\nNo movie with such name exists! Try again.\n", 'red') ip.play( "No movie with such name exists! Try again.") # if music is said elif (outText in ["music"]): # ask if user wants to play specific track or shuffle all the avail. tracks print colored("\nTrack or shuffle\n", 'green') ip.play("Track or shuffle") while (True): # record tracks or shuffle audio = ip.recordAudio(device) if (cr.isConnected()): musicOption = cr.bingRecog(audio)[0] else: musicOption = cr.sphinxRecog(audio) # if user says exit, return the main function if (musicOption in ["exit", "Exit", "exact", "Exact"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # if track is said if (musicOption in ["track", "Track"]): print colored("Which track to play?", 'green') ip.play("Which track to play?") while (True): # print all the possible options by printing out.txt print colored("\nPossible Options :\n", 'green') if (device == 1): os.chdir('/home/piyush/ied/music') else: os.chdir('/home/pi/ied/music') os.system('cat out.txt') print("\n") # record track name audio = ip.recordAudio(device) if (cr.isConnected()): musicName = cr.bingRecog(audio)[0] else: musicName = cr.sphinxRecog(audio) # if exit is said, exit the main function if (musicName in ["exit", "Exit", "exact", "Exact"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # po.playTrack(musicName) returns false if track doesn't exist # plays the track and returns true if track exists if (po.playTrack(musicName, device)): return else: # if po.playMovie(movieName) returns false, give error message print colored( "\nNo track with such name exists! Try again.\n", 'red') ip.play( "No track with such name exists! Try again." ) # if user wants to shuffle tracks elif (musicOption in ["shuffle", "Shuffle"]): po.shuffle(device) return # if user says something instead of track or shuffle else: print colored("\nWrong Option! Try again.\n", 'red') ip.play("Wrong Option! Try again.") # if photos is said # works only for albums stored as folders elif (outText in ["photos", "photo"]): print colored("Which album to play?", 'green') ip.play("Which album to play?") while (True): # show all albums stored print colored("\nPossible Options :", 'green') if (device == 1): os.chdir('/home/piyush/ied/photos') else: os.chdir('/home/pi/ied/photos') os.system('cat out.txt') # recored the album name audio = ip.recordAudio(device) if (cr.isConnected()): albumName = cr.bingRecog(audio)[0] else: albumName = cr.sphinxRecog(audio) # if user says exit, exit the main function if (albumName in ["exit", "Exit", "exact", "Exact"]): ip.play("Ending Session") print colored("\nEnding Session..\n", 'red') return # po.playPhotos(albumName) returns false if album doesn't exist # plays the album and returns true if album exists if (po.playPhotos(albumName, device)): return else: # if po.playPhotos(albumName) returns false, give error message print colored( "\nNo album with such name exists! Try again.\n", 'red') ip.play( "No album with such name exists! Try again.") # to access calender # currently programmed to only show upcoming 5 events # creating a new event functionality is implemented # but not used due to the recognition constraints # future scope : add insertion and deletion of events elif (outText == "schedule"): print colored("\nUpcoming events are..\n", 'green') ip.play("Upcoming events are") calender.main() return # if alarm is said # sets an alarm for the first occurrence of the recorded time # this won't work offline # future scope : tell the date and set an alarm for a particular day elif (outText == "alarm"): print colored("\nTell me the time?\n", 'green') ip.play("Tell me the time?") while (True): # record alarm time audio = ip.recordAudio(device) if (cr.isConnected()): (query, time) = cr.bingRecog(audio) else: print colored("\nNot connected to the internet.\n", 'red') return # if user says exit, exit the main function if (query in ["exit", "Exit", "exact", "Exact"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # if text is not understood, record again elif (query == -1): print("\nWhat did you say?\n", 'red') ip.play("What did you say?") continue # if something else is recognized else: # time -> 8 21 PM # query -> eight twenty one PM # extract last two words ampm = query[len(query) - 2:] # check if they are am or pm # if not record again if (ampm.lower() not in ["am", "pm"]): print colored( "\nWrong format! Recording again..\n", 'red') ip.play("Wrong format! Recording again..") continue # remove last two letters from time and query time = time[:len(time) - 2] query = query[:len(query) - 2] splitQuery = query.split(" ") # case because 1 is often recognised as vine if (splitQuery[0] == "vine"): hour = "1" time = time[4:] # case because 2 is often recognised as do elif (splitQuery[0] == "do"): hour = "2" time = time[2:] elif (splitQuery[0] in ["ten", "eleven", "twelve"]): hour = time[:2] time = time[2:] else: hour = time[:1] time = time[1:] if (time.startswith(":")): time = time[1:] minutes = time.strip(" ") # if minutes is empty if (minutes in [None, " ", ""]): minutes = "0" try: # check if "hour" and "minutes" contains numbers or not int(hour) int(minutes) except: print colored( "\nWrong format! Recording again..\n", 'red') ip.play("Wrong format! Recording again..") continue # confirm the alarm details print colored( "\nShould I set an alarm for " + hour + " " + minutes + " " + ampm + "? Yes or No?\n", 'green') ip.play("Should I set an alarm for " + hour + " " + minutes + " " + ampm + "? Yes or No?") while (True): # record consent audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) consent = str(consent).lower() if (consent in [ "yes", "yah", "ya", "yup", "yes please" ]): # on confirming set alarm and exit the main function alarm.set(hour, minutes, ampm, device) return else: # record time again if user says no print colored("\nRecording time again..\n", 'green') ip.play("Recording time again") break # if user wants to perform a google search elif (outText == "search"): print colored("\nWhat should I search for?\n", 'green') ip.play("What should I search for?") while (True): # record search query audio = ip.recordAudio(device) if (cr.isConnected()): query = cr.bingRecog(audio)[0] else: query = cr.sphinxRecog(audio) # exit the main function if (query in ["exit", "Exit", "exact", "Exact"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # if voice is not understood elif (query == -1): print colored("\nWhat did you say?\n", 'red') ip.play("What did you say?") continue # if voice is recognized else: # confirm the search query print colored( "\nShould I search for " + query + "? Yes or No?\n", 'green') ip.play("Should I search for " + query + "? Yes or No?") while (True): # record consent audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) try: consent = consent.lower() except: print colored("\nPardon?\n", 'red') ip.play("Pardon?") continue if (consent in [ "yes", "yah", "ya", "yup", "yes please" ]): # perform the google search and exit the main fucntion google.search(query, device) return else: # if the user says no to the recognized text print colored( "\nRecording search query again..\n", 'green') ip.play("Recording search query again") break # if user wants news elif (outText == "news"): google.searchNews() return elif (outText in ["mail", "male", "man", "men"]): mail.mailDriver(device) return # if user wants to watch a youtube video elif (outText == "youtube"): print colored("\nWhich video should I search for?\n", 'green') ip.play("Which video should I search for?") while (True): # record video name audio = ip.recordAudio(device) if (cr.isConnected()): videoName = cr.bingRecog(audio)[0] else: videoName = cr.sphinxRecog(audio) # exit the main function if (videoName in ["exit", "Exit", "exact", "Exact"]): print colored("\nEnding Session..\n", 'red') ip.play("Ending Session") return # if text is not understood elif (videoName == -1): print colored("\nWhat did you say?\n", 'red') ip.play("What did you say?") continue # if text is understood else: # confirm the video name print colored( "\nShould I search for " + videoName + "? Yes or No?\n", 'green') ip.play("Should I search for " + videoName + "? Yes or No?") while (True): # record confirmation audio = ip.recordAudio(device) if (cr.isConnected()): consent = cr.bingRecog(audio)[0] else: consent = cr.sphinxRecog(audio) consent = consent.lower() if (consent in [ "yes", "yah", "ya", "yup", "yes please" ]): # play the youtube video and exit main function you.youtube(videoName, 0, device) return else: # if user says no to recognized text, record again print colored( "\nRecording the video name again..\n", 'green') ip.play("Recording the video name again") break # if the word recognied is not a phrase word else: print colored( "\n" + outText + " is not a phrase, try again.\n", 'red') ip.play(outText + " is not a phrase, try again.")
def playMovie(name,device): if(device==1): os.chdir('/home/piyush/ied/movies') else: os.chdir('/home/pi/ied/movies') # store file names in a python list with open("out.txt") as f: content = f.readlines() content = [x.strip() for x in content] name = str(name) # concatenate the extension for matching nameList = [name+".mkv",name+".avi",name+".mp4"] flag = False # check if movie exists for i in nameList: for j in content: if(i==j): play = i flag = True break # return false and exit if movie name not found if(flag==False): return False else: # play the movie using mplayer if movie is found mCommand = "'"+"mplayer -fs "+play+"'" if(device==1): command = "gnome-terminal --command "+mCommand else: command = "lxterminal --command "+mCommand os.system(command) moviePlaying = True count = 0 time.sleep(10) while(count!=2): p = os.popen("ps ux | grep mplayer","r") count = 0 while(True): line = (p.readline()).strip() if not line: break count+=1 print colored("\nSay 'Victoria' access movie controls!\n",'green') audio = ip.recordAudio(device); if(cr.isConnected()): if(device==1): outText = cr.googleRecog(audio) else: outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if(outText==-1): continue else: if((outText.lower() in triggerList)or("victoria" in outText.lower())): if(moviePlaying==True): ip.keypress(pause_sequence) moviePlaying = False ip.play("Which control sequence?") while(1): print colored("\nOptions available :\n",'yellow') print colored(movieControls,'yellow') print("\n") audio = ip.recordAudio(device) if(cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if(outText==-1): print colored("\nNot a control, try again!\n",'red') ip.play("Not a control, try again!") elif(outText.lower() in movieControls): text = outText.lower() if(text=="pause"): break elif(text=="stop"): ip.keypress(stop_sequence) time.sleep(1) return True elif(text=="play"): if(moviePlaying!=True): ip.keypress(pause_sequence) moviePlaying = True break else: print colored("\nMovie is already playing!\n",'red') break elif(text=="volume up"): for i in range(3): ip.keypress(volume_up_sequence) time.sleep(1) if(device!=1): ip.keypress(pause_sequence) moviePlaying = True break elif(text=="volume down"): for i in range(3): ip.keypress(volume_down_sequence) time.sleep(1) if(device!=1): ip.keypress(pause_sequence) moviePlaying = True break elif(outText in ["exit","Exit","exact","Exact"]): print colored("\nExiting..\n",'red') ip.play("Exiting") ip.keypress(pause_sequence) moviePlaying = True break return True
def playPhotos(name,device): if(device==1): os.chdir('/home/piyush/ied/photos') else: os.chdir('/home/pi/ied/photos') with open("out.txt") as f: content = f.readlines() content = [x.strip() for x in content] flag = False # search for album name for i in content: if(i == name): play = i flag = True break # return False and exit if album doesn't exist if(flag==False): return False else: # play using eog fullscreen/slideshow command command = "eog "+play+" -s" if(device==1): command1 = "gnome-terminal --command='"+command+"'" else: command1 = "lxterminal --command='"+command+"'" os.system(command1) photosPlaying = True count = 0 # wait 5 seconds for the slideshow to startup time.sleep(5) # no. of processes with keyword 'eog' when eog not playing = 1 while(count!=1): p = os.popen("ps ux | grep eog","r") count = 0 while(True): line = (p.readline()).strip() if not line: break count+=1 print colored("\nSay 'Victoria' access photo controls!\n",'green') audio = ip.recordAudio(device); if(cr.isConnected()): if(device==1): outText = cr.googleRecog(audio) else: outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if(outText==-1): continue else: if((outText.lower() in triggerList)or("victoria" in outText.lower())): if(photosPlaying==True): ip.keypress(pause_sequence) photosPlaying = False ip.play("Which control sequence?") while(1): print colored("\nOptions available :\n",'yellow') print colored(photoControls,'yellow') print("\n") audio = ip.recordAudio(device) if(cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if(outText==-1): print colored("\nNot a control, try again!\n",'red') ip.play("Not a control, try again!") elif(outText.lower() in photoControls): text = outText.lower() if(text=="pause"): break elif(text=="stop"): # stop by killing using PID k = os.popen("ps ux | grep 'eog "+play+" -s'","r") k = (k.readline()).strip() # print colored("\n"+k+"\n",'yellow') pid = (k.split())[1] command2 = "kill -9 "+pid os.system(command2) time.sleep(2) return True elif(text=="play"): if(photosPlaying!=True): ip.keypress(pause_sequence) photosPlaying = True break else: print colored("\nAlbum is already playing!\n",'red') break elif(outText in ["exit","Exit","exact","Exact"]): print colored("\nExiting..\n",'red') ip.play("Exiting") ip.keypress(pause_sequence) photosPlaying = True break return True
def musicControlFunc(device): musicPlaying = True count = 0 time.sleep(10) while(count!=2): p = os.popen("ps ux | grep mplayer","r") count = 0 while(True): line = (p.readline()).strip() if not line: break count+=1 print colored("\nSay 'Victoria' access music controls!\n",'green') audio = ip.recordAudio(device); if(cr.isConnected()): if(device==1): outText = cr.googleRecog(audio) else: outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if(outText==-1): continue else: if((outText.lower() in triggerList)or("victoria" in outText.lower())): if(musicPlaying==True): ip.keypress(pause_sequence) musicPlaying = False while(1): print colored("\nOptions available :\n",'yellow') print colored(movieControls,'yellow') print("\n") ip.play("Which control sequence?") audio = ip.recordAudio(device) if(cr.isConnected()): outText = cr.bingRecog(audio)[0] else: outText = cr.sphinxRecog(audio) if(outText==-1): print colored("\nNot a control, try again!\n",'red') ip.play("Not a control, try again!") elif(outText.lower() in movieControls): text = outText.lower() if(text=="pause"): break elif(text=="stop"): ip.keypress(stop_sequence) time.sleep(1) return True elif(text=="play"): if(musicPlaying!=True): ip.keypress(pause_sequence) musicPlaying = True break else: print colored("\nMusic is already playing!\n",'red') break elif(text=="volume up"): for i in range(3): ip.keypress(volume_up_sequence) time.sleep(1) if(device!=1): ip.keypress(pause_sequence) musicPlaying = True break elif(text=="volume down"): for i in range(3): ip.keypress(volume_down_sequence) time.sleep(1) if(device!=1): ip.keypress(pause_sequence) musicPlaying = True break elif(outText in ["exit","Exit","exact","Exact"]): print colored("\nExiting..\n",'red') ip.play("Exiting") ip.keypress(pause_sequence) musicPlaying = True break return True