Last active
September 19, 2023 02:48
-
-
Save Swilder-M/980a298645431781023232cb499ab0e0 to your computer and use it in GitHub Desktop.
Apple Store pickup status monitoring
This file contains 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
import re | |
import requests | |
def get_apple_store_list(): | |
response = requests.get('https://www.apple.com.cn/retail/storelist/') | |
patt = r'<div class="address-lines">(.*?)</div>' | |
states = re.findall(patt, response.text, re.DOTALL) | |
for state in states: | |
store_name_regex = re.compile(r'href="/[^"]+/">(\w+)</a>') | |
store_name = store_name_regex.search(state) | |
store_name = store_name.group(1) if store_name else '' | |
store_code_regex = re.compile(r'data-store-number="(\w+)"') | |
store_code = store_code_regex.search(state) | |
store_code = store_code.group(1) if store_code else '' | |
address_regex = re.compile(r'<address>(.*?)<br/>') | |
address = address_regex.search(state) | |
address = address.group(1) if address else '' | |
print(f'{store_code}: {store_name} {address}') | |
def get_apple_store_status(product_numbers, location='浙江 杭州 西湖区', notify_stores=None): | |
if notify_stores is None: | |
notify_stores = [] | |
headers = { | |
'authority': 'www.apple.com.cn', | |
'accept': '*/*', | |
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8', | |
'cache-control': 'no-cache', | |
'dnt': '1', | |
'pragma': 'no-cache', | |
'referer': f'https://www.apple.com.cn/shop/buy-iphone/iphone-15-pro/{product_numbers[0]}', | |
'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"', | |
'sec-ch-ua-mobile': '?0', | |
'sec-ch-ua-platform': '"macOS"', | |
'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/117.0.0.0 Safari/537.36', | |
} | |
params = { | |
'pl': 'true', | |
'mts.0': 'regular', | |
'location': location, | |
} | |
for index, value in enumerate(product_numbers): | |
params[f'parts.{index}'] = value | |
params[f'mts.{index}'] = 'regular' | |
response = requests.get('https://www.apple.com.cn/shop/fulfillment-messages', headers=headers, params=params) | |
response_dict = response.json() | |
stores = response_dict['body']['content']['pickupMessage']['stores'] | |
for store in stores: | |
store_name = store['state'] + store['city'] + '' + store['storeName'] | |
print(store_name) | |
for _, product_info in store['partsAvailability'].items(): | |
product_status = product_info['pickupDisplay'] | |
product_name = product_info['messageTypes']['regular']['storePickupProductTitle'] | |
print(f'{product_name} {product_status}') | |
if product_status == 'available' and store['storeNumber'] in notify_stores: | |
print(f'{store_name} 可取货: {product_name}') | |
# send to server | |
return False | |
if __name__ == '__main__': | |
# 15 pro 256 白色 MTQ93CH/A | |
# 15 pro 256 原色 MTQA3CH/A | |
# 15 pro 256 蓝色 MTQC3CH/A | |
# 15 pro 256 黑色 MTQ83CH/A | |
# 15 pro 512 白色 MTQE3CH/A | |
# 15 pro 512 原色 MTQF3CH/A | |
# 15 pro 512 蓝色 MTQG3CH/A | |
# 15 pro 512 黑色 MTQD3CH/A | |
pro_256 = ['MTQ93CH/A', 'MTQA3CH/A', 'MTQC3CH/A', 'MTQ83CH/A'] | |
pro_512 = ['MTQE3CH/A', 'MTQF3CH/A', 'MTQG3CH/A', 'MTQD3CH/A'] | |
need_notify_stores = ['R471'] | |
# get_apple_store_list() | |
get_apple_store_status(product_numbers=pro_256, notify_stores=need_notify_stores) | |
get_apple_store_status(product_numbers=pro_512, notify_stores=need_notify_stores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment