告别排队!用 Python 定时自动挂号和快捷查询化验报告

2019 年 1 月 25 日 程序人生


我什么要做这个事情


去年单位体检查出问题来,经过穿刺手术确诊是个慢性肾脏病2期, IGA 肾病三期,可能大家对于这个病并不是很了解,但是另外一个词可能大家都听过,叫"尿毒症"。

慢性肾脏病分五期,终末期就是尿毒症。慢性肾脏病非常隐秘,并且病情进展缓慢,一般到尿毒症需要0-20年时间,如果不是体检化验尿液看里面的隐血和尿蛋白指标,根本没任何感觉。

三甲医院人满为患,挂号跟春运买火车票一样的,很麻烦。所以我打算用程序员的方式来解决挂号这件事。


实现自动挂号代码


于是就自己通过 Charles 抓包分析了医院的 App 的请求,这里是分析浙江大学第一附属医院的 App,然后用 Python 写了个脚本去模拟登录医院的 App 然后去挂号,具体代码如下:

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("|项目||||"+"\n")
            f.write("|---|---|---|---|"+"\n")
            f.write("|化验项目|"+sample_type+"|"+file_name+"||"+"\n")
            f.write("|接收时间|"+send_time+"|||"+"\n")
            f.write("|报告时间|"+entry_time+"|||"+"\n")
            f.write("|送检医生|"+send_name+"|||"+"\n")
            f.write("|报告医生|"+entry_name+"|||"+"\n")
            f.write("|审计医生|"+audit_name+"|||"+"\n")
            f.write("|医嘱号|"+report_barcode+"|||"+"\n")
            f.write("\n")
            f.write("|项目|单位|结果|参考范围|"+"\n")
            f.write("|---|---|---|---|"+"\n")
            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+"|"+"\n")
                else:
                    f.write("|"+item_name_info+"|"+"|"+result_data_info+"|"+ref_range_low_info+"|"+"\n")

if __name__ == '__main__':

    session_id = login('xxxx','xxx')
    report_barcode = input("请输入医嘱号:")
    print(get_huayan_save(session_id,"阿文",report_barcode))

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


最后


年纪越大身体经不起体检,希望新的一年里,能够早日康复,也希望所有人都能够健康。

现在我改掉了很多坏习惯,比如熬夜、喝饮料,不吃辣、不烧烤、不饮酒、不吃高盐油腻食品等,命比这些重要。最后提醒大家,为了自己也为了家人,一定要定期体检。

 热 文 推 荐 

女程序员:我负责赚钱养家,老公负责貌美如花

码农的尴与尬:过年相亲

马云获评“全球最具影响力人物”,比起流量明星,这才是中国需要的公众人物!

告别摩拜

6大改进:盘点以太坊的2018冒险之旅

天罡现世,Balong出海,华为这波5G动作666!

2:0!谷歌 AI “AlphaStar“ 虐杀职业星际玩家

print_r('点个好看吧!');
var_dump('点个好看吧!');
NSLog(@"点个好看吧!");
System.out.println("点个好看吧!");
console.log("点个好看吧!");
print("点个好看吧!");
printf("点个好看吧!");
cout << "点个好看吧!" << endl;
Console.WriteLine("点个好看吧!");
fmt.Println("点个好看吧!");
Response.Write("点个好看吧!");
alert("点个好看吧!")
echo "点个好看吧!"


点击“阅读原文”,打开 CSDN App 阅读更贴心!


喜欢就点击“好看”吧
登录查看更多
1

相关内容

JSON( Java Script Object Notation)是一种轻量级的资料交换语言,以文字为基础,且易于让人阅读。尽管 JSON 是在 JavaScript 的一個子集,但 JSON 是独立于语言的文本格式,並且采用了类似于 C 语言家族的一些习惯。
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
机器学习相关资源(框架、库、软件)大列表
专知会员服务
38+阅读 · 2019年10月9日
告别 PS !3 行代码 5 秒搞定抠图的 AI 神器!
程序人生
6+阅读 · 2019年7月11日
Linux挖矿病毒的清除与分析
FreeBuf
14+阅读 · 2019年4月15日
已删除
创业邦杂志
5+阅读 · 2019年3月27日
GitHub 热门:各大网站的 Python 爬虫登录汇总
机器学习算法与Python学习
9+阅读 · 2019年3月20日
C# 10分钟完成百度人脸识别
DotNet
3+阅读 · 2019年2月17日
比Selenium快100倍的方法爬东方财富网财务报表
程序人生
8+阅读 · 2018年10月31日
使用Keras和LSTM生成说唱歌词
论智
5+阅读 · 2018年5月22日
Python 爬虫实践:《战狼2》豆瓣影评分析
数据库开发
5+阅读 · 2018年3月19日
《小美好》短评文本情感分析+生成词云
数据挖掘入门与实战
5+阅读 · 2018年1月7日
【python 自然语言处理】对胡歌【猎场】电视剧评论进行情感值分析
A Comprehensive Survey on Transfer Learning
Arxiv
117+阅读 · 2019年11月7日
Arxiv
3+阅读 · 2019年3月1日
Arxiv
135+阅读 · 2018年10月8日
Knowledge Based Machine Reading Comprehension
Arxiv
4+阅读 · 2018年9月12日
Arxiv
3+阅读 · 2018年4月5日
VIP会员
相关VIP内容
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
机器学习相关资源(框架、库、软件)大列表
专知会员服务
38+阅读 · 2019年10月9日
相关资讯
告别 PS !3 行代码 5 秒搞定抠图的 AI 神器!
程序人生
6+阅读 · 2019年7月11日
Linux挖矿病毒的清除与分析
FreeBuf
14+阅读 · 2019年4月15日
已删除
创业邦杂志
5+阅读 · 2019年3月27日
GitHub 热门:各大网站的 Python 爬虫登录汇总
机器学习算法与Python学习
9+阅读 · 2019年3月20日
C# 10分钟完成百度人脸识别
DotNet
3+阅读 · 2019年2月17日
比Selenium快100倍的方法爬东方财富网财务报表
程序人生
8+阅读 · 2018年10月31日
使用Keras和LSTM生成说唱歌词
论智
5+阅读 · 2018年5月22日
Python 爬虫实践:《战狼2》豆瓣影评分析
数据库开发
5+阅读 · 2018年3月19日
《小美好》短评文本情感分析+生成词云
数据挖掘入门与实战
5+阅读 · 2018年1月7日
【python 自然语言处理】对胡歌【猎场】电视剧评论进行情感值分析
Top
微信扫码咨询专知VIP会员