Skip to content

Instantly share code, notes, and snippets.

@paulborm
Last active October 17, 2021 09:44
Show Gist options
  • Save paulborm/061866368c14e056369a3c663ffa3721 to your computer and use it in GitHub Desktop.
Save paulborm/061866368c14e056369a3c663ffa3721 to your computer and use it in GitHub Desktop.
Scriptable Widget for viewing your dm Fotoparadies order status directly on your iOS homescreen.
/**
* @license
* Copyright (c) 2021 Paul Borm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
"use strict";
const __DEV__ = false;
const parameters = getWidgetParameters();
const orderNumber = parameters ? parameters[0] : null;
const shopNumber = parameters ? parameters[1] : null;
let widgetData = {
summaryStateText: null,
summaryDate: null,
orderNo: null,
summaryPriceText: null,
error: false,
};
try {
const response = await fetchOrderStatus(shopNumber, orderNumber);
widgetData = {
summaryStateText: response.summaryStateText,
summaryDate: response.summaryDate,
orderNo: response.orderNo,
summaryPriceText: response.summaryPriceText,
error: response.error,
};
} catch (error) {
widgetData = {
error: !!error,
};
}
const widget = createWidget(widgetData);
if (__DEV__) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function fetchOrderStatus(shop, order) {
const request = new Request(
`https://spot.photoprintit.com/spotapi/orderInfo/forShop?config=1320&shop=${shop}&order=${order}&language=de`
);
const response = await request.loadJSON();
return response;
}
function createWidget({
summaryStateText,
summaryDate,
orderNo,
summaryPriceText,
error,
} = {}) {
const widget = new ListWidget();
widget.setPadding(10, 10, 10, 10);
widget.backgroundColor = new Color("#0e204e");
if (error) {
const errorHeadline = widget.addText("⚠️ Fehler.");
errorHeadline.font = Font.headline();
errorHeadline.textColor = Color.white();
} else {
const stateText = widget.addText(summaryStateText);
stateText.font = Font.headline();
stateText.textColor = Color.white();
widget.addSpacer(5);
const orderNoText = widget.addText(`Auftr.-Nr. : ${orderNo}`);
orderNoText.font = Font.caption2();
orderNoText.textColor = Color.white();
const dateText = widget.addText(
`Status vom: ${new Date(summaryDate).toLocaleDateString()}`
);
dateText.font = Font.caption2();
dateText.textColor = Color.white();
if (!!summaryPriceText) {
widget.addSpacer(5);
const priceText = widget.addText(`Preis: ${summaryPriceText}`);
priceText.font = Font.subheadline();
priceText.textColor = Color.white();
}
}
return widget;
}
function getWidgetParameters(char = ",") {
if (!args.widgetParameter) {
return null;
}
return args.widgetParameter.replaceAll(" ", "").split(char);
}
@paulborm
Copy link
Author

Configure the Scriptable widget like this (screenshot):

grafik

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