Last active
August 28, 2019 09:14
-
-
Save bluemeda/7060acdc0324d1d4d62ec6b721690201 to your computer and use it in GitHub Desktop.
A wise way to spend your remaining internet data
This file contains 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 bash | |
usage() | |
{ | |
clear | |
echo | |
RESET_ATTR="$(tput sgr0)" | |
COLOR_GREEN="$(tput setaf 2)" | |
COLOR_DARK_GREY="$(tput setaf 245)" | |
COLOR_DARK_BLUE="$(tput setaf 046)" | |
echo "geget, Version 0.0.1" | |
echo | |
echo "A ${COLOR_GREEN}wise way${RESET_ATTR} to spend your remaining internet data." | |
echo | |
echo "Usage: ${COLOR_GREEN}$0 [-q|--quota] quota{M|G}${RESET_ATTR}" | |
echo | |
echo "Where: ${COLOR_GREEN}quota${RESET_ATTR} is either in GB (1G or 2.3G)," | |
echo " or in MB (Must be greater or equal than 100.)." | |
echo | |
echo "Example: ${COLOR_GREEN}$0 -q 2G${RESET_ATTR}" | |
echo " ${COLOR_GREEN}$0 -q 1.5G${RESET_ATTR}" | |
echo " ${COLOR_GREEN}$0 -q 700M${RESET_ATTR}" | |
echo | |
echo "Written by ${COLOR_DARK_GREY}Apip${RESET_ATTR}" | |
echo "${COLOR_DARK_BLUE}https://bluemeda.web.id${RESET_ATTR}" | |
echo | |
exit 2 | |
} | |
calculate() | |
{ | |
QUOTA_NUM=${1%?} | |
URL="https://sgp-ping.vultr.com/vultr.com.100MB.bin" | |
if [[ $QUOTA =~ .+[G]$ ]]; then | |
QUOTA_NUM="$(echo "${QUOTA_NUM} * 1000" | bc)" | |
elif [[ 1 -eq "$(echo "${QUOTA_NUM} < 100" | bc)" ]]; then | |
usage | |
fi | |
LOOP="$(echo "${QUOTA_NUM} / 100" | bc)" | |
for (( i = 0; i < $LOOP; i++ )); do | |
curl -# -H 'Cache-Control: no-cache' --output /dev/null $URL | |
TOTAL_DATA=$(((i+1)*100)) | |
DATA_UNIT="MB" | |
if [[ $TOTAL_DATA -gt 1000 ]]; then | |
TOTAL_DATA="$(echo "scale=1; ${TOTAL_DATA} / 1000" | bc)" | |
DATA_UNIT=GB | |
fi | |
echo "${TOTAL_DATA}${DATA_UNIT}" | |
done | |
} | |
case $1 in | |
-q | --quota ) | |
shift; QUOTA=$1 | |
;; | |
*) | |
usage | |
;; | |
esac; shift | |
if [[ $QUOTA =~ ^[0-9]+\.?[0-9]*[GM]$ ]]; then | |
calculate $QUOTA | |
else | |
usage | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment