微软官方商城补货监测提醒脚本

不知道官方的到货通知是否有效,简单地用 Python 写了个库存监测脚本,获取到补货后,可以结合发信脚本,提醒自己。

脚本仅适用于只用一种颜色选项的购买链接,需要结合所获得的 products.json 修改。

仅在 Windows 下测试通过,Linux 下会遇到编码相关问题。

import re, requests
import json
import smtplib
import time
from time import sleep
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
url = 'https://www.microsoftstore.com.cn/certified-refurbished-surface-go-configurate'

def sendMail(target_addr='xxxxxx',title='xxxxxx',content='xxxxxx'):
    smtp_server = 'xxxxxx'
    from_addr = 'sxxxxxx'
    password = 'xxxxxx'
    msg = MIMEText(content, 'html', 'utf-8')
    msg['From']=formataddr(["xxxxxx",from_addr])
    msg['To']=formataddr(["xxxxxx",target_addr]) 
    msg['Subject'] = Header(title, 'utf-8').encode()
    server=smtplib.SMTP_SSL(smtp_server, 465)
    try:
        server.login(from_addr, password)
        server.sendmail(from_addr, [target_addr], msg.as_string())
        server.quit()
    except Exception as e:
        return repr(e)
    return 'OK'


while 1:
	print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
	response = requests.get(url)
	pattern = re.compile(r'"jsonConfig":(.*),\n')

	result = pattern.findall(response.text)

	p = json.loads(result[0])
	#print(p)

	json_str = json.dumps(p, ensure_ascii=False,indent=4)

	with open('products.json', 'w',encoding='utf-8') as fp:
	    fp.write(json_str)

	options = p["attributes"][list(p["attributes"].keys())[0]]["options"]

	total = 0

	for i in options:
	    total = total + p["mainProducts"][list(p["mainProducts"].keys())[0]][i['products'][0]]["is_saleable"]
	    print(i['products'][0],p["mainProducts"][list(p["mainProducts"].keys())[0]][i['products'][0]]["is_saleable"],i['label'] )

	# for pro_id in p["additional"]:
	#     print(pro_id,p["mainProducts"][list(p["mainProducts"].keys())[0]][pro_id]["is_saleable"])

	if total>0 :
	    print("OK!")
	    sendMail()

	print("OOS")

	sleep(8)
运行结果

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Back to Top