Last active
April 25, 2017 06:16
-
-
Save jubo/2acdc6faad3c11e8c68a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 | |
""" | |
Tweet ton speed test | |
====================== | |
Appelle l'api en ligne de commande | |
speed test, parse le resultat, et envoie un | |
tweet au compte du FAI si les valeurs | |
sont considérées trop basses. | |
Auteur : https://twitter.com/juboo_ | |
credit script original : https://t.co/sCz9Zxm9zR | |
""" | |
import os | |
import sys | |
import csv | |
import datetime | |
import time | |
import twitter | |
CONS_SEC = os.environ.get('CONS_SEC') | |
CONS_KEY = os.environ.get('CONS_KEY') | |
TOKEN = os.environ.get('TOKEN') | |
TOKEN_KEY = os.environ.get('TOKEN_KEY') | |
DOWNLOAD_VAL_PAID = os.environ.get('DOWNLOAD_VAL_PAID') | |
UPLOAD_VAL_PAID = os.environ.get('UPLOAD_VAL_PAID') | |
MIN_DOWNLOAD = os.environ.get('MIN_DOWNLOAD') | |
MIN_UPLOAD = os.environ.get('MIN_UPLOAD') | |
ISP_TWEET_ACCOUNT = "@Orange_Conseil" | |
def test_speed(): | |
ping_val = 100 | |
download_val = 0 | |
upload_val = 0 | |
# run speedtest-cli | |
print 'running test ----------------' | |
result = os.popen("python ./bin/speedtest-cli --simple").read() | |
print 'test ok ---------------------' | |
# split the 3 line result (ping,down,up) | |
lines = result.split('\n') | |
print result | |
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 not "Cannot" in result: | |
ping_val = lines[0][6:11] | |
download_val = lines[1][10:14] | |
upload_val = lines[2][8:12] | |
print date, ping_val, download_val, upload_val | |
# save the data to file for local network plotting | |
out_file = open('./data.csv', 'a') | |
writer = csv.writer(out_file) | |
writer.writerow((ts * 1000, ping_val, download_val, upload_val)) | |
out_file.close() | |
my_auth = twitter.OAuth(TOKEN, TOKEN_KEY, CONS_SEC, CONS_KEY) | |
twit = twitter.Twitter(auth=my_auth) | |
if eval(download_val) < eval(MIN_DOWNLOAD) or eval(upload_val) < eval(MIN_UPLOAD): | |
print "trying to tweet --------------" | |
formatted_download_val = str(float(eval(download_val))) | |
formatted_upload_val = str(float(eval(upload_val))) | |
try: | |
tweet = "Hey %s pourquoi ma connexion internet %sdown/%sup alors que je paie pour %sdown/%sup ? #speedtest" % (ISP_TWEET_ACCOUNT, | |
formatted_download_val, | |
formatted_upload_val, | |
DOWNLOAD_VAL_PAID, | |
UPLOAD_VAL_PAID) | |
# test | |
print tweet | |
print len(tweet) | |
twit.statuses.update(status=tweet) | |
print "tweet sent ! --------------" | |
except Exception, e: | |
print str(e) | |
pass | |
return | |
if __name__ == '__main__': | |
test_speed() | |
print 'completed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment