Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
scottvlaminck committed Apr 29, 2016
1 parent 2b32acd commit 2082cfc
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gsheet_add.cfg
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
# raspberrypi-speedtest

# raspberrypi-speedtest

Inspired by Make magazine http://makezine.com/projects/send-ticket-isp-when-your-internet-drops/


# Setup

## Prep your Raspberry Pi for doing the speedtest, per the article

* `sudo apt-get install python-pip`
* `sudo pip install speedtest-cli`


### Test

* `speedtest-cli`
* `speedtest-cli --simple`


### Install the extras

* `sudo apt-get install git`
* `git clone https://github.com/HenrikBengtsson/speedtest-cli-extras.git`


### Test

* /home/pi/speedtest-cli-extras/bin/speedtest-csv


## Prep your Raspberry Pi for saving the speedtest results (via this project)

### Install google api client for python

* `git clone https://github.com/google/gdata-python-client.git`
* `python ./setup.py install`


### Clone this project

* `git clone https://github.com/scottvlaminck/raspberrypi-speedtest`


### Config this project

* Create a google spreadsheet via https://docs.google.com/spreadsheets/u/0/
* With the following headers in the first row:
> `ConnectionType startdate stopdate provider ip speedtestserver distance pingtime downloadspeed uploadspeed resultimg`
* Get the id (which you can get from the url, e.g.: https://docs.google.com/spreadsheets/d/**SPREADSHEET-ID**/edit#gid=0)
* Create a gdocs app with oauth creds via https://console.developers.google.com/project and for that app:
* Create an OAuth 2.0 client ID
* Enable Google Drive API
* Rename gsheet.cfg to gsheet_add.cfg
* `mv gsheet.cfg gsheet_add.cfg`
* Update `gsheet_add.cfg` with sheet id and oauth client + secret
* Get an oauth token
* `python get_auth_token.py`
* Update `gsheet_add.cfg` with oauth & refresh tokens


### Test this thing

* `run.sh`
* NOTE: This `run.sh` script is helpful, but very naive. It assumes that this project lives side-by-side with the speedtest-cli-extras/ directory. If that's not true, you should skip using it.
* verify the sheet has a row of data


## Run the script at the top of each hour

### Add it to cron

* `crontab -e`
* In the resulting editor, add the following line:
> `0 * * * * /home/pi/raspberrypi-speedtest/run.sh`

### Graph the data

* Update the spreadsheet to graph the data






45 changes: 45 additions & 0 deletions get_auth_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# see: http://stackoverflow.com/questions/15075919/gdata-python-google-apps-authentication

import ConfigParser
import gdata.gauth

config = ConfigParser.ConfigParser()
config.read('gsheet_add.cfg')

if not (config.has_option('oauth', 'client_id') and config.has_option('oauth', 'client_secret')):
raise ValueError('auth client_id and client_secret are required')



Client_id=config.get('oauth', 'client_id')
Client_secret=config.get('oauth', 'client_secret')
Scope='https://spreadsheets.google.com/feeds/'
User_agent='rpi-speedtest-auth'

token = gdata.gauth.OAuth2Token(client_id=Client_id,client_secret=Client_secret,scope=Scope,user_agent=User_agent)

print ''
print 'open ' + token.generate_authorize_url(redirect_uri='urn:ietf:wg:oauth:2.0:oob')
print ''

code = raw_input('What is the verification code? ').strip()
token.get_access_token(code)

print ""
print "Access Token"
print token.access_token
print ""
print "Refresh token"
print token.refresh_token

print ""


# config.set('oauth_token', 'access_token', token.access_token)
# config.set('oauth_token', 'refresh_token', token.refresh_token)
#
# with open('gsheet.cfg', 'wb') as configfile:
# config.write(configfile)


16 changes: 16 additions & 0 deletions gsheet.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[oauth]
client_id=CLIENTID
client_secret=CLIENTSECRET

[oauth_token]
access_token=ACCESS_TOKEN
refresh_token=REFRESH_TOKEN

[sheet_info]
sheet_id=SPREADSHEETID
tab_id=od6

[other_values]
connection_type=lan


75 changes: 75 additions & 0 deletions gsheet_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import ConfigParser
import fileinput
import gdata.spreadsheets.client
import gdata.spreadsheets.data
import gdata.gauth
import os


# see: https://pythonhosted.org/gdata/docs/auth.html#docs-auth

# - https://developers.google.com/apps-script/guides/rest/api#general_procedure
# - https://developers.google.com/apps-script/guides/rest/quickstart/python#step_1_turn_on_the_api_name


config_filename = os.path.splitext(os.path.realpath(__file__))[0]+'.cfg'
config = ConfigParser.ConfigParser()
config.read(config_filename)


if not (config.has_option('oauth', 'client_id') and config.has_option('oauth', 'client_secret')):
raise ValueError('auth client_id and client_secret are required')


client_id=config.get('oauth', 'client_id')
client_secret=config.get('oauth', 'client_secret')

access_token=config.get('oauth_token', 'access_token')
refresh_token=config.get('oauth_token', 'refresh_token')

sheet_id=config.get('sheet_info', 'sheet_id')
tab_id=config.get('sheet_info', 'tab_id')

connection_type=config.get('other_values', 'connection_type')




for result_string in fileinput.input():

result_names = ['startdate', 'stopdate', 'provider', 'ip', 'speedtestserver', 'distance', 'pingtime', 'downloadspeed', 'uploadspeed', 'resultimg']

# 2016-04-26 02:59:03;2016-04-26 02:59:37;CenturyLink;97.116.3.36;US Internet (Minnetonka, MN);16.21 km;45.778 ms;45.73 Mbit/s;16.96 Mbit/s;http://www.speedtest.net/result/5278910900.png
result_list = result_string.split(";")


# create the OAuth2 token
token = gdata.gauth.OAuth2Token(client_id=client_id,client_secret=client_secret,scope='https://spreadsheets.google.com/feeds/',user_agent='rpi-speedtest-add',access_token=access_token,refresh_token=refresh_token)

# create the spreadsheet client and authenticate
spr_client = gdata.spreadsheets.client.SpreadsheetsClient()
token.authorize(spr_client)

#create a ListEntry. the first item of the list corresponds to the first 'header' row
entry = gdata.spreadsheets.data.ListEntry()

entry.set_value('connectiontype', connection_type)

for i in range(len(result_list)):

clean_result = result_list[i].strip()

for ending in [" km", " ms", " Mbit/s"]:
if clean_result.endswith(ending):
clean_result = clean_result[:-len(ending)]

entry.set_value(result_names[i], clean_result)

# add the ListEntry you just made
spr_client.add_list_entry(entry, sheet_id, tab_id)

# print ""
# print entry
# print ""


3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

../speedtest-cli-extras/bin/speedtest-csv | python gsheet_add.py

0 comments on commit 2082cfc

Please sign in to comment.