Created
July 6, 2021 17:54
-
-
Save ruotianluo/4bb51765d249f302ead0f929e6530977 to your computer and use it in GitHub Desktop.
find_ipad_slot(pick up)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find spot. | |
from bs4 import BeautifulSoup | |
import requests | |
import json | |
# change accordingly. | |
postal_code = '60615' | |
def find_spot(): | |
headers = { | |
#':authority': 'www.apple.com', | |
#':path': '/us-hed/shop/fulfillment-messages?pl=true&parts.0=MHNH3LL/A&location=60615', | |
#':scheme': 'https', | |
'accept': '*/*', | |
'accept-encoding': 'gzip, deflate, br', | |
'accept-language': 'en-US,en;q=0.9,zh;q=0.8', | |
'cookie': "dssid2=1a13590b-b402-4628-86cd-99f4ba0f15dc; dssf=1; pxro=1; dslang=US-EN; xp_ci=3z19Dxcez81Fz5OZzA8szLY87rymu; as_tex=~1~|377147:2:1640937600:USA|384011:1:1616631598:USA|L9GCEjjqE2HT10QmbFET986sF1LH1uwdArXwf42L+WM; POD=us~en; XID=bc12effafefe753837ce2f5567bd80e2; as_sfa=Mnx1cy1oZWR8dXN8fGVuX1VTfGVkdUluZC1oaWdoZXJFZC1kaXNjb3VudGVkfGludGVybmV0fDF8MHwx; as_uct=2; geo=US; s_cc=true; as_pcts=xpelRdo1UljpkHv:_81UTctDhnNiUdEo9KuCkQSXBscZruxYzGf1GH+EhOzG5SEPs3oWx+5cA:vWGDOXFrJZ; at_check=true; as_dc=ucp1; as_affl=p238%7C%7Cmtid%3A%3A2092592t39165%26mnid%3A%3AsF0ZLmHo8-dc_mtid_2092592t39165_pcrid_516125033222_pgrid_100691686715_%26cid%3A%3Awwa-us-kwgo-ipad-slid---Brand-iPadPro-Avail-%26%7C%7C20210526_092800; s_fid=4BB73A283461CD19-38BF8A679B087F5A; s_campaign=wwa-us-kwgo-ipad-slid---Brand-iPadPro-Avail-; s_afc=p238%7CsF0ZLmHo8-dc_mtid_2092592t39165_pcrid_516125033222_pgrid_100691686715_; pt-dm=v1~x~w8cxe7o3~n~ipad pro - overview (us); s_vi=[CS]v1|30573B7B7EC4F8C0-40001EF9616CD5E7[CE]; rtsid=%7BUS%3D%7Bt%3Da%3Bi%3DR035%3B%7D%3B%7D; as_loc=787db1eeb33d3ca775e4c795aab4473d0ee56859c0f0f2b51ec82508dcf158cf4d99de42ac3ed2dc880408dd614a50768855bbd76c0d6345336da27b61ee04bfb4dd16eb46f27d6dad9820cb92c40fc8b8064c0e5e9ba44c1a7330b4286e7247; as_atb=1.0|MjAyMS0wNS0yNiAwOTozOTowMw|b0fbb407163abf62adc1ed5fb8444693d4276b70; pt-dm=v1~x~w8cxe7o3~n~AOS: home/shop_ipad/family/ipad_pro/select; mbox=session#a3350df669334011a5f0d7caa858c550#1622048317|PC#a3350df669334011a5f0d7caa858c550.35_0#1622048945; s_sq=applestoreww%252Cappleglobal%3D%2526c.%2526a.%2526activitymap.%2526page%253DAOS%25253A%252520home%25252Fshop_ipad%25252Ffamily%25252Fipad_pro%25252Fselect%2526link%253Dipad%252520availabilitycity%252520or%252520zip%252520resetsav%252520%252528inner%252520text%252529%252520%25257C%252520no%252520href%252520%25257C%252520body%2526region%253Dbody%2526pageIDType%253D1%2526.activitymap%2526.a%2526.c%2526pid%253DAOS%25253A%252520home%25252Fshop_ipad%25252Ffamily%25252Fipad_pro%25252Fselect%2526pidt%253D1%2526oid%253Dfunctioncr%252528%252529%25257B%25257D%2526oidt%253D2%2526ot%253DDIV", | |
'referer': 'https://www.apple.com/us-hed/shop/buy-ipad/ipad-pro/12.9-inch-display-256gb-space-gray-wifi', | |
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"', | |
'sec-ch-ua-mobile': '?0', | |
'sec-fetch-dest': 'empty', | |
'sec-fetch-mode': 'cors', | |
'sec-fetch-site': 'same-origin', | |
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36', | |
} | |
print('By default, the model is 12.9-inch-display-256gb-space-gray-wifi, if you want to get other type. You need to inspect network at pickup location page, and find the corresponding url.') | |
url = f'https://www.apple.com/us-hed/shop/fulfillment-messages?pl=true&parts.0=MHNH3LL/A&location={postal_code}' | |
try: | |
response = requests.get(url, | |
headers=headers, | |
timeout=10) | |
result = json.loads(response.text) | |
print(result) | |
for store in result['body']['content']['pickupMessage']['stores']: | |
if store['city'] != 'Chicago': | |
continue | |
part = store['partsAvailability'] | |
assert len(part) == 1 | |
part = list(part.values())[0] | |
if part['pickupDisplay'] != "unavailable": | |
print(store) | |
return True | |
return False | |
except: | |
print('Fail to load') | |
return False | |
from typing import List | |
import os | |
import datetime | |
import time | |
import traceback | |
import functools | |
import json | |
import socket | |
import requests | |
DATE_FORMAT = "%Y-%m-%d %H:%M:%S %Z" | |
# replace with your own slack webhook. | |
webhook_url = "https://hooks.slack.com/services/T7LHGNAG1/B0164QL0WKH/XXXXXXX" | |
def slack_sender(webhook_url: str, channel: str, message: str, user_mentions: List[str] = []): | |
""" | |
Slack sender wrapper: execute func, send a Slack notification with the end status | |
(sucessfully finished or crashed) at the end. Also send a Slack notification before | |
executing func. | |
`webhook_url`: str | |
The webhook URL to access your slack room. | |
Visit https://api.slack.com/incoming-webhooks#create_a_webhook for more details. | |
`channel`: str | |
The slack room to log. | |
`user_mentions`: List[str] (default=[]) | |
Optional users ids to notify. | |
Visit https://api.slack.com/methods/users.identity for more details. | |
""" | |
dump = { | |
"username": "DiskAlarm", | |
"channel": channel, | |
"icon_emoji": ":clapper:", | |
} | |
start_time = datetime.datetime.now() | |
contents = ['There is a slot! %s' % start_time.strftime(DATE_FORMAT)] | |
contents.append(message) | |
contents.append(' '.join(user_mentions)) | |
dump['text'] = '\n'.join(contents) | |
dump['icon_emoji'] = ':clapper:' | |
requests.post(webhook_url, json.dumps(dump)) | |
def call_sender(): | |
from twilio.rest import Client | |
print('Get your own free trial twilio account.') | |
account_sid = 'xx' | |
auth_token = 'xx' | |
client = Client(account_sid, auth_token) | |
call = client.calls.create( | |
twiml='<Response><Say>There is a spot!!!!!</Say></Response>', | |
to='+1xxx', | |
from_='+1xxx' | |
) | |
import random | |
while True: | |
if find_spot(): | |
slack_sender(webhook_url, 'knockknock', '') | |
call_sender() | |
time.sleep(60 * 60 * 4) | |
t = random.randint(0, 10) | |
time.sleep(t) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment