Last active
July 31, 2024 21:22
-
-
Save cbruegg/62bffa3fee538880ad5a27c74229a3d8 to your computer and use it in GitHub Desktop.
Scriptable REWE Lieferservice Verfügbarkeit
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
let zipCode = "33100" | |
let widget = new ListWidget() | |
widget.setPadding(8, 8, 8, 8) | |
widget.url = "https://shop.rewe.de/" | |
await createWidget() | |
Script.setWidget(widget) | |
Script.complete() | |
if (config.runsInApp) { | |
widget.presentSmall() | |
} | |
async function createWidget() { | |
const data = await fetchMarketsWithAvailability(3) | |
const upperStack = widget.addStack() | |
upperStack.layoutVertically() | |
let logo = upperStack.addText("REWE") | |
logo.font = Font.boldSystemFont(14) | |
logo.textColor = Color.red() | |
upperStack.addSpacer(4) | |
let dayText = upperStack.addText("Stand: " + formatDate(new Date().toISOString(), true)) | |
dayText.textColor = Color.dynamic(Color.black(), Color.white()) | |
dayText.font = Font.boldSystemFont(6) | |
for (marketWithAvailability of data) { | |
upperStack.addSpacer(8) | |
let name = upperStack.addText(marketWithAvailability.name) | |
name.textColor = Color.dynamic(Color.black(), Color.white()) | |
name.font = Font.regularSystemFont(10) | |
let availability = upperStack.addText(marketWithAvailability.availability) | |
availability.textColor = Color.dynamic(Color.black(), Color.white()) | |
availability.font = Font.boldSystemFont(10) | |
} | |
widget.addSpacer(8) | |
} | |
async function fetch(url) { | |
return { | |
json: async function() { | |
return new Request(url).loadJSON(); | |
} | |
} | |
} | |
async function fetchMarketsWithAvailability(limit) { | |
let markets = await (await fetch(`https://shop.rewe.de/api/marketselection/zipcodes/${zipCode}/services/pickup`)).json() | |
markets.sort((a, b) => (a.distance > b.distance) ? 1 : -1); | |
markets = markets.slice(0, Math.min(limit, markets.length)) | |
let promises = markets.map(async (market) => { | |
const json = await (await fetch(`https://shop.rewe.de/api/timeslots/pickup/${market.wwIdent}/next-bookable`)).json() | |
const niceAvailability = formatDate(json.startTime, false) | |
return { | |
name: market.companyName, | |
availability: niceAvailability, | |
toString: function() { | |
return market.companyName + " / " + niceAvailability | |
} | |
} | |
}) | |
const result = [] | |
for (promise of promises) { | |
result.push(await promise) | |
} | |
return result | |
} | |
function formatDate(str, withMs) { | |
const formatter = new DateFormatter() | |
formatter.dateFormat = withMs ? "yyyy-MM-dd'T'HH:mm:ss.SSSZ" : "yyyy-MM-dd'T'HH:mm:ssZ" | |
const date = formatter.date(str) | |
formatter.useShortDateStyle() | |
formatter.useShortTimeStyle() | |
return formatter.string(date) | |
} | |
// | |
// end of script, make sure to copy everything | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@GoldenToast Sadly the only API calls I'm familiar with are the ones used in this script - haven't played around with the rest