Skip to content

Commit

Permalink
Created structure for project.
Browse files Browse the repository at this point in the history
  • Loading branch information
notsag committed Sep 3, 2019
1 parent c7e5b20 commit c1facde
Show file tree
Hide file tree
Showing 2,157 changed files with 172 additions and 412,018 deletions.
79 changes: 79 additions & 0 deletions .gitignore
@@ -0,0 +1,79 @@
#DS store
*.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# pyenv
.python-version

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# pycharm
.idea

# vim
*.swp

# mp3
*.mp3
13 changes: 13 additions & 0 deletions README.md
@@ -0,0 +1,13 @@
# TARS

TARS can help you to automate your tasks such as search videos in YouTube and play them, send emails, open websites, search materials in Wikipedia and read them,inform weather forecast in your country, greetings and more.

## Getting Started

TARS works on **Python 3+**.

Install dependencies:

```
python3 -m pip install -r requirements.txt
```
5 changes: 5 additions & 0 deletions requirements.txt
@@ -0,0 +1,5 @@
gTTS
SpeechRecognition
PyAudio
pygame
selenium
152 changes: 75 additions & 77 deletions tars.py
Expand Up @@ -12,158 +12,156 @@
import urllib.request
import urllib.parse
import bs4


def talk(audio):
"speaks audio passed as argument"

print(audio)
for line in audio.splitlines():
text_to_speech = gTTS(text=audio, lang='en-uk')
text_to_speech.save('audio.mp3')
text_to_speech = gTTS(text=audio, lang="en-uk")
text_to_speech.save("audio.mp3")
mixer.init()
mixer.music.load("audio.mp3")
mixer.music.play()


def myCommand():
"listens for commands"
#Initialize the recognizer
#The primary purpose of a Recognizer instance is, of course, to recognize speech.
# Initialize the recognizer
# The primary purpose of a Recognizer instance is, of course, to recognize speech.
r = sr.Recognizer()

with sr.Microphone() as source:
print('TARS is Ready...')
print("TARS is Ready...")
r.pause_threshold = 1
#wait for a second to let the recognizer adjust the
#energy threshold based on the surrounding noise level
# wait for a second to let the recognizer adjust the
# energy threshold based on the surrounding noise level
r.adjust_for_ambient_noise(source, duration=1)
#listens for the user's input
# listens for the user's input
audio = r.listen(source)
print('analyzing...')
print("analyzing...")

try:
command = r.recognize_google(audio).lower()
print('You said: ' + command + '\n')
print("You said: " + command + "\n")
time.sleep(2)

#loop back to continue to listen for commands if unrecognizable speech is received
# loop back to continue to listen for commands if unrecognizable speech is received
except sr.UnknownValueError:
print('Your last command couldn\'t be heard')
command = myCommand();
print("Your last command couldn't be heard")
command = myCommand()

return command


def tars(command):
errors=[
"I don't know what you mean",
"Excuse me?",
"Can you repeat it please?",
]
errors = ["I don't know what you mean", "Excuse me?", "Can you repeat it please?"]
"if statements for executing commands"

# Search on Google
if 'open google and search' in command:
reg_ex = re.search('open google and search (.*)', command)
search_for = command.split("search",1)[1]
if "open google and search" in command:
reg_ex = re.search("open google and search (.*)", command)
search_for = command.split("search", 1)[1]
print(search_for)
url = 'https://www.google.com/'
url = "https://www.google.com/"
if reg_ex:
subgoogle = reg_ex.group(1)
url = url + 'r/' + subgoogle
talk('Okay!')
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
driver.get('http://www.google.com')
search = driver.find_element_by_name('q')
url = url + "r/" + subgoogle
talk("Okay!")
driver = webdriver.Firefox(executable_path="/path/to/geckodriver")
driver.get("http://www.google.com")
search = driver.find_element_by_name("q")
search.send_keys(str(search_for))
search.send_keys(Keys.RETURN) # hit return after you enter search text
search.send_keys(Keys.RETURN) # hit return after you enter search text

#Send Email
elif 'email' in command:
talk('What is the subject?')
# Send Email
elif "email" in command:
talk("What is the subject?")
time.sleep(3)
subject = myCommand()
talk('What should I say?')
talk("What should I say?")
message = myCommand()
content = 'Subject: {}\n\n{}'.format(subject, message)
content = "Subject: {}\n\n{}".format(subject, message)

#init gmail SMTP
mail = smtplib.SMTP('smtp.gmail.com', 587)
# init gmail SMTP
mail = smtplib.SMTP("smtp.gmail.com", 587)

#identify to server
# identify to server
mail.ehlo()

#encrypt session
# encrypt session
mail.starttls()

#login
mail.login('username_gmail', 'password_gmail')
# login
mail.login("username_gmail", "password_gmail")

#send message
mail.sendmail('FROM', 'TO', content)
# send message
mail.sendmail("FROM", "TO", content)

#end mail connection
# end mail connection
mail.close()

talk('Email sent.')
talk("Email sent.")

# search in wikipedia (e.g. Can you search in wikipedia apples)
elif 'wikipedia' in command:
reg_ex = re.search('wikipedia (.+)', command)
if reg_ex:
query = command.split("wikipedia",1)[1]
elif "wikipedia" in command:
reg_ex = re.search("wikipedia (.+)", command)
if reg_ex:
query = command.split("wikipedia", 1)[1]
response = requests.get("https://en.wikipedia.org/wiki/" + query)
if response is not None:
html = bs4.BeautifulSoup(response.text, 'html.parser')
html = bs4.BeautifulSoup(response.text, "html.parser")
title = html.select("#firstHeading")[0].text
paragraphs = html.select("p")
for para in paragraphs:
print (para.text)
intro = '\n'.join([ para.text for para in paragraphs[0:3]])
print (intro)
mp3name = 'speech.mp3'
language = 'en'
myobj = gTTS(text=intro, lang=language, slow=False)
print(para.text)
intro = "\n".join([para.text for para in paragraphs[0:3]])
print(intro)
mp3name = "speech.mp3"
language = "en"
myobj = gTTS(text=intro, lang=language, slow=False)
myobj.save(mp3name)
mixer.init()
mixer.music.load("speech.mp3")
while mixer.music.play()
elif 'stop' in command:
mixer.music.play()
elif "stop" in command:
mixer.music.stop()

# Search videos on Youtube and play (e.g. Search in youtube believer)
elif 'youtube' in command:
talk('Ok!')
reg_ex = re.search('youtube (.+)', command)
elif "youtube" in command:
talk("Ok!")
reg_ex = re.search("youtube (.+)", command)
if reg_ex:
domain = command.split("youtube",1)[1]
query_string = urllib.parse.urlencode({"search_query" : domain})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
#print("http://www.youtube.com/watch?v=" + search_results[0])
webbrowser.open("http://www.youtube.com/watch?v={}".format(search_results[0]))
domain = command.split("youtube", 1)[1]
query_string = urllib.parse.urlencode({"search_query": domain})
html_content = urllib.request.urlopen(
"http://www.youtube.com/results?" + query_string
)
search_results = re.findall(
r"href=\"\/watch\?v=(.{11})", html_content.read().decode()
)
# print("http://www.youtube.com/watch?v=" + search_results[0])
webbrowser.open(
"http://www.youtube.com/watch?v={}".format(search_results[0])
)
pass



elif 'hello' in command:
talk('Hello! I am TARS. How can I help you?')
elif "hello" in command:
talk("Hello! I am TARS. How can I help you?")
time.sleep(3)
elif 'who are you' in command:
talk('I am one of four former U.S. Marine Corps tactical robots')
elif "who are you" in command:
talk("I am one of four former U.S. Marine Corps tactical robots")
time.sleep(3)
else:
error = random.choice(errors)
talk(error)
time.sleep(3)


talk('TARS activated!')
talk("TARS activated!")

#loop to continue executing multiple commands
# loop to continue executing multiple commands
while True:
time.sleep(4)
tars(myCommand())


0 comments on commit c1facde

Please sign in to comment.