Last active
January 25, 2021 13:40
-
-
Save juusechec/90dea0ca27708755dd464eba0ec5a39f to your computer and use it in GitHub Desktop.
Publicar en twitter mi velocidad de internet si este se pierde o si está mucho más abajo de lo vendido
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 | |
# based on: https://arstechnica.com/information-technology/2016/02/comcast-customer-made-bot-that-tweets-at-comcast-when-internet-is-slow/ | |
# twitter access usint this tutorial: https://projects.raspberrypi.org/en/projects/getting-started-with-the-twitter-api/5 | |
import os | |
import sys | |
import csv | |
import datetime | |
import time | |
import twitter | |
def test(): | |
# run speedtest-cli | |
print('running test') | |
a = os.popen("speedtest-cli --simple").read() | |
# a = "Cannot" # uncomment for mock connection error | |
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] | |
# d = 10 # uncomment for mock download | |
print(date, "Ping: {}, Download: {}, Upload: {}".format(p, d, u)) | |
# save the data to file for local network plotting | |
file_path = os.getenv('HOME') + '/internet-data.csv' | |
out_file = open(file_path, 'a') | |
writer = csv.writer(out_file) | |
writer.writerow((ts * 1000, p, d, u)) | |
out_file.close() | |
# connect to twitter | |
TOKEN = os.getenv('TWITTER_ACCESS_TOKEN') # or paste | |
TOKEN_KEY = os.getenv('TWITTER_ACCESS_TOKEN_SECRET') # or paste | |
CON_SEC = os.getenv('TWITTER_API_KEY') # or paste | |
CON_SEC_KEY = os.getenv('TWITTER_API_KEY_SECRET') # or paste | |
my_auth = twitter.OAuth(TOKEN, TOKEN_KEY, CON_SEC, CON_SEC_KEY) | |
twit = twitter.Twitter(auth=my_auth) | |
# try to tweet if speedtest couldnt even connet. Probably wont work if the | |
# internet is down | |
if "Cannot" in a: | |
print("trying to tweet connection error") | |
try: | |
tweet = "Hola @ClaroColombia. Por qué mi internet está abajo? Pago por 75 MB bajada y 10 MB de subida en Bogotá DC? #claroteayuda #Clarofalta #Clarodown #nointernet #offline" | |
twit.statuses.update(status=tweet) | |
except BaseException as e: | |
print("Cannot Twit Error", e) | |
pass | |
# tweet if down speed is less than whatever I set | |
elif float(d) < 50: | |
print("trying to tweet") | |
try: | |
# i know there must be a better way than to do (str(int(eval()))) | |
tweet = "Hola @ClaroColombia. Por qué mi internet tiene {} Mb de bajada y {} Mb de subida cuando pago por 75 MB bajada y 10 MB de subida en Bogotá DC? #claroteayuda #Clarofalta #falla".format(d, u) | |
twit.statuses.update(status=tweet) | |
except Exception as e: | |
print("exception download less than", str(e)) | |
pass | |
return | |
if __name__ == '__main__': | |
test() | |
print('completed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment