Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active September 24, 2023 11:25
Show Gist options
  • Save marco79cgn/1184e77b39fdf7ba510ac8650afe0674 to your computer and use it in GitHub Desktop.
Save marco79cgn/1184e77b39fdf7ba510ac8650afe0674 to your computer and use it in GitHub Desktop.
Bash script that checks whether an Apple product is available for pickup at your nearest Apple Store and sends a push notification if it gets available again (via ntfy.sh)
#!/bin/bash
partNo=$1
storeId=$2
notifyUrl=https://ntfy.sh/$3
FILE=$4/${partNo/\//-}-$storeId
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist."
echo -n "1" > $FILE
fi
productName=""
function curlItem {
apiResult=$(/usr/bin/curl -s "https://www.apple.com/de/shop/fulfillment-messages?store=${storeId}&little=false&parts.0=${partNo}&purchaseOption=fullPrice&mts.0=regular&mts.1=sticky&fts=true")
productName=$(echo -n ${apiResult} | jq -r ".body.content.pickupMessage.stores[0].partsAvailability.\"${partNo}\".messageTypes.regular.storePickupProductTitle")
storeAvailability=$(echo -n ${apiResult} | jq -r ".body.content.pickupMessage.stores[0].partsAvailability.\"${partNo}\".pickupDisplay" | grep -i -w "available" | wc -l)
printf "Store ${storeId}: ${storeAvailability}"
if [ "$storeAvailability" -eq "1" ]; then
previousState=$(<$FILE)
if [[ "$previousState" == 0 ]]; then
sendPush
echo -n "1" > $FILE
exit
fi
else
echo -n "0" > $FILE
fi
}
function sendPush {
result=$(/usr/bin/curl -s -X POST -H "Title: Apple Store" -H "Click: https://store.apple.com/de/xc/product/$partNo" -H "Tags: apple" -d "${productName} ist jetzt verfügbar! (Tap to order)" $notifyUrl)
}
responsedata=$(curlItem)
echo $responsedata
@marco79cgn
Copy link
Author

marco79cgn commented Nov 1, 2021

Auf dem iPhone selbst geht es am einfachsten über die Apple Store app. Produkt öffnen, dann oben rechts auf den share button drücken. Im kopierten Link steht dann die Produkt Nummer am Ende der URL. Die muss dann im Skript oben hinter "parts.0=" eingefügt werden.

Beispiel:
AirPods (3. Generation)

Link aus Apple Store app:
https://store.apple.com/de/xc/product/MME73ZM/A

@Manu8D
Copy link

Manu8D commented Sep 22, 2023

Vielen Dank für das Script! Ich habe damit eben erfolgreich mein iPhone zur Abholung für morgen bestellt. Eine kleine Anmerkung: Bei mir hat das Script auf dem Raspberry Pi dauerhaft angeschlagen, weil "grep -i "available" auch "unavailable" gefunden hat. Mit "grep -i -w "available" hat es dann aber funktioniert.

@marco79cgn
Copy link
Author

marco79cgn commented Sep 22, 2023

@Manu8D
Ach spannend, Danke für‘s Feedback und natürlich Glückwunsch zum Kauf.

Ich hatte mich kürzlich erst gefragt, wie mein 2 Jahre alter Code eigentlich funktionieren kann aus genau dem Grund, den du genannt hast. Aber bei mir funktioniert er tatsächlich so, weil die Anführungszeichen explizit Teil der Ausgabe und des greps sind. Daher matcht „available“ nicht auf „unavailable“. Anders wäre es, wenn ich jq -r (raw) machen würde, denn dann werden die Anführungszeichen unterdrückt. Ich teste nochmal mit deiner Lösung und passe dann das Skript an, wenn‘s klappt.

//EDIT:
Skript ist angepasst!

@marco79cgn
Copy link
Author

Update 24.09.2023

Das Skript ist jetzt komplett universell benutzbar und muss nicht mehr editiert werden. Beim Aufruf müssen die benötigten 4 Parameter lediglich mitgegeben werden: partNo storeId notifyTopic tempFolder

Zum Beispiel für das iPhone 15 Pro Max 256 GB Titan Blau im Apple Store Köln Schildergasse:
./apple-availability-check.sh "MU7A3ZD/A" "R559" "my-secret-apple-alerts-395920" "/Users/marco/dev/temp"

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