Skip to content

Instantly share code, notes, and snippets.

@kaveenr
Last active May 29, 2022 14:23
Show Gist options
  • Save kaveenr/a820616adf2f5d9d82db1b1250bf15f3 to your computer and use it in GitHub Desktop.
Save kaveenr/a820616adf2f5d9d82db1b1250bf15f3 to your computer and use it in GitHub Desktop.
SLT Data Meter Plugin For bit bar.

SLT Data Meter Bit Bar Extension

Inspired by https://github.com/kmchmk/SLT-Data-Usage

Screenshot

App Running On OSX

Prerequisites

Configuration File Example

Create the following file and change the CREDENTIALS_FILE path on the script.

USERNAME="catto"
PASSWORD="patto"

Use SLT Download Tool

Configure the config file and run slt-download.sh

SLT Data Export Untitlity help
	./slt-download.sh current		Current Month
	./slt-download.sh previous 1		Previous Month
	./slt-download.sh specific 2020-08-12	Previous Month

Disclaimer

This code is for education purposes only! The code shown in this gist is copyleft and provided "AS IS".

#!/bin/bash
if [[ -z $1 ]]; then
echo "SLT Data Export Untitlity help"
echo $'\t./slt-download.sh current\t\tCurrent Month'
echo $'\t./slt-download.sh previous 1\t\tPrevious Month'
echo $'\t./slt-download.sh specific 2020-08-12\tPrevious Month'
exit 2
elif [[ "$1" == "current" ]]; then
TIME_RANGE="current"
APP_MODE="month"
elif [[ "$1" == "previous" ]]; then
TIME_RANGE="previous/$2"
APP_MODE="month"
elif [[ "$1" == "specific" ]]; then
SPECIFIC_DAY="$2"
APP_MODE="day"
fi
CREDENTIALS_FILE="$HOME/.slt-creds.env"
if [[ -e "$CREDENTIALS_FILE" ]]; then
source $CREDENTIALS_FILE
else
echo "CREDENTIAL FILE $CREDENTIALS_FILE MISSING"
exit 2
fi
# Constants
JQ="/usr/local/bin/jq"
CLIENT_ID="622cc49f-6067-415e-82cd-04b1b76f6377"
BASE_PATH="https://omniscapp.slt.lk/mobitelint/slt/"
USER_AGENT="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15"
IFS=$'\t'
# Get Access Token Function
getAccessToken() {
local TOKEN_RESPONSE=$(
curl -s --fail -L -X POST "$BASE_PATH/sltvasoauth/oauth2/token" \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H "User-Agent: $USER_AGENT" \
--data-urlencode "client_id=$CLIENT_ID" \
--data-urlencode "grant_type=password" \
--data-urlencode "username=$USERNAME" \
--data-urlencode "password=$PASSWORD" \
--data-urlencode "scope=scope1"
)
if [ "$?" -ne "0" ]; then
echo "Unable to get Acccess Token"
exit 1
fi
SUBSCRIBER_ID=$(echo "$TOKEN_RESPONSE" | $JQ -r '.metadata')
TOKEN=$(echo "$TOKEN_RESPONSE" | $JQ -r '.access_token')
return 1
}
# Protocol Data Function
getProtocolUsage() {
local RESPONSE=$(
curl -s -L -X GET "https://omniscapp.slt.lk/mobitelint/slt/sltvasservices/protocolhistory?date=$1" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
PROTO_USAGE=$(echo "$RESPONSE" | $JQ -r '.total | map([.protocol, .presentage]) | .[] | @tsv')
return 1
}
# Daily Usage Function
getDailyUsage() {
local RESPONSE=$(
curl -s -L -X GET "$BASE_PATH/sltvasservices/dailyusage/$1?billdate=01" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
DAILY_USAGE=$(echo "$RESPONSE" | $JQ -r '.dailylist | map(select(.totalusage != null ))| map([.date, (.totalusage | tonumber), .volumeunit]) | .[] | @tsv')
return 1
}
# Print TSV Header
echo $'Date\tDailyUsage\tUnit\tProtocol\tPresentage\tUsage'
# Get Token
getAccessToken
if [[ "$APP_MODE" == "month" ]]; then
# Get Daily Usage Numbers
getDailyUsage "$TIME_RANGE"
# Iterate Trhough Daily Numbers
while read -r date usage unit
do
if [[ $(echo "$usage > 0" | bc -l) == 1 ]]; then
# Get Protocol Usage For Day
getProtocolUsage "$date"
# Iternate Through Daily Protocol Usage
while read -r protocol presentage
do
PROTO_USAGE=$(echo "$usage * $presentage / 100" | bc -l | awk '{printf "%f", $0}')
echo "$date$IFS$usage$IFS$unit$IFS$protocol$IFS$presentage$IFS$PROTO_USAGE"
done <<< "$PROTO_USAGE"
fi
done <<< "$DAILY_USAGE"
elif [[ "$APP_MODE" == "day" ]]; then
echo "I need to do this"
fi
#!/bin/bash
# Load Credentials File
CREDENTIALS_FILE="$HOME/.slt-creds.env"
if [[ -e "$CREDENTIALS_FILE" ]]; then
source $CREDENTIALS_FILE
else
echo "[ CREDENTIAL FILE $CREDENTIALS_FILE MISSING]"
exit 2
fi
# Constants
IFS=$'\t'
JQ="/usr/local/bin/jq"
CLIENT_ID="622cc49f-6067-415e-82cd-04b1b76f6377"
BASE_PATH="https://omniscapp.slt.lk/mobitelint/slt/"
USER_AGENT="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15"
# Get Access Token
TOKEN_RESPONSE=$(
curl -s -L -X POST "$BASE_PATH/sltvasoauth/oauth2/token" \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H "User-Agent: $USER_AGENT" \
--data-urlencode "client_id=$CLIENT_ID" \
--data-urlencode "grant_type=password" \
--data-urlencode "username=$USERNAME" \
--data-urlencode "password=$PASSWORD" \
--data-urlencode "scope=scope1"
)
SUBSCRIBER_ID=$(echo "$TOKEN_RESPONSE" | $JQ -r '.metadata')
TOKEN=$(echo "$TOKEN_RESPONSE" | $JQ -r '.access_token')
# Get Summary
SUMARRY_RESPONSE=$(
curl -s -L -X GET "$BASE_PATH/sltvasservices/dashboard/summary" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
CON_STATUS=$(echo "$SUMARRY_RESPONSE" | $JQ -r '.status')
CON_USED_PRES=$(echo "$SUMARRY_RESPONSE" | $JQ -r '.my_package_info.usageDetails[0].percentage')
CON_PKG=$(echo "$SUMARRY_RESPONSE" | $JQ -r '.my_package_info.package_name')
CON_USED=$(echo "$SUMARRY_RESPONSE" | $JQ -r '.my_package_info.usageDetails[0].used')
CON_TOT=$(echo "$SUMARRY_RESPONSE" | $JQ -r '.my_package_info.usageDetails[0].limit')
CON_REPORTED=$(echo "$SUMARRY_RESPONSE" | $JQ -r '.reported_time')
CON_AVB_GB=$(echo "$CON_TOT - $CON_USED" | bc -l)
# Get Current History
USAGE_REPORTS=$(
curl -s -L -X GET "$BASE_PATH/sltvasservices/dailyusage/current?billdate=01" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
DAILY_REPORTS=$(echo "$USAGE_REPORTS" | $JQ -r '.dailylist | map(.displaydate + " -> " + .totalusage + .volumeunit ) | reverse[0:10] | .[]')
YESTERDAY=$(echo "$USAGE_REPORTS" | $JQ -r '.dailylist | reverse[0] | .date')
YESTERDAY_USAGE=$(echo "$USAGE_REPORTS" | $JQ -r '.dailylist | reverse[0] | .totalusage')
# Get Detailed Reort For Yesterday
DETAILED_REPORT=$(
curl -s -L -X GET "https://omniscapp.slt.lk/mobitelint/slt/sltvasservices/protocolhistory?date=$YESTERDAY" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
YESTERDAY_PROTO=$(echo "$DETAILED_REPORT" | $JQ -r '.total | map([.protocol, .presentage]) | .[] | @tsv')
# Get Extra GB Data
EXTRA_GB=$(
curl -s -L -X GET "$BASE_PATH/sltvasservices/dashboard/extragb" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
EXTRA_GB_LIMIT=$(echo "$EXTRA_GB" | $JQ -r '.package_summary | .limit')
EXTRA_GB_USED=$(echo "$EXTRA_GB" | $JQ -r '.package_summary | .used')
EXTRA_AVB_GB=$(echo "$EXTRA_GB_LIMIT - $EXTRA_GB_USED" | bc -l)
# Bonus Data
BONUS_DATA=$(
curl -s -L -X GET "$BASE_PATH/sltvasservices/dashboard/bonus_data" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
BONUS_DATA_LIMIT=$(echo "$BONUS_DATA" | $JQ -r '.package_summary | .limit')
BONUS_DATA_USED=$(echo "$BONUS_DATA" | $JQ -r '.package_summary | .used')
BONUS_DATA_AVB_GB=$(echo "$BONUS_DATA_LIMIT - $BONUS_DATA_USED" | bc -l)
# Vas Data
VAS_DATA=$(
curl -s -L -X GET "$BASE_PATH/sltvasservices/dashboard/vas_data" \
-H "User-Agent: $USER_AGENT" \
-H "subscriberid: $SUBSCRIBER_ID" \
-H "x-ibm-client-id: $CLIENT_ID" \
-H "authorization: Bearer $TOKEN"
)
VAS_DETAILS=$(echo "$VAS_DATA" | $JQ -r '.usageDetails | map([.name, .used]) | .[] | @tsv')
TOTAL_AVB=$(echo "$CON_AVB_GB + $EXTRA_AVB_GB + $BONUS_DATA_AVB_GB" | bc -l)
echo "[ 🌏 SLT $CON_USED_PRES% $TOTAL_AVB GB ]"
echo "---"
echo "Package Name: $CON_PKG"
echo "Used $CON_USED / $CON_TOT GB"
echo "Available $CON_AVB_GB GB"
echo "---"
echo "Extra Data: $EXTRA_GB_USED / $EXTRA_GB_LIMIT GB"
echo "Bonus Data: $BONUS_DATA_USED / $BONUS_DATA_LIMIT GB"
echo "---"
echo "VAS Packages / Free Usage"
echo "---"
while read -r pkg usage
do
echo "$pkg -> $usage GB"
done <<< "$VAS_DETAILS"
echo "---"
echo "Usage For Last 10 Days"
echo "---"
echo "$DAILY_REPORTS"
echo "---"
echo "Yesterday's Protocol Breakdown"
echo "Total Usage: $YESTERDAY_USAGE"
echo "---"
while read -r protocol presentage
do
PROTO_USAGE=$(echo "($YESTERDAY_USAGE * $presentage) / 100" | bc -l | awk '{printf "%.1f", $0}')
PROTO_PRES=$(echo "$presentage" | awk '{printf "%.1f", $0}')
echo "$protocol -> $PROTO_USAGE GB / $PROTO_PRES%"
done <<< "$YESTERDAY_PROTO"
echo "---"
echo "Reported time $CON_REPORTED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment