Skip to content

Instantly share code, notes, and snippets.

@kopiro
Last active January 10, 2022 17:58
Show Gist options
  • Select an option

  • Save kopiro/1025bcce056066e8582a5d1c40f4d7e8 to your computer and use it in GitHub Desktop.

Select an option

Save kopiro/1025bcce056066e8582a5d1c40f4d7e8 to your computer and use it in GitHub Desktop.
Telia Scriptable widget
// icon-color: purple; icon-glyph: newspaper;
/*
Telia Scriptable widget
Version: 0.0.1
You need to know your subscription ID from your Telia Plan
and grab a session cookie by logging in into the mobile-app
Then, save a file in `iCloudDrive/Scriptable/Settings/Telia.vars.json` with the following
{ "SESSION_ID": "", "SUBSCRIPTION_ID": "" }
*/
function bytesToGb(bytes) {
return Math.floor(Math.round((bytes / 1024 / 1024 / 1024) * 100) / 100);
}
async function getUsage(sessId, subId) {
const req = new Request(
`https://sa.telia.se/se/rs/subscriptions/${subId}/usage`
);
req.headers = {
Accept: "application/json",
Cookie: `STSSESSION=${sessId}`,
"X-IOS-Build": "4068",
"ga-av": "10.10.4",
"ga-aid": "com.teliasonera.selfservice.telia",
"User-Agent": "com.teliasonera.selfservice.telia/10.10.4 (iOS 15.2; Apple)",
"ga-an": "Mitttelia",
};
const body = await req.loadJSON();
return body.usagesForCategory[0];
}
function createProgress({ limit, used, width, height, baseColor, fillColor }) {
const context = new DrawContext();
context.size = new Size(width, height);
context.opaque = false;
context.respectScreenScale = true;
context.setFillColor(new Color(baseColor));
const base = new Path();
base.addRoundedRect(new Rect(0, 0, width, height), 16, 24);
context.addPath(base);
context.fillPath();
context.setFillColor(new Color(fillColor));
const fill = new Path();
fill.addRoundedRect(new Rect(0, 0, (width * used) / limit, height), 16, 24);
context.addPath(fill);
context.fillPath();
return context.getImage();
}
async function createWidget() {
const fm = FileManager.iCloud();
const settingsFilePath = fm.joinPath(
fm.joinPath(fm.documentsDirectory(), "Settings"),
"Telia.vars.json"
);
await fm.downloadFileFromiCloud(settingsFilePath);
const { SESSION_ID, SUBSCRIPTION_ID } = JSON.parse(
fm.readString(settingsFilePath)
);
const { used, limit, remaining } = await getUsage(
SESSION_ID,
SUBSCRIPTION_ID
);
const baseColor = "#990ae3";
const width = 120;
const height = 30;
const w = new ListWidget();
w.backgroundColor = new Color(baseColor);
w.addSpacer();
const progressBar = w.addImage(
createProgress({
limit,
used,
width,
height,
baseColor: "#FFFFFF22",
fillColor: "#FFF",
})
);
progressBar.imageSize = new Size(width, height);
progressBar.centerAlignImage();
w.addSpacer(6);
const s = w.addStack();
s.addSpacer(8);
const usedText = s.addText(`${bytesToGb(used)}GB`);
usedText.font = Font.semiboldSystemFont(10);
s.addSpacer();
const limitText = s.addText(`${bytesToGb(limit)}GB`);
limitText.font = Font.semiboldSystemFont(10);
w.addSpacer();
const remText = w.addText(`${bytesToGb(remaining)}GB`);
remText.font = Font.boldSystemFont(36);
remText.centerAlignText();
return w;
}
const widget = await createWidget();
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentSmall();
}
Script.complete();
@kopiro
Copy link
Copy Markdown
Author

kopiro commented Jan 10, 2022

Image from iOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment