Monday, July 21, 2008

iPhone availability

UPDATE: Looks like apple changed the link to a PHP page. Not sure of all the details, maybe they are actually checking the time on the server end instead of just relying on some javascript. Oh well, I got mine. :-p

So my contract is finally up with Sprint so I can now leave them without paying any penalties. Avoid sprint like the plague. Nextel was decent until they merged with Sprint, but that's a much longer rant than the cool info you'll find below.

So I've decided to ditch Sprint in favor of AT&T, primarily so I can get an iPhone. Apple has an iPhone stock bit on their web page. Go there after 9pm and you can see if a store is expected to have an iPhone in stock the next day. I do not always get on my computer after 9 for whatever reason so I thought I would script something to just scrape the page and see if the store will have it available. I found something even better.

Go ahead, find a store near you and look at the source, I'll wait. See it? Yes, that bit of javascript where it calls:

That file appears to not only have if there will be an iPhone available, it also has which models it expects to have available. The only other thing I could wish for would be the quantity, but now I'm expecting too much.

So what do we do with it? I'll show you what I did. First, we'll use python because it's cool and makes stuff like this easy. Second, since it's a json formatted file, good choice apple, we'll use simplejson. Got all that? Good, let's script this sucker.

import simplejson, urllib
url = u''
availability = simplejson.load(urllib.urlopen(url))

Ok, that basically downloads the json file and parses it into a nice little python data structure which we called availability. Now, I'm really only interested in certain stores in NJ. So, I do a loop like the following:

for store in availability['locations']['NJ']:
print store['city']

There seems to be available: url, city, storeid, available. Available then lists each type of iPhone (white16, black8, black16) and a true/false if it is available or not. So go forth and script to your heart's content. To be nice, here my final script:

import simplejson, urllib
import smtplib


url = u''
availability = simplejson.load(urllib.urlopen(url))

interested_stores = [u'Rockaway', u'Short Hills', u'Bridgewater']

msg = "From: %s\r\nTo: %s\r\nSubject: iPhone availability\r\n\r\n" % (EMAIL_FROM, EMAIL_TO)
has_iphone = False

for store in availability['locations']['NJ']:
if store['city'] in interested_stores:
if store['available']['black16']:
msg += u'%s has black 16Gb iPhones available\n' % (store['city'])
has_iphone = True

if has_iphone:
server = smtplib.SMTP('', 587)
server.sendmail(EMAIL_FROM, EMAIL_TO, msg)

No comments: