环境版本
系统环境:Mac os 13.4
Python版本:3.11
脚本功能
检测数据库验证码是否更新,如更新则发送一条Mac弹窗,db、sql、正则按需进行修改
安装依赖包
pip install pync
pip install pymysql
完整代码
from pync import Notifier
import pymysql
import re
from datetime import datetime
import time
import os
# 1. 配置mysql信息
conn=pymysql.connect(host='xxx', port=xxx, user='xxx', password='xxx',
db='xxx', charset='utf8mb4', )
sql ="select code,create_time from message order by id desc limit 1"
# 2. 判断code文件是否存在,不存在则创建
if not os.path.isfile('code.txt'):
os.system("touch code.txt")
while True:
with conn.cursor() as o_message:
o_message.execute(sql)
message = o_message.fetchone()
regex = r"验证码:(\d+)"
with open('code.txt', 'r') as rfile:
aa = rfile.read()
# 正则提取弹窗内容
match = re.search(regex,message[0]).group(1)
if match == aa:
continue
timedelta = (datetime.now() - message[1]).total_seconds() / 60
if len(match) == 6:
if timedelta < 1:
# 发送验证码
Notifier.notify(f"message最新验证码是{match}",title='数据库验证码更新')
with open('code.txt', 'w') as wfile:
wfile.write(match)
time.sleep(10)