Created
August 8, 2017 03:00
-
-
Save pedroburon/68f41fa4757a49445df7bc8c70de7276 to your computer and use it in GitHub Desktop.
measure isp download/upload speed and send to ifttt
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/env python | |
import speedtest # pip install speedtest-cli | |
import requests # pip install requests | |
IFTTT_URL = 'https://maker.ifttt.com/trigger/{event}/with/key/{key}' | |
IFTTT_KEY = '<IFTTT_KEY>' # get from iftt webhook service | |
DOWNLOAD_THRESHOLD = 20 | |
def as_megabits(bits): | |
return int(bits) / 1000000 | |
def get_speedtest_results(): | |
test = speedtest.Speedtest() | |
test.get_best_server() | |
ping = test.best['latency'] | |
download = as_megabits(test.download()) | |
upload = as_megabits(test.upload()) | |
return ping, download, upload | |
def send_values(event, value1, value2, value3): | |
data = { | |
'value1': value1, | |
'value2': value2, | |
'value3': value3 | |
} | |
url = IFTTT_URL.format(event=event, key=IFTTT_KEY) | |
response = requests.post(url, json=data) | |
response.raise_for_status() | |
def main(): | |
ping, download, upload = get_speedtest_results() | |
send_values('speedtest', ping, download, upload) | |
if download < DOWNLOAD_THRESHOLD: | |
send_values('speedtest_threshold', ping, download, upload) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment