Guest User

Untitled

a guest
Sep 27th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import json, urllib, urllib2, base64, time
  4.  
  5. ## [readme]
  6. # (1). Install python 2.7 (this script will not work with python 3+)
  7. # (2). Install PushBullet to your mobile device's + set it up (takes few minutes)
  8. # - On PC, login to pushbullet.com and go to Account Settings, copy your API key and paste it into pushbullet_Key below
  9. # (3). Look at stores variable below to find the exact names of the stores you want to be checked, enter their names exactly as shown into allowedStores below (use a comma for multiple stores)
  10. # - DO NOT CHANGE stores/appleStores/appleStock URL's unless you know what you are doing / modfying this script for alternative locations (Canada?)
  11. # (4). Look at the models variable in the Vars section below, locate all of the model codes that you want to be notified of when they are in stock at the stores chosen
  12. # - Enter them into the wantedModels variable..
  13. # (5). Change nextCheck to how long to wait until performing the next check (this is in seconds, 60seconds is recommended!)
  14. # (6). Run script and leave running, as soon as there is stock for the chosen stores/models a PUSH notifation will be sent to all of your devices that are linked with the specified pushbullet account!
  15. #
  16. #
  17. # (*) - See below as an example of a configuration that checks the Basingstake/Southampton/Reading stores for ANY iPhone 6+ stock every 60seconds
  18. ## [/readme]
  19.  
  20. ## Config
  21. pushbullet_Key = "xxx"
  22. allowedStores = "Victoria Square"
  23. wantedModels = {
  24. # 7+ 256GB
  25. "MN4W2" : True,
  26. };
  27. nextCheck = 60
  28.  
  29. ## Vars
  30. appleStores = "https://reserve.cdn-apple.com/GB/en_GB/reserve/iPhone/stores.json"
  31. appleStock = "https://reserve.cdn-apple.com/GB/en_GB/reserve/iPhone/availability.json"
  32. stores = {
  33. "R659" : "Apple Watch at Selfridges",
  34. "R227" : "Bentall Centre",
  35. "R113" : "Bluewater",
  36. "R340" : "Braehead",
  37. "R163" : "Brent Cross",
  38. "R496" : "Bromley",
  39. "R135" : "Buchanan Street",
  40. "R118" : "Bullring",
  41. "R252" : "Cabot Circus",
  42. "R391" : "Chapelfield",
  43. "R244" : "Churchill Square",
  44. "R245" : "Covent Garden",
  45. "R393" : "Cribbs Causeway",
  46. "R545" : "Drake Circus",
  47. "R341" : "Eldon Square",
  48. "R482" : "Festival Place",
  49. "R270" : "Grand Arcade",
  50. "R308" : "Highcross",
  51. "R242" : "Lakeside",
  52. "R239" : "Liverpool ONE",
  53. "R215" : "Manchester Arndale",
  54. "R153" : "Meadowhall",
  55. "R423" : "Metrocentre",
  56. "R269" : "Milton Keynes",
  57. "R328" : "Princes Street",
  58. "R279" : "Princesshay",
  59. "R092" : "Regent Street",
  60. "R335" : "SouthGate",
  61. "R334" : "St David's 2",
  62. "R410" : "Stratford City",
  63. "R176" : "The Oracle",
  64. "R255" : "Touchwood Centre",
  65. "R136" : "Trafford Centre",
  66. "R372" : "Trinity Leeds",
  67. "R363" : "Union Square",
  68. "R313" : "Victoria Square",
  69. "R527" : "Watford",
  70. "R174" : "WestQuay",
  71. "R226" : "White City"
  72. };
  73. models = { # iPhone 7
  74. "MN962" : "iPhone 7 Jet Black 128GB",
  75. "MN9C2" : "iPhone 7 Jet Black 256GB",
  76. "MN8X2" : "iPhone 7 Black 32GB",
  77. "MN922" : "iPhone 7 Black 128GB",
  78. "MN972" : "iPhone 7 Black 256GB",
  79. "MN8Y2" : "iPhone 7 Silver 32GB",
  80. "MN932" : "iPhone 7 Silver 128GB",
  81. "MN982" : "iPhone 7 Silver 256GB",
  82. "MN902" : "iPhone 7 Gold 32GB",
  83. "MN942" : "iPhone 7 Gold 128GB",
  84. "MN992" : "iPhone 7 Gold 256GB",
  85. "MN912" : "iPhone 7 Rose 32GB",
  86. "MN952" : "iPhone 7 Rose 128GB",
  87. "MN9A2" : "iPhone 7 Rose 256GB",
  88.  
  89. # iPhone 7+
  90. "MN4V2" : "iPhone 7+ Jet Black 128GB",
  91. "MN512" : "iPhone 7+ Jet Black 256GB",
  92. "MNQM2" : "iPhone 7+ Black 32GB",
  93. "MN4M2" : "iPhone 7+ Black 128GB",
  94. "MN4W2" : "iPhone 7+ Black 256GB",
  95. "MNQN2" : "iPhone 7+ Silver 32GB",
  96. "MN4P2" : "iPhone 7+ Silver 128GB",
  97. "MN4X2" : "iPhone 7+ Silver 256GB",
  98. "MNQP2" : "iPhone 7+ Gold 32GB",
  99. "MN4Q2" : "iPhone 7+ Gold 128GB",
  100. "MN4Y2" : "iPhone 7+ Gold 256GB",
  101. "MNQQ2" : "iPhone 7+ Rose 32GB",
  102. "MN4U2" : "iPhone 7+ Rose 128GB",
  103. "MN502" : "iPhone 7+ Rose 256GB"
  104. };
  105.  
  106. ## Quick Classes
  107. class AppleStore:
  108. def GetStores(self):
  109. storesData = urllib2.urlopen(appleStores).read()
  110. tmp = json.loads(storesData)
  111. tmp2 = {}
  112. for store in tmp['stores']:
  113. if store['storeEnabled'] is True:
  114. tmp2[store['storeNumber']] = store['storeName']
  115.  
  116. return tmp2
  117.  
  118. def GetStock(self):
  119. return urllib2.urlopen(appleStock).read()
  120.  
  121. class PushBullet:
  122. def __init__(self, apiKey):
  123. self.key = apiKey
  124. self.auth = 'Basic {0}'.format(base64.b64encode(apiKey))
  125.  
  126. def sendNote(self, title, message):
  127. header = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json', 'Authorization': self.auth }
  128. postData = urllib.urlencode({'type': 'note', 'title': title, 'body': message})
  129. req = urllib2.Request("https://api.pushbullet.com/v2/pushes", postData, header)
  130. return urllib2.urlopen(req).read()
  131.  
  132.  
  133. ## Funcs
  134. def DoCheck():
  135. apple = AppleStore()
  136. push = PushBullet(pushbullet_Key)
  137.  
  138. stock = apple.GetStock()
  139. tmp = json.loads(stock)
  140. #tmp = json.load(open("test.txt"))
  141.  
  142. for store in tmp:
  143. if store == "updated":
  144. continue
  145. if store[:1] != 'R':
  146. continue
  147. if allowedStores.find(stores[store]) != -1:
  148. # Store is in allowedStores
  149. for item in tmp[store]:
  150. try: # Exceptions will occur if model is not in the wanted list
  151. if wantedModels[item[:5]] and tmp[store][item] == 'ALL':
  152. # Found stock!
  153. push.sendNote(models[item[:5]], "Stock found at " + stores[store] + "\n\n" + time.strftime('(%d.%b.%y/%I:%M%p)'))
  154. print time.strftime('(%d.%b.%y/%I:%M%p)') + ": Found " + models[item[:5]] + " at " + stores[store]
  155. except:
  156. pass # continue the loop as current model is not wanted
  157.  
  158. print time.strftime('(%d.%b.%y/%I:%M%p)') + ": Finished checking..."
  159.  
  160. ## Main Code
  161. if __name__ == '__main__':
  162. tmp = AppleStore()
  163. tmp2 = PushBullet(pushbullet_Key)
  164. tmp2.sendNote("Stock Checker", "iPhone Stock checker is up and running!\n\nYou will be notifed immediately once stock is found for your locations!")
  165.  
  166. while True:
  167. try:
  168. DoCheck()
  169. except:
  170. print time.strftime('(%d.%b.%y/%I:%M%p)') + ": Hmm, reserve for pickup may be down?"
  171.  
  172. time.sleep(nextCheck)
Add Comment
Please, Sign In to add comment