-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
speedtest_ifttt
87 lines (75 loc) · 2.53 KB
/
speedtest_ifttt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
###########################################################################
# Originally written by: Henrik Bengtsson, 2014
# https://github.com/HenrikBengtsson/speedtest-cli-extras
# Modified to use IFTTT by: Alasdair Allan, 2015
# https://gist.github.com/aallan/bafc70a347f3b9526d30
# Modified to use .config file: Stefan Natter, 2017
# License: GPL (>= 2.1) [http://www.gnu.org/licenses/gpl.html]
###########################################################################
# import config
source $PWD/speedtest.cfg
secret_key=$ifttt_secretKey
event_name=$ifttt_event
# Character for separating values
# (commas are not safe, because some servers return speeds with commas)
sep=";"
log=$PWD/ifttt.csv
# Local functions
function str_extract() {
pattern=$1
# Extract
res=`grep "$pattern" $log | sed "s/$pattern//g"`
# Drop trailing ...
res=`echo $res | sed 's/[.][.][.]//g'`
# Trim
res=`echo $res | sed 's/^ *//g' | sed 's/ *$//g'`
echo $res
}
# Display header?
if test "$1" = "--header"; then
start="start"
stop="stop"
from="from"
from_ip="from_ip"
server="server"
server_dist="server_dist"
server_ping="server_ping"
download="download"
upload="upload"
share_url="share_url"
else
mkdir -p `dirname $log`
start=`date +"%Y-%m-%d %H:%M:%S"`
if test -n "$SPEEDTEST_CSV_SKIP" && test -f "$log"; then
# Reuse existing results (useful for debugging)
1>&2 echo "** Reusing existing results: $log"
else
# Query Speedtest
$PWD/speedtest_cli/speedtest.py --share > $log
fi
stop=`date +"%Y-%m-%d %H:%M:%S"`
# Parse
from=`str_extract "Testing from "`
from_ip=`echo $from | sed 's/.*(//g' | sed 's/).*//g'`
from=`echo $from | sed 's/ (.*//g'`
server=`str_extract "Hosted by "`
server_ping=`echo $server | sed 's/.*: //g'`
server=`echo $server | sed 's/: .*//g'`
server_dist=`echo $server | sed 's/.*\\[//g' | sed 's/\\].*//g'`
server=`echo $server | sed 's/ \\[.*//g'`
download=`str_extract "Download: "`
upload=`str_extract "Upload: "`
share_url=`str_extract "Share results: "`
fi
# Standardize units?
if test "$1" = "--standardize"; then
download=`echo $download | sed 's/Mbits/Mbit/'`
upload=`echo $upload | sed 's/Mbits/Mbit/'`
fi
# Send to IFTTT
value1=`echo $server_ping | cut -d" " -f1`
value2=`echo $download | cut -d" " -f1`
value3=`echo $upload | cut -d" " -f1`
json="{\"value1\":\"${value1}\",\"value2\":\"${value2}\",\"value3\":\"${value3}\"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/${event_name}/with/key/${secret_key}