下载脚本
wget -N https://raw.githubusercontent.com/woniu336/open_shell/main/check_stock.py
修改脚本
使用这个脚本之前,请确保:
- 将
YOUR_ACCESS_TOKEN
替换为您的实际钉钉机器人access token。 - 将
YOUR_SECRET
替换为您的实际钉钉机器人加签密钥(SEC开头的字符串)。 - 安装所需的Python库:
pip install requests
pip install requests
您可以直接运行这个Python脚本:
python3 check_stock.py
右键复制其中一款商品按钮链接:Order Now
填写商品链接、钉钉通知token、加签密钥
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
# 配置信息
URL = "https://my.frantech.ca/cart.php?a=add&pid=1424"
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
SECRET = "YOUR_SECRET"
def generate_sign():
timestamp = str(round(time.time() * 1000))
secret_enc = SECRET.encode('utf-8')
string_to_sign = f'{timestamp}\n{SECRET}'
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return timestamp, sign
def send_dingtalk_notification(message):
timestamp, sign = generate_sign()
webhook_url = f"https://oapi.dingtalk.com/robot/send?access_token={ACCESS_TOKEN}×tamp={timestamp}&sign={sign}"
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "text",
"text": {
"content": message
},
"at": {
"isAtAll": False
}
}
response = requests.post(webhook_url, headers=headers, json=data)
print(f"钉钉通知发送状态: {response.status_code}")
print(f"钉钉通知响应: {response.text}")
def check_stock():
response = requests.get(URL)
if "Out of Stock" in response.text:
print("目前此商品已断货")
else:
print("商品有货啦")
message = f"卢森堡vps有货啦!快去购买: {URL}"
send_dingtalk_notification(message)
if __name__ == "__main__":
check_stock()
定时任务
每2分钟监控一次
(crontab -l ; echo "*/2 * * * * python3 check_stock.py >/dev/null 2>&1") | crontab -
删除任务
crontab -l | grep -v 'check_stock.py' | crontab -
其他
卢森堡服务器设置:
安装curl
apt-get update
apt-get install curl
安装sudo
apt-get update
apt-get install sudo