Skip to content

Instantly share code, notes, and snippets.

@levidurfee
Created January 31, 2016 06:17
Show Gist options
  • Save levidurfee/f3bc55f2e87413879fda to your computer and use it in GitHub Desktop.
Save levidurfee/f3bc55f2e87413879fda to your computer and use it in GitHub Desktop.
Tweet your speed results from speedtest
#!/usr/bin/env python
import os
import sys
import csv
import datetime
import time
import twitter
def tb():
#run speedtest-cli
print 'running test'
a = os.popen("python /home/pi/speedtest/speedtest-cli --simple").read()
print 'ran'
#split the 3 line result (ping,down,up)
lines = a.split('\n')
print a
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
#if speedtest could not connect set the speeds to 0
if "Cannot" in a:
p = 100
d = 0
u = 0
#extract the values for ping down and up values
else:
p = lines[0][6:11]
d = lines[1][10:14]
u = lines[2][8:12]
print date,p, d, u
#save the data to file for local network plotting
out_file = open('/home/pi/twitterbot/data.csv', 'a')
writer = csv.writer(out_file)
writer.writerow((ts*1000,p,d,u))
out_file.close()
# twitter stuff
# put in your twitter stuff below
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='')
print(api.VerifyCredentials())
status = api.PostUpdate('@levidurfee ' + str(int(eval(d))) + ' down and ' + str(int(eval(u))) + ' up')
print(status.text)
return
if __name__ == '__main__':
tb()
print 'completed'
@levidurfee
Copy link
Author

Credit to AlekseyP for the majority of the code and the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment