Guest User

Untitled

a guest
Jan 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. from gtts import gTTS
  2.  
  3. print(audio)
  4. for line in audio.splitlines():
  5. os.system("say " + audio)
  6.  
  7. # use the system's inbuilt say command instead of mpg123
  8. # text_to_speech = gTTS(text=audio, lang='en')
  9. # text_to_speech.save('audio.mp3')
  10. # os.system('mpg123 audio.mp3')
  11.  
  12. r = sr.Recognizer()
  13.  
  14. with sr.Microphone() as source:
  15. print('Ready...')
  16. r.pause_threshold = 1
  17. r.adjust_for_ambient_noise(source, duration=1)
  18. audio = r.listen(source)
  19.  
  20. try:
  21. command = r.recognize_google(audio).lower()
  22. print('You said: ' + command + 'n')
  23.  
  24. #loop back to continue to listen for commands if unrecognizable speech is received
  25. except sr.UnknownValueError:
  26. print('Your last command couldn't be heard')
  27. command = myCommand();
  28.  
  29. return command
  30.  
  31. if 'open reddit' in command:
  32. reg_ex = re.search('open reddit (.*)', command)
  33. url = 'https://www.reddit.com/'
  34. if reg_ex:
  35. subreddit = reg_ex.group(1)
  36. url = url + 'r/' + subreddit
  37. webbrowser.open(url)
  38. print('Done!')
  39.  
  40. elif 'open website' in command:
  41. reg_ex = re.search('open website (.+)', command)
  42. if reg_ex:
  43. domain = reg_ex.group(1)
  44. url = 'https://www.' + domain
  45. webbrowser.open(url)
  46. print('Done!')
  47. else:
  48. pass
  49.  
  50. elif 'what's up' in command:
  51. talkToMe('Just doing my thing')
  52. elif 'joke' in command:
  53. res = requests.get(
  54. 'https://icanhazdadjoke.com/',
  55. headers={"Accept":"application/json"}
  56. )
  57. if res.status_code == requests.codes.ok:
  58. talkToMe(str(res.json()['joke']))
  59. else:
  60. talkToMe('oops!I ran out of jokes')
  61.  
  62. elif 'current weather in' in command:
  63. reg_ex = re.search('current weather in (.*)', command)
  64. if reg_ex:
  65. city = reg_ex.group(1)
  66. weather = Weather()
  67. location = weather.lookup_by_location(city)
  68. condition = location.condition()
  69. talkToMe('The Current weather in %s is %s The tempeture is %.1f degree' % (city, condition.text(), (int(condition.temp())-32)/1.8))
  70.  
  71. elif 'weather forecast in' in command:
  72. reg_ex = re.search('weather forecast in (.*)', command)
  73. if reg_ex:
  74. city = reg_ex.group(1)
  75. weather = Weather()
  76. location = weather.lookup_by_location(city)
  77. forecasts = location.forecast()
  78. for i in range(0,3):
  79. talkToMe('On %s will it %s. The maximum temperture will be %.1f degree.'
  80. 'The lowest temperature will be %.1f degrees.' % (forecasts[i].date(), forecasts[i].text(), (int(forecasts[i].high())-32)/1.8, (int(forecasts[i].low())-32)/1.8))
  81.  
  82.  
  83. elif 'email' in command:
  84. talkToMe('Who is the recipient?')
  85. recipient = myCommand()
  86.  
  87. if 'John' in recipient:
  88. talkToMe('What should I say?')
  89. content = myCommand()
  90.  
  91. #init gmail SMTP
  92. mail = smtplib.SMTP('smtp.gmail.com', 587)
  93.  
  94. #identify to server
  95. mail.ehlo()
  96.  
  97. #encrypt session
  98. mail.starttls()
  99.  
  100. #login
  101. mail.login('username', 'password')
  102.  
  103. #send message
  104. mail.sendmail('John Fisher', 'JARVIS2.0@protonmail.com', content)
  105.  
  106. #end mail connection
  107. mail.close()
  108.  
  109. talkToMe('Email sent.')
  110.  
  111. else:
  112. talkToMe('I don't know what you mean!')
Add Comment
Please, Sign In to add comment