Skip to content

Instantly share code, notes, and snippets.

@cbruegg
Last active July 31, 2024 21:22
Show Gist options
  • Save cbruegg/62bffa3fee538880ad5a27c74229a3d8 to your computer and use it in GitHub Desktop.
Save cbruegg/62bffa3fee538880ad5a27c74229a3d8 to your computer and use it in GitHub Desktop.
Scriptable REWE Lieferservice Verfügbarkeit
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
//
@cbruegg
Copy link
Author

cbruegg commented Nov 23, 2021

@trobert2 Sadly I haven't, but that's a cool idea! From what I've seen the REWE API is pretty straightforward, just try having a look at the data flow using Firefox or Chrome Web Inspector.

@joalbrecht
Copy link

@trobert2 i'm also looking into this right now, and trying to build something on top of this, if you want to collaborate on this, feel free to reach out. @cbruegg is there any documentation available, i couldn't find any.

@cbruegg
Copy link
Author

cbruegg commented Dec 19, 2021

@joalbrecht I don't think there's any official documentation, but at least so far it looks like the API is stable.

@GoldenToast
Copy link

@cbruegg The problem is this script is just for the pickup dates not delivery. Wrong script name ;)

To do that kind of stuff you need to login first. Have you managed to login to rewe via API (OAuth, Digest etc.)?

@cbruegg
Copy link
Author

cbruegg commented Jan 22, 2022

@GoldenToast Sadly the only API calls I'm familiar with are the ones used in this script - haven't played around with the rest

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