Skip to content

Instantly share code, notes, and snippets.

@BirkhoffLee
Created March 14, 2019 23:46
Show Gist options
  • Select an option

  • Save BirkhoffLee/0e8f494a3944708b61beaf5a6f906b4f to your computer and use it in GitHub Desktop.

Select an option

Save BirkhoffLee/0e8f494a3944708b61beaf5a6f906b4f to your computer and use it in GitHub Desktop.
Calculates traffic usage with rixCloud API and push notification to your iPhone
#!/bin/bash
# https://unix.stackexchange.com/a/259254/301433
bytesToHuman() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,P,E,Z,Y}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]}"
}
RECENT="$(curl -X GET "https://api.rixcloud.io/v2/profile/service/XXXXX/traffic/log" -H "accept: */*" -H "Authorization: Basic XXXXXXXXXXXXXXXXXXXX")"
TOTAL="$(curl -X GET "https://api.rixcloud.io/v2/profile/service/XXXXX/traffic" -H "accept: */*" -H "Authorization: Basic XXXXXXXXXXXXXXXXXXXX")"
a=$(jq '.data.download[6]' <<< "$RECENT")
b=$(jq '.data.download[7]' <<< "$RECENT")
c=$(jq '.data.upload[6]' <<< "$RECENT")
d=$(jq '.data.upload[7]' <<< "$RECENT")
m=$(jq '.data.download' <<< "$TOTAL")
n=$(jq '.data.upload' <<< "$TOTAL")
p=$(jq '.data.total' <<< "$TOTAL")
YESTERDAY_USAGE=$(bytesToHuman $(( $b - $a + $d - $c )))
TOTAL_REMAINING=$(bytesToHuman $(( $p - $m - $n )))
MSG="Yesterday used $YESTERDAY_USAGE. $TOTAL_REMAINING left for the month."
echo $MSG
# https://www.v2ex.com/t/467407
# curl -L "https://api.day.app/XXXXXXXXXXXXXXXXXXXX/rixCloud/$MSG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment