Skip to content

Instantly share code, notes, and snippets.

@dakira
Forked from marco79cgn/dm-toilet-paper-wheat-flour.js
Last active March 12, 2021 00:51
Show Gist options
  • Save dakira/f3c7a138b0fac1212f832c103e10e0db to your computer and use it in GitHub Desktop.
Save dakira/f3c7a138b0fac1212f832c103e10e0db to your computer and use it in GitHub Desktop.
ios widget to show number of diapers at dm market
// dm Windel Widget
// Basiert auf
// dm Klopapier Widget
// https://gist.github.com/marco79cgn/23ce08fd8711ee893a3be12d4543f2d2
//
// Copyright (C) 2020 by marco79 <marco79cgn@gmail.com>
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
let storeId = 1613
let param = args.widgetParameter
if (param != null && param.length > 0) {
storeId = param
}
const widget = new ListWidget()
const storeInfo = await fetchStoreInformation()
const storeCapacityDiapers4 = await fetchAmountOfDiapers4()
const storeCapacityDiapers5 = await fetchAmountOfDiapers5()
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() {
const logoImg = await getImage('dm-logo.png')
widget.setPadding(6,6,6,6)
const titleFontSize = 12
const detailFontSize = 36
const logoStack = widget.addStack()
const shopStateStack = logoStack.addStack()
shopStateStack.layoutVertically()
shopStateStack.addSpacer(10)
let currentTime = new Date().toLocaleTimeString('de-DE', { hour: "numeric", minute: "numeric" })
let currentDay = new Date().getDay()
let isOpen
if (currentDay > 0) {
const todaysOpeningHour = storeInfo.openingHours[currentDay-1].timeRanges[0].opening
const todaysClosingHour = storeInfo.openingHours[currentDay-1].timeRanges[0].closing
const range = [todaysOpeningHour, todaysClosingHour];
isOpen = isInRange(currentTime, range)
} else {
isOpen = false
}
let shopStateText
if (isOpen) {
shopStateText = shopStateStack.addText('GEÖFFNET')
shopStateText.textColor = new Color("#00CD66")
logoStack.addSpacer(36)
} else {
shopStateText = shopStateStack.addText('GESCHLOSSEN')
shopStateText.textColor = new Color("#E50000")
logoStack.addSpacer(10)
}
shopStateText.font = Font.boldRoundedSystemFont(12)
const logoImageStack = logoStack.addStack()
logoStack.layoutHorizontally()
logoImageStack.backgroundColor = new Color("#ffffff", 1.0)
logoImageStack.cornerRadius = 8
const wimg = logoImageStack.addImage(logoImg)
wimg.imageSize = new Size(32,32)
wimg.rightAlignImage()
widget.addSpacer(6)
// size 4 diapers
const diapers4Icon = await getImage('windeln4.jpg')
let row = widget.addStack()
row.layoutHorizontally()
row.addSpacer(2)
const diapers4Img = row.addImage(diapers4Icon)
diapers4Img.imageSize = new Size(30,30)
row.addSpacer(13)
let column = row.addStack()
column.layoutVertically()
const diapers4Text = column.addText("WINDELN Gr4")
diapers4Text.font = Font.mediumRoundedSystemFont(11)
const diapers4PackageCount = column.addText(storeCapacityDiapers4.toString())
diapers4PackageCount.font = Font.mediumRoundedSystemFont(18)
if (storeCapacityDiapers4 < 30) {
diapers4PackageCount.textColor = new Color("#E50000")
} else {
diapers4PackageCount.textColor = new Color("#00CD66")
}
widget.addSpacer(4)
// diapers size 5
const diapers5Icon = await getImage('windeln5.jpg')
let row2= widget.addStack()
row2.layoutHorizontally()
row2.addSpacer(3)
const diapers5Img = row2.addImage(diapers5Icon)
diapers5Img.imageSize = new Size(30,30)
row2.addSpacer(12)
let column2 = row2.addStack()
column2.layoutVertically()
const diapers5Text = column2.addText("WINDELN Gr5")
diapers5Text.font = Font.mediumRoundedSystemFont(11)
const diapers5PackageCount = column2.addText(storeCapacityDiapers5.toString())
diapers5PackageCount.font = Font.mediumRoundedSystemFont(18)
if (storeCapacityDiapers5 < 30) {
diapers5PackageCount.textColor = new Color("#E50000")
} else {
diapers5PackageCount.textColor = new Color("#00CD66")
}
widget.addSpacer(4)
// shop info
const row3 = widget.addStack()
row3.layoutVertically()
const street = row3.addText(storeInfo.address.street)
street.font = Font.regularSystemFont(10)
const zipCity = row3.addText(storeInfo.address.zip + " " + storeInfo.address.city)
zipCity.font = Font.regularSystemFont(10)
}
// fetches the amount of size 4 diapers
async function fetchAmountOfDiapers4() {
let url
let counter = 0
url = 'https://products.dm.de/store-availability/DE/availability?dans=594439&storeNumbers=' + storeId
const req = new Request(url)
const apiResult = await req.loadJSON()
for (var i in apiResult.storeAvailabilities) {
counter += apiResult.storeAvailabilities[i][0].stockLevel
}
return counter
}
// fetches the amount of size 5 diapers
async function fetchAmountOfDiapers5() {
let url
let counter = 0
url = 'https://products.dm.de/store-availability/DE/availability?dans=594442&storeNumbers=' + storeId
const req = new Request(url)
const apiResult = await req.loadJSON()
for (var i in apiResult.storeAvailabilities) {
counter += apiResult.storeAvailabilities[i][0].stockLevel
}
return counter
}
// fetches information of the configured store, e.g. opening hours, address etc.
async function fetchStoreInformation() {
let url
url = 'https://store-data-service.services.dmtech.com/stores/item/de/' + storeId
widget.url = 'https://www.dm.de/search?query=Windeln%20nature%20Gr.&searchType=product'
let req = new Request(url)
let apiResult = await req.loadJSON()
return apiResult
}
// checks whether the store is currently open or closed
function isInRange(value, range) {
return value >= range[0] && value <= range[1];
}
// 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
switch (image) {
case 'dm-logo.png':
imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Dm_Logo.svg/300px-Dm_Logo.svg.png"
break
case 'windeln4.jpg':
imageUrl = "https://media.dm-static.com/images/f_auto,q_auto,c_fit,w_30,h_30/v1615441984/products/pim/4058172771699-2572784/babylove-windeln-nature-gr-4-maxi-8-14-kg"
break
case 'windeln5.jpg':
imageUrl = "https://media.dm-static.com/images/f_auto,q_auto,c_fit,w_30,h_30/v1600100889/products/pim/4058172458125-2245099/babylove-windeln-nature-gr-5-junior-12-25-kg"
break
default:
console.log(`Sorry, couldn't find ${image}.`);
}
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()
}
@dakira
Copy link
Author

dakira commented Mar 12, 2021

Nur eine einfache Kopie vom Klopapierwidget. Dort ist auch die Anleitung zur Nutzung: https://gist.github.com/marco79cgn/23ce08fd8711ee893a3be12d4543f2d2

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