Last active
February 16, 2024 09:53
-
-
Save marco79cgn/a0b1b707abfc51662a1467f7cb70f19c to your computer and use it in GitHub Desktop.
iOS Widget für die Scriptable App, das den aktuellen Bestand von Smart #1 und #3 Fahrzeugen anzeigt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: blue; icon-glyph: car-alt; | |
let productId = 'HC1H2D3B61EU011295' // Produktnummer des gewünschten Fahrzeugs | |
let param = args.widgetParameter | |
if (param != null && param.length > 0) { | |
productId = param | |
} | |
const carImageFileName = productId + ".png" | |
const widget = new ListWidget() | |
widget.url = 'https://de.smart.com/de/product-details/?pn18=' + productId | |
const vehicleData = await fetchStoreAmount() | |
await createWidget() | |
// used for debugging if script runs inside the app | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
// build the content of the widget | |
async function createWidget() { | |
widget.addSpacer(4) | |
const logoImg = await getImage('smart-logo.png') | |
const logoStack = widget.addStack() | |
const logoImageStack = logoStack.addStack() | |
logoStack.layoutHorizontally() | |
const wimg = logoImageStack.addImage(logoImg) | |
wimg.imageSize = new Size(40, 9) | |
wimg.rightAlignImage() | |
console.log(carImageFileName) | |
const icon = await getImage(carImageFileName) | |
let row = widget.addStack() | |
row.layoutHorizontally() | |
row.addSpacer() | |
const iconImg = row.addImage(icon) | |
iconImg.imageSize = new Size(100,57) | |
iconImg.rightAlignImage() | |
widget.addSpacer(4) | |
const productName = widget.addText(vehicleData.name) | |
productName.font = Font.boldRoundedSystemFont(12) | |
const exterior = widget.addText(vehicleData.exterior[0].name) | |
exterior.font = Font.regularSystemFont(10) | |
const roof = widget.addText(vehicleData.exterior[0].roofDescription) | |
roof.font = Font.regularSystemFont(10) | |
const interior = widget.addText(vehicleData.interior[0].name) | |
interior.font = Font.regularSystemFont(10) | |
widget.addSpacer(1) | |
const availability = widget.addStack() | |
availability.layoutHorizontally() | |
const paperText = availability.addText("Bestand: ") | |
paperText.font = Font.semiboldRoundedSystemFont(12) | |
const stockAmount = availability.addText(vehicleData.stock.stockLevel.toString() + " Stück") | |
stockAmount.font = Font.boldRoundedSystemFont(12) | |
if (vehicleData.stock.stockLevel < 5) { | |
stockAmount.textColor = new Color("#E50000") | |
} else { | |
stockAmount.textColor = new Color("#00CD66") | |
} | |
const lastUpdateDate = new Date().toLocaleTimeString("de-DE", { timeZone: "CET", hour: '2-digit', minute: '2-digit' }) | |
let lastUpdatedText = widget.addText("Stand: " + lastUpdateDate + " Uhr") | |
lastUpdatedText.textColor = Color.gray() | |
lastUpdatedText.font = Font.mediumSystemFont(9) | |
lastUpdatedText.centerAlignText() | |
// widget.addSpacer() | |
} | |
// fetches information of the configured store, e.g. opening hours, address etc. | |
async function fetchStoreAmount() { | |
let url = 'https://de.smart.com/__app__/product-summary/prod/bff/product-model?envName=prod&preview=false&marketId=de&language=de&productId=' + productId | |
let req = new Request(url) | |
let apiResult = await req.loadJSON() | |
return apiResult | |
} | |
// get images from local filestore or download them once | |
async function getImage(image) { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(dir, image) | |
if (fm.fileExists(path)) { | |
return fm.readImage(path) | |
} else { | |
// download once | |
let imageUrl | |
if (image === 'smart-logo.png') { | |
imageUrl = "https://i.imgur.com/MM0UI31.png" | |
} else { | |
imageUrl = "https://s7.future.smart.com/is/image/smarteurope/ppo_" + productId + ":16-9?$smartResponsiveHiDPI$&wid=400&hei=232"; | |
} | |
let iconImage = await loadImage(imageUrl) | |
fm.writeImage(path, iconImage) | |
return iconImage | |
} | |
} | |
// helper function to download an image from a given url | |
async function loadImage(imgUrl) { | |
const req = new Request(imgUrl) | |
return await req.loadImage() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intro
Das Smart #1 Widget zeigt den aktuellen Bestand des verfügbaren Modells an. Ein Klick auf das Widget öffnet die Produktseite des konfigurierten Autos im Browser (Safari) zum schnellen bestellen.
Anforderungen
Installation
Product IDs der Modelle
Brabus Line:
Brabus / Meta Black Metallic / Radiant Red / B52 Brabus
Brabus / Digital White Metallic / Eclipse Black / B52 Brabus
Brabus / Laser Red Metallic / Eclipse Black / B52 Brabus
Brabus / Cyber Silver Metallic / Eclipse Black / B52 Brabus
Brabus / Meta Black Metallic / Unifarben / B52 Brabus
Brabus / Atom Grey-Matte / Unifarben / B52 Brabus
Brabus / Atom Grey-Matte / Radiant Red / B52 Brabus
Brabus / Cyber Silver Metallic / Unifarben / B52 Brabus
Premium Line:
Premium / Meta Black Metallic / Unifarben / Touch
Premium / Digital White Metallic / Eclipse Black / Touch
Premium / Future Green Metallic / Eclipse Black / Dark Matter
Premium / Laser Red Metallic / Eclipse Black / Dark Matter
Premium / Cyber Silver Metallic / Eclipse Black / Dark Matter
Premium / Lumen Yellow / Eclipse Black / Dark Matter
Premium / Quantum Blue Metallic / Eclipse Black / Dark Matter
Premium / Meta Black Metallic / Unifarben / Dark Matter
Premium / Solar Silver Metallic / Unifarben / Dark Matter
Premium / Solar Silver Metallic / Unifarben / Touch
Premium / Atom Grey-Matte / Unifarben / Dark Matter
Premium / Atom Grey-Matte / Unifarben / Touch
Premium / Digital White Metallic / Unifarben / Dark Matter
Premium / Digital White Metallic / Eclipse Black / Dark Matterl
Pro+ Line:
Pro+ / Meta Black Metallic / Yellow / Lightning
Pro+ / Cyber Silver Metallic / Eclipse Black / After Dark
Pro+ / Laser Red Metallic / Eclipse Black / After Dark
Pro+ / Lumen Yellow / Eclipse Black / Lightning
Pro+ / Quantum Blue Metallic / Eclipse Black / After Dark
Pro+ / Meta Black Metallic / Unifarben / After Dark
Pro+ / Digital White Metallic / Eclipse Black / After Dark
Pro+ / Future Green Metallic / Eclipse Black / After Dark
Disclaimer
Es handelt sich um ein von mir selbst entwickeltes Spaßprojekt, kein offizielles Produkt der Marke smart. Ich stehe in keinerlei Beziehung zu smart und bekomme weder Provision noch sonstiges.
Updates
28.12.2023, 23:21
Support für Smart #3
08.02.2023, 13:45
Farbe des Dachs wird jetzt angezeigt
27.01.2023, 15:52
Erste Version