Skip to content

Instantly share code, notes, and snippets.

@seungjin
Last active August 29, 2015 14:07
Show Gist options
  • Save seungjin/222627e7fa6cdb30c409 to your computer and use it in GitHub Desktop.
Save seungjin/222627e7fa6cdb30c409 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
availability_resource_url = 'https://reserve.cdn-apple.com/CN/zh_CN/reserve/iPhone/availability.json'
store_resource_url = 'https://reserve.cdn-apple.com/CN/zh_CN/reserve/iPhone/stores.json'
iphone_model_name = {
"MG4C2CH/A" : "iPhone6 128G(银色)",
"MGAJ2CH/A" : "iPhone6 Plus 64G(银色)",
"MGAA2CH/A" : "iPhone6 Plus 16G(金色)",
"MG4J2CH/A" : "iPhone6 64G(金色)",
"MG472CH/A" : "iPhone6 16G(深空灰)",
"MGAH2CH/A" : "iPhone6 Plus 64G(深空灰)",
"MG4H2CH/A" : "iPhone6 64G(银色)",
"MGA92CH/A" : "iPhone6 Plus 16G(银色)",
"MGA82CH/A" : "iPhone6 Plus 16G(深空灰)",
"MG4A2CH/A" : "iPhone6 128G(深空灰)",
"MG4E2CH/A" : "iPhone6 128G(金色)",
"MGAE2CH/A" : "iPhone6 Plus 128G(银色)",
"MG482CH/A" : "iPhone6 16G(银色)",
"MGAF2CH/A" : "iPhone6 Plus 128G(金色)",
"MG492CH/A" : "iPhone6 16G(金色)",
"MGAK2CH/A" : "iPhone6 Plus 64G(金色)",
"MG4F2CH/A" : "iPhone6 64G(深空灰)",
"MGAC2CH/A" : "iPhone6 Plus 128G(深空灰)"
}
def get_store_info(store_id,store_json):
for store in store_json['stores']:
if store['storeNumber'] == store_id:
return store
def check_availability(want=None):
availability_json = requests.get(availability_resource_url).text
store_json = json.loads(requests.get(store_resource_url).text)
for attribute, value in json.loads(availability_json).iteritems():
if attribute != 'updated':
for model, availability in value.iteritems():
if availability == True:
store_info=get_store_info(attribute, store_json)
print "{0}[{1}] is in The Apple Store {2}({3})".format(iphone_model_name[model],model, store_info['storeName'].encode('utf-8'),attribute)
if model == want and availability == True:
send_alert("{0}[{1}] is in The Apple Store {2}({3})".format(iphone_model_name[model],model, store_info['storeName'].encode('utf-8'),attribute))
def send_alert(text):
send_from = ""
send_to = [""]
subject = text
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
smtp = smtplib.SMTP("localhost")
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
if __name__ == "__main__":
check_availability("MGAF2CH/A")
#check_availability("MG472CH/A")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment