Skip to content

Commit

Permalink
Jasper SpeedTest Module
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtBIT committed Jun 21, 2016
0 parents commit 404b001
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2016 Djordje Ungar. http://djordjeungar.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Jasper-Module-Speedtest
======================

Jasper Speedtest Module which allows you to run speedtest.net tests and sends you the report to your email.

##Steps to install Speedtest Module

* Install the `speedtest-cli` package via pip:
```
sudo pip install speedtest-cli
```
* run the following commands in order:
```
git clone https://github.com/ArtBIT/jasper-module-speedtest.git
cp jasper-module-speedtest/Speedtest.py <path to jasper/client/modules>
#i.e. cp jasper-module-speedtest/Speedtest.py /usr/local/lib/jasper/client/modules/
```
* Edit `~/.jasper/profile.yml` and add the following at the bottom:
```
speedtest:
email: '[email protected]'
```
* Make sure you have [mailgun](http://jasperproject.github.io/documentation/configuration/#mailgun) or gmail account set-up for Jasper.
* Restart the Pi:
```
sudo reboot
```
##Congrats, JASPER Speedtest Module is now installed and ready for use.
Here are some examples:
```
YOU: Speedtest
JASPER: Okay, this will only take a minute. *runs the test and reports the upload/download speeds back to you*
```

105 changes: 105 additions & 0 deletions Speedtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8-*-
# vim: set expandtab:ts=4:sw=4:ft=python
import re
import subprocess
import urllib
from datetime import datetime

WORDS = ["SPEEDTEST", "SPEED", "TEST"]
PRIORITY = 3


def send_mail(profile, subject, text, files=None):
try:
if 'mailgun' in profile:
user = profile['mailgun']['username']
password = profile['mailgun']['password']
server = 'smtp.mailgun.org'
else:
user = profile['gmail_address']
password = profile['gmail_password']
server = 'smtp.gmail.com'
except:
pass

send_to = profile['speedtest']['email']
msg = MIMEMultipart()
msg['From'] = user
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

msg.attach(MIMEText(text))

for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)

session = smtplib.SMTP(server, 587)
session.starttls()
session.login(user, password)
session.sendmail(user, send_to, msg.as_string())
session.quit()


def handle(text, mic, profile):
"""
Responds to user-input, typically speech text, by performing a
speedtest.net analysis and sending the resulting image to users
email.
Arguments:
text -- user-input, typically transcribed speech
mic -- used to interact with the user (for both input and output)
profile -- contains information related to the user (e.g., phone
number)
"""

mic.say("Okay, this will only take a minute.")

## call the speedtest-cli command
p = subprocess.Popen(["speedtest", "--simple", "--share"],
stdout=subprocess.PIPE)
(output, err) = p.communicate()

## Wait for it to terminate. Get return returncode
p_status = p.wait()

upload = ''
download = ''
filename = ''
speedtest_url = ''
for line in output.splitlines():
if line.startswith('Download: '):
download = line
elif line.startswith('Upload: '):
upload = line
elif line.startswith('Share results: '):
filename = '/tmp/speedtest-%s.png' % datetime.now().strftime('%Y-%m-%d.%H:%M:%S.%f')
speedtest_url = line.replace('Share results: ', '')
urllib.urlretrieve(speedtest_url, filename)

if upload == '' or download == '' or speedtest_url == '' or filename == '':
return mic.say("There was an error executing speedtest")

mic.say(upload.replace(': ', ' speed is ').replace('Mbit/s', 'megabits per second.'))
mic.say(download.replace(': ', ' speed is ').replace('Mbit/s', 'megabits per second.'))
message = "\n".join([upload, download, speedtest_url])
send_mail(profile, 'SpeedTest.Net Results', message, [filename])


def isValid(text):
"""
Returns True if the input is related to the meaning of life.
Arguments:
text -- user-input, typically transcribed speech
"""

regex = "(" + "|".join(WORDS) + ")"
return bool(re.search(regex, text, re.IGNORECASE))


0 comments on commit 404b001

Please sign in to comment.