Skip to content

Instantly share code, notes, and snippets.

@darrenfu
Created April 10, 2020 17:21
Show Gist options
  • Save darrenfu/5bb9701598e87dd69ab27b466d86d641 to your computer and use it in GitHub Desktop.
Save darrenfu/5bb9701598e87dd69ab27b466d86d641 to your computer and use it in GitHub Desktop.
iPhone stock detector
#!/usr/bin/python
import sys
import os
class IParser:
def __init__():
pass
def parse(self, doc, out_file):
self._doc = doc
self._out_file = out_file
class JSONParser(IParser):
def __init__(self):
pass
def parse(self, doc, out_file, model):
import json
out_arr = []
IParser.parse(self, doc, out_file)
jobj = json.load(doc)
nodes = jobj['body']['stores']
model_txt = None
for node in nodes:
if model_txt == None:
model_txt = node['partsAvailability'][model]['storePickupProductTitle']
if node['partsAvailability'][model]['pickupDisplay'] == 'available':
out_arr.append("%s, %s, %s" % (node['address']['address'], node['address']['address2'], node['storeDistanceVoText']))
self._out_file.write("Detect a stock at: %s, %s, %s\n" % (node['address']['address'], node['address']['address2'], node['storeDistanceVoText']))
return {"productTitle":model_txt, "availableStores":out_arr}
def send_request(url):
import urllib2
print "Downloading %s" % url
res = urllib2.urlopen(url)
return res
# main flow starts from here
def check_iphone(model, *args):
zipcode = '98033'
uri = 'https://www.apple.com/shop/retail/pickup-message?pl=true&cppart=UNLOCKED/US&parts.0=%s&location=%s'
url = uri % (model, zipcode)
res = send_request(url)
if len(args) < 0:
print('Usage: .py [zkhost:port=localhost:2181]')
sys.exit(1)
outfile = "/tmp/applestore_list.dat"
print "Generating available appstore list to: " + outfile
out = open(outfile, 'a')
# Download interesting grid batch ids via QMS service
parser = JSONParser()
jresult = parser.parse(res, out, model)
out.close()
res.close()
if len(jresult['availableStores']) > 0:
print("Detect an available iphone! SMS ...")
from twilio.rest import Client
account_sid='ACfdc83d84cb014c7a7acb1a5fbed5dfbf'
auth_token='c2f457a0d79a538713ecbf2bda815721'
client = Client(account_sid, auth_token)
client.api.account.messages.create(to="+14253244679", from_="+14252304220", body= "%s\n\n%s" % (jresult['productTitle'], ";\n".join(jresult['availableStores'])))
if __name__ == "__main__":
check_iphone('MQ8D2LL/A', *sys.argv) # Grey
check_iphone('MQ8E2LL/A', *sys.argv) # Silver
check_iphone('MQ8F2LL/A', *sys.argv) # Rose gold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment