first commit
David Blume

David Blume commited on 2019-10-10 22:35:14
Showing 3 changed files, with 98 additions and 0 deletions.

... ...
@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright (c) 2019 David Blume
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy 
6
+of this software and associated documentation files (the "Software"), to deal 
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
9
+copies of the Software, and to permit persons to whom the Software is 
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in all 
13
+copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+SOFTWARE.
... ...
@@ -0,0 +1,34 @@
1
+[![License](https://img.shields.io/badge/license-MIT_license-blue.svg)](https://raw.githubusercontent.com/dblume/md-reader/master/LICENSE.txt)
2
+![bash](https://img.shields.io/badge/bash-4.2-blue.svg)
3
+
4
+## A Command-line Bandwidth Calculator
5
+
6
+With fast.sh, you can issue a Unix command from the terminal to calculate
7
+download bandwidth from fast.com. For example:
8
+
9
+    $ fast.sh
10
+    # Bytes Cur. rate   Avg. Rate
11
+    ------- ----------- -----------
12
+    1.22GiB [31.1MiB/s] [31.1MiB/s]
13
+
14
+## Credit
15
+
16
+The original implementation comes from Tiksi's 
17
+[one-liner at Hacker News](https://news.ycombinator.com/item?id=12259331). I just
18
+added some whitespace and made some adjustments that made it more flexible and
19
+easier for me to read and maintain.
20
+
21
+## Requirements
22
+
23
+The script requires these two handy modules:
24
+
25
+1. **[jq](https://stedolan.github.io/jq/)**: A JSON parser.
26
+2. **[pv](http://www.ivarch.com/programs/pv.shtml)**: ([mirror](https://github.com/icetee/pv)) A pipe viewer.
27
+
28
+## Is it any good?
29
+
30
+[Yes](https://news.ycombinator.com/item?id=3067434).
31
+
32
+## Licence
33
+
34
+This software uses the [MIT license](http://git.dlma.com/fast-bandwidth-calculator.git/blob/master/LICENSE.txt).
... ...
@@ -0,0 +1,43 @@
1
+#!/usr/bin/env bash
2
+# Adapted from the one-liner at https://news.ycombinator.com/item?id=12259331
3
+set -eu -o pipefail
4
+
5
+if ! command -v pv >/dev/null; then
6
+    echo >&2 "Pipe viewer, pv, is required. Please install pv with your package manager."
7
+    exit 1
8
+fi
9
+
10
+if ! command -v jq >/dev/null; then
11
+    echo >&2 "JSON processor, jq, is required. Please install jq with your package manager."
12
+    exit 1
13
+fi
14
+
15
+# Use the fifo as a lock so only one instance of this script can run. (Alternative to flock(1))
16
+declare -r FIFO=${TMPDIR:-/tmp}/$(basename "$BASH_SOURCE").fifo
17
+if [ -e "$FIFO" ]; then
18
+    echo >&2 "Run aborted since fifo and mutex lock $FIFO already exists."
19
+    exit 1
20
+fi
21
+mkfifo $FIFO
22
+trap "rm -f $FIFO" EXIT
23
+
24
+# This works on OS X, but GNU grep works without the cut: grep -Po '(?<=\<script src=")[^"]+'
25
+declare -r JS=$(curl -s https://fast.com/ | egrep -o '\<script src="[^"]+' | cut -f2 -d'"')
26
+# This works on OS X, but GNU grep works without the cut: grep -Pom1 '(?<=token:")[^"]+'
27
+declare -r TOKEN=$(curl -s https://fast.com/$JS | egrep -om1 'token:"[^"]+' | cut -f2 -d'"')
28
+
29
+echo "# Bytes Cur. rate   Avg. Rate"
30
+echo "------- ----------- -----------"
31
+
32
+# Get a list of URL templates, insert ranges, and make requests in the background.
33
+curl -s "https://api.fast.com/netflix/speedtest?https=true&token=$TOKEN" | jq -r '.[].url' | while read url
34
+do
35
+    FILE_2K=${url/speedtest/speedtest\/range\/0-2048}
36
+    FILE_25M=${url/speedtest/speedtest\/range\/0-26214400}
37
+    (curl -s -H 'Referer: https://fast.com/' -H 'Origin: https://fast.com' "$FILE_2K" > $FIFO
38
+     for i in {1..10}
39
+     do
40
+         curl -s -H 'Referer: https://fast.com/' -H 'Origin: https://fast.com'  "$FILE_25M" > $FIFO
41
+     done) &
42
+done
43
+pv -rab $FIFO > /dev/null
0 44