Python 能预约自动预约脚本

这里写自定义目录标题

  • Python 能预约自动预约脚本
    • 数据库结构
    • 代码

Python 能预约自动预约脚本

记录一下

数据库结构

使用的是MySQL,结构如下:
Python 能预约自动预约脚本_第1张图片
这个数据库用于存放要预约的信息,便于批量预约,其实你也可以弄成TXT文件,然后稍微改一下代码也能实现。

代码

# 传入数据库
import json
import time

import pymysql
import requests


# 获取时间戳
def getTime():
    return str(int(time.time()))


# 通用预约
def common_order(username, password, bookstatusid):
    # 登录获取token
    body = {'code': username, 'password': password}
    loginURL = 'http://ligong.deshineng.com:8082/brmclg/api/logon/login'

    timestamp = getTime()
    headers = {'Content-Type': 'application/json'}
    login = requests.post(loginURL + "?time=" + timestamp, json=body, headers=headers)
    token = ''
    loginid = ''
    if login.status_code == 200:
        token = login.json()['data']['token']
        loginid = login.json()['data']['loginid']
    else:
        print(f"{username}登录失败,预约失败")
    loginid = str(loginid)
    # 预约
    headers = {
        'token': token,
        'loginid': loginid,
        'Content-Type': 'application/json;charset=UTF-8'
    }
    payload = json.dumps({})
    timestamp = getTime()
    bookURL = 'http://ligong.deshineng.com:8082/brmclg/api/bathRoom/bookOrder' + "?time=" + timestamp + "&bookstatusid=" + bookstatusid
    bookURL = requests.request("POST", bookURL, headers=headers, data=payload)
    if bookURL.status_code == 200:
        if bookURL.json()['data']['succeed'] == 'Y':
            print(f"{username}预约成功")
        if bookURL.json()['data']['succeed'] == 'Q':
            print(f"{username}重复预约")
    else:
        print(f"{username}预约失败")


# 查询数据库
conn = pymysql.connect(host='', port=3306, user='', password='',
                       database='tjut_bathroom', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
print(cursor)
# 定义SQL语句
sql = "SELECT * FROM orderUserList;"
# 执行SQL语句
cursor.execute(sql)
conn.commit()
# 游标对象关闭
searchList = cursor.fetchall()
cursor.close()
# 关闭连接
conn.close()

# 执行预约操作
for i in searchList:
    common_order(i['username'],i['password'],i['bookstatusid'])

你可能感兴趣的:(python,json)