实现自动挂号代码
于是就自己通过 Charles 抓包分析了医院的 App 的请求,这里是分析浙江大学第一附属医院的 App,然后用 Python 写了个脚本去模拟登录医院的 App 然后去挂号,具体代码如下:
进群:960410445 获取源文件!
import requests
import json
import time
import datetime
from dateutil.relativedelta import relativedelta
# 登录获取session_id
def login(username,password):
url = "https://zyyy.zwjk.com/api/exec.htm"
data = {"api_Channel":"1",
"client_version":"3.6.6",
"app_id":"zyyy_android",
"app_key":"xxxx",
"user_type":"0",
"client_mobile":"863008041030718",
"api_name":"api.user.user.login.info",
"params":{"phone":username, # 账号
"psw":password}, # 密码
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'User-Agent': "health",
'Host': "zyyy.zwjk.com",
'Connection': "Keep-Alive",
'Accept': "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
'cache-control': "no-cache",
}
response = requests_session.post( url, data={"requestData":json.dumps(data)}, headers=headers)
if response.status_code != 200:
return False
resp_json = response.json()
session_id = resp_json['return_params']['user_model']['session_id']
return session_id
# 获取挂号信息
def get_doctor_info(session_id,appointment_date):
url = "https://zyyy.zwjk.com/api/exec.htm"
payload = {"api_Channel":"1",
"client_version":"3.6.6",
"app_id":"zyyy_android",
"app_key":"xxxx",
"user_type":"0",
"client_mobile":"863008041030718",
"api_name":"api.yygh.expert.schedule.list",
"params":{"type_id":1,
"source_id":"12",
"dept_id":26,
"page_no":1,
"page_size":2147483647
},
"session_id":session_id}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'User-Agent': "health",
'Host': "zyyy.zwjk.com",
'Connection': "Keep-Alive",
'Accept': "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
'cache-control': "no-cache",
}
response = requests_session.post(url, data={"requestData":json.dumps(payload)}, headers=headers)
if response.status_code != 200:
return False
resp_json = response.json()
return_params = resp_json['return_params']['list']
for key in return_params:
if int(key['date']) == int(appointment_date):
doctor_info = key['doctor']
for i in doctor_info:
if i['id'] == 1960 and i['schedulList'][0]['am_pm_flag'] == "1":
return True
def get_time(session_id):
pre_date = (time_now + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
url = "https://zyyy.zwjk.com/api/exec.htm"
payload = {
"api_Channel": "1",
"client_version": "3.6.6",
"app_id": "zyyy_android",
"app_key": "xxxx",
"user_type": "0",
"client_mobile": "863008041030718",
"api_name": "api.yygh.remain.num",
"params": {
"sourceId": "12",
"planId": 9759,
"orderDate": str(pre_date),
"ampmFlag": "1"
},
"session_id": session_id
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'User-Agent': "health",
'Host': "zyyy.zwjk.com",
'Connection': "Keep-Alive",
'Accept': "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
'cache-control': "no-cache",
}
response = requests_session.post(url, data={"requestData": json.dumps(payload)}, headers=headers)
if response.status_code != 200:
return False
resp_json = response.json()
regno = resp_json['return_params']['list'][0]['regno']
timespan = resp_json['return_params']['list'][0]['timespan']
return [regno,timespan]
# 在指定时间挂号
def set_doctor_number(session_id,pre_date,reg_no,timeregion):
url = "https://zyyy.zwjk.com/api/exec.htm"
payload = {
"api_Channel": "1",
"client_version": "3.6.6",
"app_id": "zyyy_android",
"app_key": "Zxxxx",
"user_type": "0",
"client_mobile": "863008041030718",
"api_name": "api.yygh.expert.reservation",
"params": {
"card_no": "x'x'x'x", # 社保卡号
"doct_name": "华佗", # 专家名称
"user_name": "xxx", # 你的姓名
"id_card": "xxxxx", # 身份证号
"phone": "xxxx", # 电话
"reg_id": "xxxx",
"reg_no": reg_no, # 预约号
"dept_name": "科室",
"yuanqu_type": "1",
"type": "1",
"dept_id": 103060302,
"pre_date": str(pre_date), #预约日期
"week_day": "3", # 预约日期是星期几
"plan_id": 9759,
"fee": "14",
"pre_time_type": "1",
"doct_id": "1960",
"clinic_fee": "",
"clinic_time":timeregion
},
"session_id": str(session_id)
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'User-Agent': "health",
'Host': "zyyy.zwjk.com",
'Connection': "Keep-Alive",
'Accept': "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
'cache-control': "no-cache",
}
response = requests_session.post(url, data={"requestData": json.dumps(payload)}, headers=headers)
if response.status_code != 200:
return False
resp_json = response.json()
ret_info = resp_json['return_params']['ret_info']
send_message_wchat("浙一预约挂号结果",ret_info)
# 发送消息到微信
def send_message_wchat(title, content):
loging_datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
url = "https://sc.ftqq.com/SCU9051Tc94a746xxxf1d559xxx5a545ff.send"
querystring = {"text": title, "desp": str(loging_datetime) + str(content)}
response = requests.request("GET", url, params=querystring)
if response != 200:
return
return True
if __name__ == '__main__':
requests_session = requests.Session()
time_now = datetime.datetime.now()
pre_date = (time_now+datetime.timedelta(days=7)).strftime("%Y%m%d")
session_id = login('xxxx','xxxxxx')
if get_doctor_info(session_id,pre_date):
regno = get_time(session_id)[0]
timespan = get_time(session_id)[1]
set_doctor_number(session_id,pre_date,regno,timespan)
else:
send_message_wchat("浙一预约挂号结果","获取列表失败,可能原因:医生不在预约列表中或者医生门诊不在上午")
然后写个计划任务每个月的第一个周三去执行脚本:
0 15 1-7 * * if [ `date '+%w'` = "3" ]; then /usr/bin/python3 /opt/hospital/zheyi.py;fi
执行脚本后结果:
命令行式输出化验单到 markdown 文件中
我觉得查询化验报告的功能不好用,每次都要登录App 然后输入姓名和医嘱号才能查询。
我希望对自己的病情做个管理,把每次的化验结果都保存起来进行分析,于是就实现只要输入医嘱号,就自动输出 markdown 格式的文档里面包含一张表格,如图所示:
代码如下:
import requests
import json
requests_session = requests.Session()
def login(username,password):
url = "https://zyyy.zwjk.com/api/exec.htm"
data = {"api_Channel":"1",
"client_version":"3.6.6",
"app_id":"zyyy_android",
"app_key":"ZW5sNWVWOWhibVJ5YjJsaw==",
"user_type":"0",
"client_mobile":"863008041030718",
"api_name":"api.user.user.login.info",
"params":{"phone":username, # 账号
"psw":password}, # 密码
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'User-Agent': "health",
'Host': "zyyy.zwjk.com",
'Connection': "Keep-Alive",
'Accept': "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
'cache-control': "no-cache",
}
response = requests_session.post( url, data={"requestData":json.dumps(data)}, headers=headers)
if response.status_code != 200:
return False
resp_json = response.json()
session_id = resp_json['return_params']['user_model']['session_id']
return session_id
def get_huayan_save(session_id,username, barcode):
url = "https://zyyy.zwjk.com/api/exec.htm"
payload = {"api_Channel": "1",
"client_version": "3.6.6",
"app_id": "zyyy_android",
"app_key": "ZW5sNWVWOWhibVJ5YjJsaw==",
"user_type": "0",