Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active October 9, 2020 07:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marco79cgn/469ba9fc067636bd34f8a96ac708d937 to your computer and use it in GitHub Desktop.
Save marco79cgn/469ba9fc067636bd34f8a96ac708d937 to your computer and use it in GitHub Desktop.
Shows the amount of free rental bikes of my home station
// your DB Bahn Developer API token
// https://developer.deutschebahn.com/store/apis/info?name=Flinkster_API_NG&version=v1&provider=DBOpenData
const bahnApiToken = "xxx"
// the longitude and latitude of your desired station
const latitude = "50.95209"
const longitude = "6.91907"
// optional: the area uid of your desired station (for 100% accurate results)
const areaUid = ""
let freeBikes = await getFreeBikes()
let widget = await createWidget()
Script.setWidget(widget)
Script.complete()
widget.presentSmall()
async function createWidget() {
let widget = new ListWidget()
widget.setPadding(10, 8, 8, 10)
// set gradient background
let gradient = new LinearGradient()
gradient.colors = [new Color("#ec0016"), new Color("#740009")]
gradient.locations = [0.0, 1]
widget.backgroundGradient = gradient
// load image
let logo = await loadImage("https://images-na.ssl-images-amazon.com/images/I/51KoHIHX20L.png")
let logoImage = widget.addImage(logo)
logoImage.rightAlignImage()
logoImage.imageSize = new Size(40,40)
let freeBikesText = widget.addText(freeBikes)
freeBikesText.font = Font.boldSystemFont(52)
freeBikesText.textColor = Color.white()
freeBikesText.centerAlignText()
widget.addSpacer()
let row = widget.addStack()
let stack = row.addStack()
stack.centerAlignContent()
stack.addSpacer(14)
let sBahnLogo = await loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/S-Bahn-Logo.svg/595px-S-Bahn-Logo.svg.png")
let sImage = stack.addImage(sBahnLogo)
let stationTxt = stack.addText(" Bhf. Ehrenfeld")
stationTxt.font = Font.boldSystemFont(12)
stationTxt.textColor = Color.white()
stationTxt.centerAlignText()
widget.url = "callabike://"
widget.addSpacer()
return widget
}
// helper function to load and parse a restful json api
async function getFreeBikes() {
const url = "https://api.deutschebahn.com/flinkster-api-ng/v1/bookingproposals?lat=" + latitude + "&lon=" + longitude + "&radius=100&providernetwork=2"
let req = new Request(url)
req.headers = { "Authorization": "Bearer " + bahnApiToken, "Content-Type": "application/json" }
const json = await req.loadJSON()
if(areaUid != null && areaUid.length > 0) {
const filteredItems = json.items.filter(x =>
x.area.href.endsWith(areaUid)
);
return filteredItems.length.toString()
} else {
return json.items.length.toString()
}
}
// helper function to download an image from a given url
async function loadImage(imgUrl) {
let req = new Request(imgUrl)
let image = await req.loadImage()
return image
}
@marco79cgn
Copy link
Author

Intro

iOS 14 Custom Widget made with the help of the Scriptable app. It shows the amount of free rental bikes at my home station (callabike.de)

Requirements

  • iOS 14
  • Scriptable version 1.5.1 (164 beta or higher)
    Join the beta here
  • Deutsche Bahn API Token
    Please create an account to get your usage token/bearer. It's necessary for searching free bikes. Insert it at the top of the script. Edit longitude & latitude as well as the area uid to get exact results.

Thanks

A big Thank you to @simonbs for making great apps like Scriptable, DataJar or Jayson.

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