Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scriptable widget script to show Singapore nearby bus stop
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: magic;\
queryBusStopCode(function(code) {
queryBusArrival(code, function(arrivalData) {
let widget = createWidget({'busstopcode':code,'arrivalData':arrivalData});
if (config.runsInWidget) {
// The script runs inside a widget, so we pass our instance of ListWidget to be shown inside the widget on the Home Screen.
Script.setWidget(widget)
} else {
// The script runs inside the app, so we preview the widget.
widget.presentMedium()
}
// Calling Script.complete() signals to Scriptable that the script have finished running.
// This can speed up the execution, in particular when running the script from Shortcuts or using Siri.
Script.complete()
});
})
function createWidget(opts) {
let widget = new ListWidget()
// Add background gradient
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
new Color("141414"),
new Color("13233F")
]
widget.backgroundGradient = gradient
var headerRow = widget.addStack()
headerRow.layoutVertically()
headerRow.topAlignContent()
headerRow.setPadding(0, 0, 0, 0)
let headerElement = headerRow.addText('Nearby Bus Arrival')
headerElement.textColor = Color.white()
headerElement.textOpacity = 1
headerElement.font = Font.mediumSystemFont(14)
headerElement.leftAlignText()
widget.addSpacer(5)
var dateRow = widget.addStack()
dateRow.layoutVertically()
dateRow.topAlignContent()
dateRow.setPadding(0, 0, 0, 0)
widget.addSpacer(5)
var currentRow = widget.addStack()
currentRow.layoutHorizontally()
currentRow.setPadding(0, 0, 0, 0)
var leftColumn = currentRow.addStack()
leftColumn.layoutVertically()
leftColumn.setPadding(0, 0, 0, 0)
leftColumn.spacing = 0
currentRow.addSpacer(100)
var rightColumn = currentRow.addStack()
rightColumn.layoutVertically()
rightColumn.setPadding(0, 0, 0, 0)
let title = createDateTitle()
let titleElement = dateRow.addText(title)
titleElement.textColor = Color.white()
titleElement.textOpacity = 1
titleElement.font = Font.mediumSystemFont(12)
titleElement.leftAlignText()
let busstopno = opts.busstopcode
let busstopElement = leftColumn.addText(busstopno)
busstopElement.textColor = Color.white()
busstopElement.textOpacity = 1
busstopElement.font = Font.mediumSystemFont(16)
busstopElement.leftAlignText()
var arrivalData = opts.arrivalData;
for(var i = 0; i < arrivalData.length; i++) {
var arrivalText = arrivalData[i].ServiceNo + ' - ' + arrivalData[i].EstimatedArrival;
createBusArrivalText(rightColumn, arrivalText);
}
return widget
}
function alignRight(alignmentStack) {
alignmentStack.addSpacer()
let returnStack = alignmentStack.addStack()
return returnStack
}
function createBusArrivalText(container, text) {
let newElement;
newElement = container.addText(text)
newElement.textColor = Color.white()
newElement.textOpacity = 1
newElement.font = Font.mediumSystemFont(12)
newElement.rightAlignText()
return newElement;
}
function createDateTitle() {
var d = new Date().toString()
var a = d.split(' ');
return a[4];
}
function queryBusStopCode(fn) {
console.log('querying location');
Location.current().then(function(res) {
try {
var url = 'https://api.imgshow-apps.com/?api=1&k=q:name=bus,action=checkbusstop,lat=' + res.latitude + ',lng=' + res.longitude + ',d=1';
console.log(url)
let req = new Request(url)
req.loadJSON().then(function(json) {
fn(json[0]['BusStopCode'])
});
} catch (e) {
console.log(e);
}
});
}
function queryBusArrival(code, fn) {
try {
var url = 'https://api.imgshow-apps.com/?api=1&k=q:name=bus,action=checkbusarrival,busStopCode=' + code;
console.log(url)
let req = new Request(url)
req.loadJSON().then(function(json) {
var res = [];
for(var i = 0; i < 3; i++) {
if(json.length > i) {
var data = {};
data.ServiceNo = json[i].ServiceNo;
data.EstimatedArrival = json[i].NextBus.EstimatedArrival.substring(11, 19);
res.push(data);
}
}
console.log(JSON.stringify(res));
fn(res);
});
} catch (e) {
console.log(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment