Last active
December 20, 2020 16:54
-
-
Save p0fi/ba0173fa0d218eb7f086d477ce9d870e to your computer and use it in GitHub Desktop.
Check the Availability of HomePod Minis Is an Apple Store of Your Choice
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: deep-green; icon-glyph: magic; | |
/* | |
Apple Store-IDs: | |
Germany | |
R358 Berlin | |
R366 Hamburg (Alstertal) | |
R396 Hamburg (Jungfernstieg) | |
R519 Sindelfingen | |
R431 Augsburg | |
R352 Frankfurt | |
R434 Sulzbach | |
R521 München (OEZ) | |
R045 München (Rosenstraße) | |
R559 Köln (Schildergasse) | |
R520 Köln (Rhein Center) | |
R331 Düsseldorf | |
R403 Oberhausen | |
R455 Hannover | |
R430 Dresden | |
*/ | |
const STORE_ID = 'R519'; | |
const DARK_BG = new Color('#101010'); | |
const DARK_TEXT = new Color('#E3E3E3'); | |
const LIGHT_BG = new Color('#F5F5F5'); | |
const LIGHT_TEXT = new Color('#000000'); | |
const img2 = new Request('https://i.ibb.co/7bCs78q/homepod.png'); | |
const homepod = await img2.loadImage(); | |
if (config.runsInWidget) { | |
const size = config.widgetFamily; | |
const widget = await createWidget(size); | |
Script.setWidget(widget); | |
Script.complete(); | |
} else { | |
// choose any size for debugging | |
const size = 'small'; | |
//const size = 'medium'; | |
//const size = 'large' | |
const widget = await createWidget(size); | |
if (size == 'small') { | |
widget.presentSmall(); | |
} else if (size == 'medium') { | |
widget.presentMedium(); | |
} else { | |
widget.presentLarge(); | |
} | |
Script.complete(); | |
} | |
async function createWidget(size) { | |
const data = await fetchData(); | |
if (data.notify) { | |
const notification = new Notification(); | |
notification.title = 'HomePod Mini'; | |
notification.body = data.availability; | |
notification.schedule(); | |
} | |
const widget = new ListWidget(); | |
widget.backgroundColor = Color.dynamic(LIGHT_BG, DARK_BG); | |
const vstack = widget.addStack(); | |
vstack.layoutVertically(); | |
// shop name stack | |
const shopStack = vstack.addStack(); | |
shopStack.layoutHorizontally(); | |
const bagImg = SFSymbol.named('bag.fill').image; | |
const bagIcon = shopStack.addImage(bagImg); | |
bagIcon.tintColor = Color.dynamic(LIGHT_TEXT, DARK_TEXT); | |
bagIcon.imageSize = new Size(18, 18); | |
shopStack.addSpacer(5); | |
const storeName = shopStack.addText(data.store); | |
vstack.addSpacer(); | |
// homepod image stack | |
const imageStack = vstack.addStack(); | |
imageStack.layoutHorizontally(); | |
imageStack.addSpacer(); | |
const rest = imageStack.addImage(homepod); | |
// rest.imageSize = new Size(70, 70); | |
imageStack.addSpacer(); | |
vstack.addSpacer(); | |
// avaliablity string stack | |
const availabilityStack = vstack.addStack(); | |
availabilityStack.layoutHorizontally(); | |
availabilityStack.addSpacer(); | |
const availabilityString = availabilityStack.addText(data.availability); | |
availabilityString.font = Font.systemFont(9); | |
availabilityStack.addSpacer(); | |
vstack.addSpacer(); | |
// last updated stack | |
const updatedStack = vstack.addStack(); | |
updatedStack.layoutHorizontally(); | |
const updatedImg = SFSymbol.named('arrow.clockwise').image; | |
updatedStack.addSpacer(); | |
const updatedIcon = updatedStack.addImage(updatedImg); | |
updatedIcon.tintColor = Color.dynamic(LIGHT_TEXT, DARK_TEXT); | |
updatedIcon.imageSize = new Size(9, 9); | |
updatedStack.addSpacer(5); | |
var currentdate = new Date(); | |
const dateString = updatedStack.addText(currentdate.toLocaleTimeString()); | |
dateString.font = Font.systemFont(8); | |
return widget; | |
} | |
async function fetchData() { | |
let url = `https://www.apple.com/de/shop/retail/pickup-message?pl=true&store=${STORE_ID}&parts.0=MY5G2D%2FA`; | |
let req = new Request(url); | |
let json = await req.loadJSON(); | |
console.log(json); | |
const availability = json.body.stores[0].partsAvailability['MY5G2D/A'].pickupSearchQuote; | |
const store = json.body.stores[0].storeName; | |
const notify = json.body.stores[0].partsAvailability['MY5G2D/A'].pickupDisplay == 'available' ? true : false; | |
return { | |
store: store, | |
availability: availability, | |
notify: notify, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment