Skip to content

Instantly share code, notes, and snippets.

@alexbain
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbain/d914dbff7473daaa0520 to your computer and use it in GitHub Desktop.
Save alexbain/d914dbff7473daaa0520 to your computer and use it in GitHub Desktop.
iPhone inventory checker
# Couple notes here:
# 0) Apple might change their APIs. If they do, this won't work any more.
# 1) If you want a different model iPhone, you'll have to do some sleuthing on apple.com "check availability" network requests to figure out what to look for.
import requests
import twilio
import sys
import time
import twilio.rest
account_sid = ""
auth_token = ""
def check_iphone():
sys.stdout.flush()
# 6+ 64GB space gray
# r = requests.get('http://store.apple.com/us/retailStore/availabilitySearch?parts.0=MGAU2LL%2FA&zip=94108')
# 6 64gb space gray
r = requests.get('http://store.apple.com/us/retailStore/availabilitySearch?parts.0=MG4W2LL%2FA&zip=94108')
# ['stores'][0] is downtown SF Apple store
# Also, be sure to update the part number ('MG4W2LL/A') if you change the GET request above
available = r.json()['body']['stores'][0]['partsAvailability']['MG4W2LL/A']['pickupDisplay']
if available == "available":
client = twilio.rest.TwilioRestClient(account_sid, auth_token)
for number in ["+14155551212", "+14155551213"]:
message = client.messages.create(
body="iPhone6 64GB is now in stock at SF Apple Store",
to=number,
from_="+1415551000"
)
print "AVAILABLE"
return True
else:
print "NOT AVAILABLE"
return False
while not check_iphone():
time.sleep(60)
[program:iphone_checker]
command=/home/USER/iphone-checker/venv/bin/python /home/USER/iphone-checker/checker.py
autorestart=unexpected
stdout_logfile=/var/log/iphone-checker.log
redirect_stderr=true
stopsignal=QUIT
@alexbain
Copy link
Author

The iphone_checker.conf is a supervisord conf script that will restart the script if it crashes, but not if it exits with exit code 0.

@alexbain
Copy link
Author

Couple other notes:

  • requests and twilio are external python libraries that must be installed with pip
  • The supervisord conf expects a virtualenv setup in a venv directory. If you install the pip packages globally, you can use the system python instead.

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