Skip to content

Commit

Permalink
Add support for other internet packages with a full usage report
Browse files Browse the repository at this point in the history
  • Loading branch information
kmchmk committed Aug 7, 2021
1 parent 35c200e commit 5208260
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SLT-Data-Usage
This program can get your SLT data usage and display it in the system tray. This is mostly useful if you are using an unlimited package with a daily limit.
This program can get your SLT data usage and display it in the system tray.

![image](https://user-images.githubusercontent.com/12431727/128552259-83c8b8ea-4b40-476a-ad6d-f8761d81604a.png)

Expand All @@ -18,5 +18,5 @@ pip install requests

Create executable:
```
pyinstaller --hidden-import 'pystray._win32' --noconsole --onefile 'system_tray.py' --name 'SLT_Usage'
pyinstaller --noconsole --onefile 'system_tray.py' --name 'SLT_Usage'
```
106 changes: 76 additions & 30 deletions data_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,81 @@
import urllib.parse
import input_ui

X_IBM_CLIENT_ID = '622cc49f-6067-415e-82cd-04b1b76f6377'


def get_data_usage():
credentials = read_write.readCredentialsFromFile()
# Get access token first
url = "https://omniscapp.slt.lk/mobitelint/slt/sltvasoauth/oauth2/token"
payload = "client_id={2}&grant_type=password&password={1}&scope=scope1&username={0}".format(
credentials['username'], urllib.parse.quote(credentials['password']), X_IBM_CLIENT_ID)
headers = {'content-type': "application/x-www-form-urlencoded"}
response = requests.request("POST", url, data=payload, headers=headers)
response = json.loads(response.text)

try:
access_token = response["access_token"]
subscriberid = response["metadata"]

# Get the data usage
url = "https://omniscapp.slt.lk/mobitelint/slt/sltvasservices/dashboard/summary"
headers = {
'subscriberid': subscriberid,
'x-ibm-client-id': X_IBM_CLIENT_ID,
'authorization': "Bearer {}".format(access_token)
}
response = requests.request("GET", url, headers=headers)

class DataUsage:
X_IBM_CLIENT_ID = '622cc49f-6067-415e-82cd-04b1b76f6377'

def __init__(self):
pass

def refresh(self):
credentials = read_write.readCredentialsFromFile()
# Get access token first
url = "https://omniscapp.slt.lk/mobitelint/slt/sltvasoauth/oauth2/token"
payload = "client_id={2}&grant_type=password&password={1}&scope=scope1&username={0}".format(
credentials['username'], urllib.parse.quote(credentials['password']), self.X_IBM_CLIENT_ID)
headers = {'content-type': "application/x-www-form-urlencoded"}
response = requests.request("POST", url, data=payload, headers=headers)
response = json.loads(response.text)
data_usage = response["vas_data_summary"]["used"]

return data_usage
except:
input_ui.change_account(None)
return 'X'
try:
access_token = response["access_token"]
subscriberid = response["metadata"]

# Get the data usage
url = "https://omniscapp.slt.lk/mobitelint/slt/sltvasservices/dashboard/summary"
headers = {
'subscriberid': subscriberid,
'x-ibm-client-id': self.X_IBM_CLIENT_ID,
'authorization': "Bearer {}".format(access_token)
}
self.response = json.loads(requests.request(
"GET", url, headers=headers).text)
self.package_name = self.response["my_package_info"]["package_name"]

except:
input_ui.change_account(None)
self.response = None

def get_summary(self):
if(not self.response):
return "X"
elif(self.package_name.startswith("UNLIMITED FLASH")):
return self.response["vas_data_summary"]["used"]
else:
return self.response["my_package_info"]["usageDetails"][1]["used"]

def get_usage_report(self):
if(not self.response):
return "Incorrect credentials..."

# Let's creat a full report
report = self.package_name

if(self.response["bonus_data_summary"]):
bonus_used = self.response["bonus_data_summary"]["used"]
bonus_limit = self.response["bonus_data_summary"]["limit"]
report += "\nBonus: {} / {}".format(bonus_used, bonus_limit)

if(self.response["vas_data_summary"]):
vas_used = self.response["vas_data_summary"]["used"]
vas_limit = self.response["vas_data_summary"]["limit"]
report += "\nVAS: {} / {}".format(vas_used, vas_limit)

if(self.response["extra_gb_data_summary"]):
extra_gb_used = self.response["extra_gb_data_summary"]["used"]
extra_gb_limit = self.response["extra_gb_data_summary"]["limit"]
report += "\nExtra: {} / {}".format(extra_gb_used, extra_gb_limit)

if(len(self.response["my_package_info"]["usageDetails"]) > 0):
anytime_used = self.response["my_package_info"]["usageDetails"][0]["used"]
anytime_limit = self.response["my_package_info"]["usageDetails"][0]["limit"]
report += "\nAnytime: {} / {}".format(anytime_used, anytime_limit)

if(len(self.response["my_package_info"]["usageDetails"]) > 1):
total_used = self.response["my_package_info"]["usageDetails"][1]["used"]
total_limit = self.response["my_package_info"]["usageDetails"][1]["limit"]
report += "\nNight: {} / {}".format(float(total_used) - float(
anytime_used), float(total_limit) - float(anytime_limit))
report += "\nTotal: {} / {}".format(total_used, total_limit)
return report
11 changes: 7 additions & 4 deletions system_tray.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pystray
import pystray._win32
from PIL import Image, ImageDraw, ImageFont
import time
import data_usage
Expand All @@ -7,19 +7,22 @@
size = 100
font_type = ImageFont.truetype("tahoma.ttf", size//2)
TOP_LEFT = (0, 0)
def get_empty_image(): return Image.new('RGBA', (size, size//2)) # Empty image
current_data_usage = data_usage.DataUsage()
refresh_itr_duration = 120 # seconds


refresh_itr_duration = 120 # seconds
def get_empty_image(): return Image.new('RGBA', (size, size//2)) # Empty image


def refresh(icon):
image = get_empty_image()
draw = ImageDraw.Draw(image)
# ToDo - I used yellow below because of the System wide Dark theme
draw.text(TOP_LEFT, data_usage.get_data_usage(),
current_data_usage.refresh()
draw.text(TOP_LEFT, current_data_usage.get_summary(),
font=font_type, fill='yellow')
icon.icon = image
icon.title = current_data_usage.get_usage_report()


def update_forever(icon):
Expand Down

0 comments on commit 5208260

Please sign in to comment.