import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import smtplib



engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')

engine.setProperty('voice', voices[0].id)


# To change the voice to female change 0 to 1.


def speak(audio):
	engine.say(audio)
	engine.runAndWait()
	pass


def take_command():
	"""
	It takes microphone input from the user and returns a  string

	:return:
	"""
	r = sr.Recognizer()
	with sr.Microphone() as  source:
		print("Listening...")
		r.pause_threshold = 1.5  # It will wait 1.5 seconds to complete a sentence
		audio = r.listen(source)
		#Do read details
	try:
		print("Recognizing")
		query = r.recognize_google(audio,language='en-in')
		print(f'user said : {query}\n')

	except Exception as e:
		#print(e)
		print("Say that again please")
		return "None"
	return query
def sendEmail(to,content):
	server =smtplib.SMTP('smtp.gmail.com',28)
	# server.connect("smtp.gmail.com",465)
	# server.ehlo()
	server.login('jayeshvijayesh@gmail.com','########')
	server.sendmail('jayeshvijayesh@gmail.com',to,content)
	server.close()
def wish_me():
	hour = int(datetime.datetime.now().hour)
	if hour >= 0 and hour < 12:
		speak("Good morning")
	elif hour >= 12 and hour < 18:
		speak("Good afternoon")

	else:
		speak("Good night")
	speak("I am JARVIS how can i  help you")


if __name__ == '__main__':
	wish_me()
	while True:
		query =take_command().lower()
		if 'wikipedia' in query:
			speak("Searching wikipedia")
			query = query.replace('wikipedia','')
			results = wikipedia.summary(query,sentences=2)#To read more increase sentence to decrease sentence decreease sentence
			speak("According to wikipedia")
			#print(results)
			speak(results)
		elif  'open youtube' in  query:
			# webbrowser.Chrome.open_new("youtube.com")
			webbrowser.open("youtube.com")
		elif "open google" in query:
			webbrowser.open("google.com")


		elif "play music" in query:
			music_dir = "D:\\vijayesh\\music"
			songs = os.listdir(music_dir)
			print(songs)
			os.startfile(os.path.join(music_dir,songs[1]))

		elif "the time" in query:
			strtime = datetime.datetime.now().strftime("%H:%M:%S")
			speak(f"The time is {strtime}")
		elif " open pycharm" in query:
			pycharmpath ="C:\\Program Files\\JetBrains\\PyCharm Community Edition 2021"
			os.startfile(pycharmpath)
		#elif "open command" in query:
			# filelocation = "path of the particular file like above"
			# os.startfile(filelocation)
		elif " email to vijayesh" or "email to vijesh" in query:
			try:
				speak("What should i say")#error present
				content = take_command()
				to = "jayeshvijayesh@gmail.com"
				sendEmail(to,content)
				speak("Email has been sent")
				exit()
			except Exception as e:
				print(e)
				speak("Sorry,I am not able to send this email")
				exit()
				

[((11, 9, 11, 30), 'pyttsx3.init', 'pyttsx3.init', ({(11, 22, 11, 29): '"""sapi5"""'}, {}), "('sapi5')", False, 'import pyttsx3\n'), ((32, 5, 32, 20), 'speech_recognition.Recognizer', 'sr.Recognizer', ({}, {}), '()', True, 'import speech_recognition as sr\n'), ((49, 9, 49, 42), 'smtplib.SMTP', 'smtplib.SMTP', ({(49, 22, 49, 38): '"""smtp.gmail.com"""', (49, 39, 49, 41): '28'}, {}), "('smtp.gmail.com', 28)", False, 'import smtplib\n'), ((33, 6, 33, 21), 'speech_recognition.Microphone', 'sr.Microphone', ({}, {}), '()', True, 'import speech_recognition as sr\n'), ((56, 12, 56, 35), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((74, 13, 74, 49), 'wikipedia.summary', 'wikipedia.summary', (), '', False, 'import wikipedia\n'), ((80, 3, 80, 33), 'webbrowser.open', 'webbrowser.open', ({(80, 19, 80, 32): '"""youtube.com"""'}, {}), "('youtube.com')", False, 'import webbrowser\n'), ((82, 3, 82, 32), 'webbrowser.open', 'webbrowser.open', ({(82, 19, 82, 31): '"""google.com"""'}, {}), "('google.com')", False, 'import webbrowser\n'), ((87, 11, 87, 32), 'os.listdir', 'os.listdir', ({(87, 22, 87, 31): 'music_dir'}, {}), '(music_dir)', False, 'import os\n'), ((89, 16, 89, 48), 'os.path.join', 'os.path.join', ({(89, 29, 89, 38): 'music_dir', (89, 39, 89, 47): 'songs[1]'}, {}), '(music_dir, songs[1])', False, 'import os\n'), ((96, 3, 96, 28), 'os.startfile', 'os.startfile', ({(96, 16, 96, 27): 'pycharmpath'}, {}), '(pycharmpath)', False, 'import os\n'), ((92, 13, 92, 36), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n')]