微信号:PythonTZXY

介绍:每天更新,更新python相关的知识.希望诸君有所收获!

还在医院苦苦排队挂号?Python定时自动挂号和快捷查询化验报告!

2019-01-25 13:32 Python学习交流


实现自动挂号代码



于是就自己通过 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",
"client_mobile": "863008041030718",
"api_name": "api.assay.report.socket",
"params": {"name": username,
"barcode": barcode},
"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()
if resp_json['return_params']['ret_code'] == -1:
return resp_json['return_params']['ret_info']
file_name = resp_json['return_params']['assayreport']['test_name']
username = resp_json['return_params']['assayreport']['name']
sample_type = resp_json['return_params']['assayreport']['sample_type']
report_barcode = resp_json['return_params']['assayreport']['report_barcode']
send_time = resp_json['return_params']['assayreport']['send_time']
send_name = resp_json['return_params']['assayreport']['send_name']
assayreportdetail = resp_json['return_params']['assayreportdetail']
entry_time = resp_json['return_params']['assayreport']['entry_time']
entry_name = resp_json['return_params']['assayreport']['entry_name']
audit_name = resp_json['return_params']['assayreport']['audit_name']
with open(username+file_name+"+"+report_barcode+".md","at") as f:
f.write("|项目||||"+" ")
f.write("|---|---|---|---|"+" ")
f.write("|化验项目|"+sample_type+"|"+file_name+"||"+" ")
f.write("|接收时间|"+send_time+"|||"+" ")
f.write("|报告时间|"+entry_time+"|||"+" ")
f.write("|送检医生|"+send_name+"|||"+" ")
f.write("|报告医生|"+entry_name+"|||"+" ")
f.write("|审计医生|"+audit_name+"|||"+" ")
f.write("|医嘱号|"+report_barcode+"|||"+" ")
f.write(" ")
f.write("|项目|单位|结果|参考范围|"+" ")
f.write("|---|---|---|---|"+" ")
for i in assayreportdetail:
item_name_info = i['item_name']
try:
result_unit_info = i['result_unit']
result_data_info = i['result_data']
ref_range_low_info = i['ref_range_low']
except KeyError:
pass
if result_unit_info:
f.write("|"+item_name_info+"|"+str(result_unit_info)+"|"+result_data_info+"|"+ref_range_low_info+"|"+" ")
else:
f.write("|"+item_name_info+"|"+"|"+result_data_info+"|"+ref_range_low_info+"|"+" ")
if __name__ == '__main__':
session_id = login('xxxx','xxx')
report_barcode = input("请输入医嘱号:")
print(get_huayan_save(session_id,"阿文",report_barcode))

后面我可以把这些数据都导入Excel 之类的表格里面进行统计分析每次的指标变化。


程序员也并没有想象中的那么轻松 熬夜加班等事太过于常见了,希望大家都多多体谅一下程序员!本文转载于程序人生公众号!


 
Python学习交流 更多文章 利用Python与HFSS联合仿真设计一个微带天线! Python这么厉害的么?一次爬完整站小说 2.02亿中国求职者履历曝光!MongoDB数据库发生重大安全事故! 7 种简洁 Python 语法,教你码出一手好代码 ! 那个叫做 Urllib 的库让我们的 python 假装是浏览器!
猜您喜欢 广告技术和区块链的精彩 Wireshark和Fiddler分析Android中的TLS协议包数据(附带案例样本) IE6 png图片透明的解决方法教程 中国移动FAST+敏捷开发运营体系推广现场会在杭圆满闭幕 这可能是开发者社区最成功的愚人节笑话